From 75eb8628804fc2882bc4fce5e742b2bf607b8be5 Mon Sep 17 00:00:00 2001 From: wancat Date: Wed, 2 Mar 2022 11:10:00 +0800 Subject: [PATCH] Put global to static local, fix isPressed bug --- roulette/roulette.ino | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/roulette/roulette.ino b/roulette/roulette.ino index 6d397bf..ce6cd89 100644 --- a/roulette/roulette.ino +++ b/roulette/roulette.ino @@ -1,13 +1,11 @@ #define BUTTON 4 int LED[] = {5, 6, 9, 10, 11}; int val[5] = {255, 255, 0, 255, 255}; -int clk = 0; + #define DELAY 100 #define STEP 100 #define STOP 1000 -int delay_time = DELAY; -bool pressed = 0; -unsigned long checkpoint = 0; + bool isPressed() { static bool last = 1; @@ -17,11 +15,10 @@ bool isPressed() { } last = digitalRead(BUTTON); delay(1); - return 0; + return result; } void setup() { - // put your setup code here, to run once: Serial.begin(9600); for (int i = 0; i < 5; i++) { pinMode(LED[i], OUTPUT); @@ -31,17 +28,20 @@ void setup() { } void loop() { - // put your main code here, to run repeatedly: + static int clk = 0; + static int delay_time = DELAY; + static bool hit = 0; + static unsigned long checkpoint = 0; if (isPressed()) { - pressed = !pressed; - if (!pressed) { + hit = !hit; + if (!hit) { delay_time = DELAY; } - Serial.println(pressed); + Serial.println(hit); } if (millis() - checkpoint > delay_time) { - if (pressed) { + if (hit) { delay_time += STEP; } if (delay_time < STOP) {