Put global to static local, fix isPressed bug

This commit is contained in:
wancat
2022-03-02 11:10:00 +08:00
parent 7c940af46a
commit 75eb862880

View File

@@ -1,13 +1,11 @@
#define BUTTON 4 #define BUTTON 4
int LED[] = {5, 6, 9, 10, 11}; int LED[] = {5, 6, 9, 10, 11};
int val[5] = {255, 255, 0, 255, 255}; int val[5] = {255, 255, 0, 255, 255};
int clk = 0;
#define DELAY 100 #define DELAY 100
#define STEP 100 #define STEP 100
#define STOP 1000 #define STOP 1000
int delay_time = DELAY;
bool pressed = 0;
unsigned long checkpoint = 0;
bool isPressed() { bool isPressed() {
static bool last = 1; static bool last = 1;
@@ -17,11 +15,10 @@ bool isPressed() {
} }
last = digitalRead(BUTTON); last = digitalRead(BUTTON);
delay(1); delay(1);
return 0; return result;
} }
void setup() { void setup() {
// put your setup code here, to run once:
Serial.begin(9600); Serial.begin(9600);
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
pinMode(LED[i], OUTPUT); pinMode(LED[i], OUTPUT);
@@ -31,17 +28,20 @@ void setup() {
} }
void loop() { 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()) { if (isPressed()) {
pressed = !pressed; hit = !hit;
if (!pressed) { if (!hit) {
delay_time = DELAY; delay_time = DELAY;
} }
Serial.println(pressed); Serial.println(hit);
} }
if (millis() - checkpoint > delay_time) { if (millis() - checkpoint > delay_time) {
if (pressed) { if (hit) {
delay_time += STEP; delay_time += STEP;
} }
if (delay_time < STOP) { if (delay_time < STOP) {