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