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
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) {