Hy guys
Am looking to see if anyone can help me out whit a code am working on
Ive one button controllin 2 leds
When button pressed led1 to come on for 5 sec and off then led2 to come on for 10 sec and off
link below on how far i got
Any help will be appreciated
As far as I can see, the code is working as written:
LED 1 and 2 come on at the same time, and stay on for 2 and 3 seconds respectively.
You will need to change the intervals to 5000 and 10000 respectively if that is what you want.
If you want the second LED to come on when the first one goes off, then please move the LED2 on statement from the BTN_PIN read if to just after the statement that turns the first LED off.
Hope this helps
thanks..led2 to come on after led1 goes off
got lost a bit..can you be a bit more specific if you dont mind
I think actually that you may want to add a variable:
bool Led2_active = false;
This should help keep track of the button press and that LED2 needs to be turned on.
Then, in the if statement where you check the button state, add
Led2_active = true;
And then in the if statement where you write LED1 LOW, add
if (Led2_active){
digitalWrite(LED_TWO, HIGH);
Led2_active = false;
See if this works the way you want it to.
You could name the variable buttonPressed or something else that makes more sense to you.
Thanks for using wokwi. You should post your code here, too, so ppl who don't wanna go there can see it. And so it will be here if it gets erased over on wokwi.
Now.
Turn on LED_TWO where you turn off LED_ONE, not when the button is pressed.
Set prev_millis_led_two when you turn on LED_TWO. not when the button is pressed.
Please: Only turn off LED_ONE (and therefor turn on LED_TWO) if LED_ONE is on. You can digitalRead() LED_ONE to see if it is on and actually needs to be turned off.
LED_TWO can be turned off even if it already turned off, it will just… stay off. Can't get more off than off.
There is no need to set prev_millis_led_one or prev_millis_led_two in the if statements that turn off the LEDs.
Use current_millis everywhere you now have millis(), except of course the one at the top of you loop() function. Since millis() will advance while you hold the button down, it messes with the logic.
In fact, holding the button down will mess with the timing - your current code, which lights both LEDs, will be messed up by how long you hold down the button.
So you should really be reacting not to the button being LOW, but to it going LOW.
See Examples / 02. Digital / StateChangeDetection in the IDE for how to fix that up.
HTH
a7
so guys am there but not there yet
when i press button led1 comes on for 5 sec and off then led 2 comes on as planned for 10 sec but wont turn off after timer runs down
ill post the code here to see if any1 mite have a fix
#define BTN_PIN 5
#define LED_ONE 9
#define LED_TWO 8
unsigned long current_millis = 0;
unsigned long prev_millis_led_one = 0;
unsigned long prev_millis_led_two = 0;
const unsigned long interval_led_one = 5000;
const unsigned long interval_led_two = 10000;
void setup() {
pinMode(BTN_PIN, INPUT_PULLUP); // sets the digital pin 5 as input
pinMode(LED_ONE, OUTPUT); // sets the digital pin 8 as output
pinMode(LED_TWO, OUTPUT); // sets the digital pin 9 as output
Serial.begin(9600);
}
void loop() {
current_millis = millis();
if(digitalRead(BTN_PIN) == LOW){
digitalWrite(LED_ONE, HIGH);
prev_millis_led_one = millis();
}
if (current_millis - prev_millis_led_one >= interval_led_one) {
digitalWrite(LED_ONE, LOW);
digitalWrite(LED_TWO, HIGH);
prev_millis_led_one = current_millis;
prev_millis_led_two = millis();
}
if (current_millis - prev_millis_led_two >= interval_led_two) {
digitalWrite(LED_TWO, LOW);
digitalWrite(LED_TWO, LOW);
prev_millis_led_two = current_millis;
prev_millis_led_two = current_millis;
}
}
Hello danailenei
Try this propsal for your project.
#define BTN_PIN 5
#define LED_ONE 9
#define LED_TWO 8
void setup() {
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(LED_ONE, OUTPUT);
pinMode(LED_TWO, OUTPUT);
}
void loop() {
if (!digitalRead(BTN_PIN)) {
digitalWrite(LED_ONE, HIGH);
delay (5000);
digitalWrite(LED_ONE, LOW);
digitalWrite(LED_TWO, HIGH);
delay (10000);
digitalWrite(LED_TWO, LOW);
}
}
Have a nice day and enjoy coding in C++.
Дайте миру шанс!
thanks paul..that works perfect
So much for getting some decent code to work.
@danailenei if you are interested in finishing your origianl approach, please show us your latest effort (in code tags) in a new post on this thread, and if you are using it, a link to the wokwi.
And re-read my earlier #6 - it appears you took some of my advices but not all.
a7
thanks alto..i will give it another try
This in particular:
Like
# define BTN_PIN 5
# define LED_ONE 9
# define LED_TWO 8
unsigned long current_millis;
unsigned long prev_millis_led_one = 0;
unsigned long prev_millis_led_two = 0;
// testing - life too short for 15 seconds per test!
const unsigned long interval_led_one = 700;
const unsigned long interval_led_two = 700;
void setup() {
pinMode(BTN_PIN, INPUT_PULLUP); // sets the digital pin 5 as input
pinMode(LED_ONE, OUTPUT); // sets the digital pin 8 as output
pinMode(LED_TWO, OUTPUT); // sets the digital pin 9 as output
Serial.begin(9600);
}
void loop() {
current_millis = millis();
if (digitalRead(BTN_PIN) == LOW) {
digitalWrite(LED_ONE, HIGH);
prev_millis_led_one = current_millis;
}
if (current_millis - prev_millis_led_one >= interval_led_one) {
if (digitalRead(LED_ONE)) {
digitalWrite(LED_ONE, LOW);
digitalWrite(LED_TWO, HIGH);
prev_millis_led_two = current_millis;
}
}
if (current_millis - prev_millis_led_two >= interval_led_two) {
digitalWrite(LED_TWO, LOW);
prev_millis_led_two = current_millis;
}
}
You still need to look fro the button going LOW rather than being LOW.
Also: use the "Autoformat" tool in the IDE, then use the "Copy for Forum" tool in the IDE, then paste your code here. Whenever you share code on these fora.
a7
thanks for advice and help...much appreciated
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.