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" #include "uniform_int_dist.h"
#define PIN1 1 #define BUTTON_PIN 1
#define DISPLAY_ENABLE 3
lcg_rng rng; lcg_rng rng;
std::uniform_int_distribution<uint8_t> distribution(1,16); std::uniform_int_distribution<uint8_t> distribution(1,16);
SegmentDisplay<4, 2, 0> display; 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() { void setup() {
pinMode(PIN1, INPUT_PULLUP); pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(3, OUTPUT);
digitalWrite(3, 0); pinMode(DISPLAY_ENABLE, OUTPUT);
digitalWrite(0, 0); digitalWrite(DISPLAY_ENABLE, 0);
display.setDash();
display.write();
} }
void loop() { void loop() {
if(!digitalRead(PIN1)) { if(digitalRead(PIN1)) {
display.set(distribution(rng)); rng();
display.write(); return;
} }
delay(16);
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);
}
} }

View File

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