There's Latest Update!
So i'm workin' on a counter just for fun. How it works is when you press BUTTON1 the LED Lights up red, the buzzer beeps and the variable "counter" adds one to itself; After counting, u can press BUTTON2 then it will first show a transition animation then blink&beep while taking away the value of "counter". After the value of "counter" is less than 1, everything will start from the beginning.
But what actually happens is after the transition animation played, the LED and the buzzer just won't do any thing. I'm sure my phisical connection is all correct, and the buttons are also pulled to ground with a resistor& a 100nf cap to prevent accidental pressing and vibration.
Note that I'm usin' a common anode RGB LED, so the anode pin is always HIGH but to light up individual colors i'll have to pull the cathode pins LOW (remember how electrons flow?)
Update @Tom Carpenter's latest answer still didn't solve the problem. I found the "else" function inside the "if" didn't work at all. So if i put the counter code inside the "else" function, the result would be none. But if i put the counter code in the "if" inside the primary "if", it would, again, count at clock speed, which results blinking forever! So is there anyway for the code to only count once per press&release cycle without losing?
The latest code:
#define RLED 10
#define GLED 11
#define BLED 12
#define B1 4
#define B2 5
#define BZZ 3
boolean OB1V;
int counter;
void setup() {
pinMode(RLED, OUTPUT);
pinMode(GLED, OUTPUT);
pinMode(BLED, OUTPUT);
pinMode(B1, INPUT);
pinMode(B2, INPUT);
pinMode(BZZ, OUTPUT);
OB1V = digitalRead(B1);
}
void loop() {
digitalWrite(RLED, HIGH);
digitalWrite(GLED, HIGH);
digitalWrite(BLED, HIGH);
boolean NB1V = digitalRead(B1);
if (NB1V != OB1V)
{
digitalWrite(RLED, LOW);
digitalWrite(BZZ, HIGH);
counter = 0;
}
else
{
digitalWrite(RLED, HIGH);
digitalWrite(BZZ, LOW);
counter = counter +1;
}
if ( digitalRead(B2) == HIGH )
{
digitalWrite(GLED, LOW);
delay(300);
digitalWrite(GLED, HIGH);
digitalWrite(RLED, LOW);
delay(300);
digitalWrite(RLED, HIGH);
digitalWrite(GLED, LOW);
delay(300);
digitalWrite(GLED, HIGH);
digitalWrite(BLED, LOW);
delay(100);
digitalWrite(BLED, HIGH);
delay(300);
for ( counter; counter >= 1; counter -- )
{
digitalWrite(GLED, LOW);
digitalWrite(BZZ, HIGH);
delay(200);
digitalWrite(GLED, HIGH);
digitalWrite(BZZ, LOW);
delay(200);
}
}
}
counter +1;
doesn't do anything as Chris says. As a result the valuecounter
is always 0, and so the for loop will never do anything (and will probably be removed as dead code by the compiler). If correcting the line causes something else to break, it means something else in the code is broken. – Tom Carpenter May 22 '16 at 1:03