Commit 773f2a56 authored by Aghayev Murad's avatar Aghayev Murad
Browse files

Update sketch_aug29a.ino, funshield.h files

parents
// Funshield constants
#ifndef FUNSHIELD_CONSTANTS_H__
#define FUNSHIELD_CONSTANTS_H__
// convenience constants for switching on/off
constexpr int ON = LOW;
constexpr int OFF = HIGH;
// 7-segs
constexpr int latch_pin = 4;
constexpr int clock_pin = 7;
constexpr int data_pin = 8;
// buzzer
constexpr int beep_pin = 3;
// LEDs
constexpr int led1_pin = 13;
constexpr int led2_pin = 12;
constexpr int led3_pin = 11;
constexpr int led4_pin = 10;
// buttons
constexpr int button1_pin = A1;
constexpr int button2_pin = A2;
constexpr int button3_pin = A3;
// trimr
constexpr int trimr_pin = A0;
// digits
constexpr int digits[11] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xff };
constexpr int digit_muxpos[4] = { 0x01, 0x02, 0x04, 0x08 };
#endif
#include "funshield.h"
constexpr size_t interval_to_boost = 600;
constexpr size_t boost_interval = 300;
char actual_display[4] = {'0', '0', '0', '0'};
char config_display[4] = {'1', 'd', '4', '.'};
int number_of_sides = 0;
int number_of_throws = 1;
struct Button {
bool pressed = false;
bool boost = false;
size_t pin;
size_t last_read;
Button (size_t button_pin){
pin = button_pin;
}
void setup(){
pinMode (pin, INPUT);
}
bool is_on(){
int readedValue = digitalRead(pin);
if (!pressed){
if(!readedValue){
pressed = true;
last_read = millis();
return true;
}
}
else if(!boost){
if(!readedValue && millis() - last_read > interval_to_boost){
boost = true;
last_read += interval_to_boost;
return true;
}
else if(readedValue){
pressed = false;
}
}
else{
if(!readedValue && millis() - last_read > boost_interval){
last_read += boost_interval;
return true;
}
else if (readedValue){
boost = false;
pressed = false;
}
}
return false;
}
};
struct Buttons{
Button buttons[3] = {
Button(button1_pin),
Button(button2_pin),
Button(button3_pin)
};
void setup(){
for (int i = 0; i < 2; i++){
buttons[i].setup();
}
}
bool is_on(int button_number){
return buttons[button_number - 1].is_on();
}
};
struct Display {
int position = 0;
Buttons buttons = Buttons();
void setup(){
pinMode(latch_pin, OUTPUT);
pinMode(clock_pin, OUTPUT);
pinMode(data_pin, OUTPUT);
randomSeed(random(0, 10));
buttons.setup();
}
void write_glyph (byte glyph, byte position){
digitalWrite(latch_pin, LOW);
shiftOut(data_pin, clock_pin, MSBFIRST, glyph);
shiftOut(data_pin, clock_pin, MSBFIRST, position);
digitalWrite(latch_pin, HIGH);
}
void write_symbol(char symbol, byte position) {
if (isAlpha(symbol)) {
write_glyph (0b10100001, 1 << position);
}
else if (isDigit(symbol)){
write_glyph (digits[symbol - '0'], 1 << position);
}
else{
write_glyph (0b11111111, 1 << position);
}
}
void fill_in_display() {
write_symbol(actual_display[position], position);
position++;
position %= 4;
}
void change_number_of_sides() {
char first[7] = {'4', '6', '8', '1', '1', '2', '0'};
char second[7] = {'.', '.', '.', '0', '2', '0', '0'};
config_display[2] = first[number_of_sides];
config_display[3] = second[number_of_sides];
}
int normal_number_of_sides() {
int configurations[7] = {4, 6, 8, 10, 12, 20, 100};
return configurations[number_of_sides];
}
void change_actual_display(char new_display[]) {
for (int i = 0; i <= 3; i++) {
actual_display[i] = new_display[i];
}
}
void new_random() {
int sum = 0; int number = 0;
for (int i = 0; i < number_of_throws; i++) {
do {
number =millis() * random(100, 1000) % (normal_number_of_sides()+1);
} while (number < 1 || number > normal_number_of_sides());
sum += number;
}
char blank[4] = {'.', '.', '.', '.'};
change_actual_display(blank);
sprintf(blank, "%d", sum);
int n = log10(sum);
int counter = 0;
for (int i = 3 - n; i <= 3; i++) {
actual_display[i] = blank[counter++];
}
}
long last = 0;
void process_buttons() {
long current = millis();
if (buttons.is_on(1) && current - last > 300) {
new_random();
change_actual_display(actual_display);
last = millis();
}
if (buttons.is_on(2) && current - last > 300) {
if (number_of_throws < 9){
number_of_throws++;
} else {
number_of_throws = 1;
}
config_display[0] = number_of_throws + '0';
change_actual_display(config_display);
last = millis();
}
if (buttons.is_on(3) && current - last > 300) {
if (number_of_sides < 6){
number_of_sides++;
} else {
number_of_sides = 0;
}
change_number_of_sides();
change_actual_display(config_display);
last = millis();
}
}
};
Display d;
void setup() {
d.setup();
}
void loop() {
d.process_buttons();
d.fill_in_display();
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment