Implement a slowing countdown on button request

This commit is contained in:
Klemens Schölhorn 2017-11-17 17:48:26 +01:00
parent ab2b863493
commit 6b0d42a35f
2 changed files with 47 additions and 9 deletions

View File

@ -8,25 +8,58 @@
#include "uniform_int_dist.h"
#define PIN1 1
#define BUTTON_PIN 1
#define DISPLAY_ENABLE 3
lcg_rng rng;
std::uniform_int_distribution<uint8_t> distribution(1,16);
SegmentDisplay<4, 2, 0> display;
#define NUM_SLOWDOWN 12
const uint16_t PROGMEM SLOWDOWN[NUM_SLOWDOWN] = {
35, 32,
20, 64,
8, 125,
4, 250,
2, 500,
2, 1000
};
void setup() {
pinMode(PIN1, INPUT_PULLUP);
pinMode(3, OUTPUT);
digitalWrite(3, 0);
digitalWrite(0, 0);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(DISPLAY_ENABLE, OUTPUT);
digitalWrite(DISPLAY_ENABLE, 0);
display.setDash();
display.write();
}
void loop() {
if(!digitalRead(PIN1)) {
display.set(distribution(rng));
display.write();
if(digitalRead(PIN1)) {
rng();
return;
}
uint8_t num;
for(size_t i = 0; i < NUM_SLOWDOWN; i += 2) {
for(size_t j = 0; j < pgm_read_word_near(SLOWDOWN + i); ++j) {
num = distribution(rng);
display.set(num);
display.write();
delay(pgm_read_word_near(SLOWDOWN + i + 1));
}
}
for(uint8_t i = 0; i < 2; ++i) {
display.setOff();
display.write();
delay(250);
display.set(num);
display.write();
delay(250);
}
delay(16);
}

View File

@ -36,6 +36,11 @@ public:
number[1] = 0xFF;
}
void setDash() {
number[0] = pgm_read_byte_near(DECODER + 11);
number[1] = pgm_read_byte_near(DECODER + 11);
}
void write() {
shiftOut(PIN_SER, PIN_SCLK, LSBFIRST, number[0]);
shiftOut(PIN_SER, PIN_SCLK, LSBFIRST, number[1]);