Cleanup code
This commit is contained in:
@@ -1,25 +1,31 @@
|
||||
#define BUTTON 4
|
||||
int LED[] = {5, 6, 9, 10, 11};
|
||||
int val[5] = {255, 255, 0, 255, 255};
|
||||
|
||||
#define DELAY 100
|
||||
#define STEP 100
|
||||
#define STOP 1000
|
||||
#define STEP 50
|
||||
|
||||
|
||||
bool isPressed() {
|
||||
static bool last = 1;
|
||||
int result = 0;
|
||||
if (last == 1 && digitalRead(BUTTON) == LOW) {
|
||||
result = 1;
|
||||
// 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() {
|
||||
Serial.begin(9600);
|
||||
for (int i = 0; i < 5; i++) {
|
||||
pinMode(LED[i], OUTPUT);
|
||||
digitalWrite(LED[i], HIGH);
|
||||
@@ -28,29 +34,29 @@ void setup() {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
static int clk = 0;
|
||||
static int delay_time = DELAY;
|
||||
static bool hit = 0;
|
||||
static bool stopping = false;
|
||||
static unsigned long checkpoint = 0;
|
||||
static int end = 0;
|
||||
static int step = 0;
|
||||
if (isPressed()) {
|
||||
hit = !hit;
|
||||
if (!hit) {
|
||||
delay_time = DELAY;
|
||||
stopping = !stopping;
|
||||
if (stopping) {
|
||||
end = random(12, 17);
|
||||
step = 0;
|
||||
} else {
|
||||
step = 0;
|
||||
}
|
||||
Serial.println(hit);
|
||||
}
|
||||
|
||||
if (millis() - checkpoint > delay_time) {
|
||||
if (hit) {
|
||||
delay_time += STEP;
|
||||
if (!stopping || step < end) {
|
||||
move();
|
||||
}
|
||||
if (delay_time < STOP) {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
analogWrite(LED[i], val[(i + clk) % 5]);
|
||||
if (stopping) {
|
||||
step++;
|
||||
}
|
||||
clk += 1;
|
||||
}
|
||||
|
||||
checkpoint = millis();
|
||||
}
|
||||
delay_time = DELAY + step * STEP;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user