Files
arduino/roulette/roulette.ino
2022-03-02 11:32:46 +08:00

63 lines
1.1 KiB
C++

#define BUTTON 4
int LED[] = {5, 6, 9, 10, 11};
#define DELAY 100
#define STEP 50
bool isPressed() {
// falling edge trigger
static bool last = true;
bool result = false;
if (last && !digitalRead(BUTTON)) {
result = true;
}
last = digitalRead(BUTTON);
delay(1);
return result;
}
void move() {
static int clk = 0;
for (int i = 0; i < 5; i++) {
digitalWrite(LED[i], clk % 5 != i);
}
clk += 1;
}
void setup() {
for (int i = 0; i < 5; i++) {
pinMode(LED[i], OUTPUT);
digitalWrite(LED[i], HIGH);
}
pinMode(BUTTON, INPUT);
}
void loop() {
static int delay_time = DELAY;
static bool stopping = false;
static unsigned long checkpoint = 0;
static int end = 0;
static int step = 0;
if (isPressed()) {
stopping = !stopping;
if (stopping) {
end = random(12, 17);
step = 0;
} else {
step = 0;
}
}
if (millis() - checkpoint > delay_time) {
if (!stopping || step < end) {
move();
}
if (stopping) {
step++;
}
checkpoint = millis();
}
delay_time = DELAY + step * STEP;
}