Commit b3715e7b authored by Aghayev Murad's avatar Aghayev Murad
Browse files

Update sketch_aug29a.ino

parent 773f2a56
#include "funshield.h"
constexpr size_t interval_to_boost = 600;
constexpr size_t boost_interval = 300;
enum ButtonState {UP, DOWN};
struct Button{
long deadline;
ButtonState state;
unsigned char pin;
};
Button button_A, button_B, button_C;
unsigned long first_press = 800000;
unsigned long latter_press = 50000;
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;
}
}
inline long duration(long now, long then) {return (now-then);}
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 init_button(Button& b, int which){
b.pin = which;
b.state = UP;
}
void setup(){
for (int i = 0; i < 2; i++){
buttons[i].setup();
}
int get_pulse(Button& b){
if(digitalRead(b.pin) == OFF){
b.state = UP;
return 0;
}
bool is_on(int button_number){
return buttons[button_number - 1].is_on();
long now = micros();
switch(b.state){
case UP:
b.deadline = now + first_press;
b.state = DOWN;
return 1;
case DOWN:
if (duration(now, b.deadline) > 0){
b.deadline = now + latter_press;
return 1;
} else return 0;
}
};
}
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();
init_button(button_A, button1_pin);
init_button(button_B, button2_pin);
init_button(button_C, button3_pin);
}
void write_glyph (byte glyph, byte position){
......@@ -131,7 +101,7 @@ struct Display {
void new_random() {
int sum = 0; int number = 0;
randomSeed(button_A.deadline);
for (int i = 0; i < number_of_throws; i++) {
do {
number =millis() * random(100, 1000) % (normal_number_of_sides()+1);
......@@ -152,13 +122,13 @@ struct Display {
long last = 0;
void process_buttons() {
long current = millis();
if (buttons.is_on(1) && current - last > 300) {
if (get_pulse(button_A)) {
new_random();
change_actual_display(actual_display);
last = millis();
}
if (buttons.is_on(2) && current - last > 300) {
if (get_pulse(button_B)) {
if (number_of_throws < 9){
number_of_throws++;
} else {
......@@ -169,7 +139,7 @@ struct Display {
last = millis();
}
if (buttons.is_on(3) && current - last > 300) {
if (get_pulse(button_C)) {
if (number_of_sides < 6){
number_of_sides++;
} else {
......@@ -192,4 +162,4 @@ void 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