Hey there,
I have a sketch that uses and int countDown. I want the countDown int to decrease while a button is held and when the int reaches <= 0 some LEDs will flash together and remain alight. The issue I'm experiencing is that the flash on the LEDs occurs when countDown reaches 0 but after that (my script continues to reduce countDown) the lights go back to alternating.
Some pseudo:
if button is held
countDown - 1
if countdown <= 0
LED turn on (should remain on as long as the button is held, countDown is < 0 as the button is held)
I can post some actual code if necessary, thanks in advance for your time.
Edit: Added code.
void loop() {
// put your main code here, to run repeatedly:
switchState = digitalRead(2);
if (switchState == HIGH){
countDown--;
if (countDown <= 0) {
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
}
else {
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(flashLength/lightSpeed);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
lightSpeed++;
delay(flashLength/lightSpeed);
}
}
else {
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
countDown = 10;
lightSpeed = 1;
}
}
I can post some actual code if necessary, thanks in advance for your time.
Please do that and use code tags (the </> icon) when you do.
When you have posted your code we will be able to see how the countDown variable is declared, ie what data type it is, and how often you are decrementing it.
Added the code! Sorry for not doing so immediately.
Also I should note that the first else statement (where the delays are located) is working properly. I have two lights flashing back and forth and I want them to both be on simultaneous and remain on together once countDown is less than 0.
int switchState = 0;
int countDown = 10;
int lightSpeed = 1;
float flashLength = 1000;
void setup() {
// put your setup code here, to run once:
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
switchState = digitalRead(2);
if (switchState == HIGH){
countDown--;
if (countDown <= 0) {
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
}
else {
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(flashLength/lightSpeed);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
lightSpeed++;
delay(flashLength/lightSpeed);
}
}
else {
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
countDown = 10;
lightSpeed = 1;
}
}
There's everything.
How is your switch is wired. Do you have a pulldown resistor in the circuit ?
Although wrapping back around to positives may be the issue (not sure, I'm no CS major) there's a good few seconds before both LEDs light up and they behave like expected. Alternating faster and faster until countDown is at 0, then once it's past zero it resumes alternating at the previous pace and continues alternating until the LEDs "APPEAR" to be both on simultaneously.
Also to UKHeliBob, I do have a pull down resistor (10kOhm). Each of the three lights are wired parallel from power to ground with a 220 Ohm resistor. The button has the pull down resistor.
Delta_G you did it!
That makes way more sense now. But I didn't realize before now that wrapping back to positives was even possible.
So I added a countDown++ inside that if statement and everything works now!
ISSUE SOLVED, thank you both Delta_G and UKHeliBob!
I added a countDown++ inside that if statement
That sounds like a clumsy way to solve the problem. Why not only decrement it if it is not already zero ?