3 leds and 3 buttons delay problem please HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

I have a project in which i have 3 buttons and 3 leds and i have to switch them on and after a 10 second delay they should switch off without the push of a button, like automatically. I have googled and tinkered about many sources but none solved my problem can anyone please share a code which can work and i can modify it thereafter. I am very sorry for the inconvenience i am causing you but thank you in advance.

Cheers

Start from here - https://forum.arduino.cc/index.php?topic=503368.0

Yes you are correct, each led has its own button and answering your second question if we press the first button the first led will turn on for 10 seconds and if the second button Is presser’s the second LED will turn on and it goes on like that. But if I press the same button while the led is on then it should extend by 10 seconds. Thank you Very much for your reply.

I am having issues getting an appropriate code for making my project run. The project consists of the following hardware-
Mega 2560
Breadboard
Leds (3)
Buttons for each led (3)
Resistors 220 ohm
Resistors 10k ohm

I want to take the example of a room with a light but it will switch of after around 10 seconds.

I have searched the arduino forums and YouTube for a code to help with my needs but haven’t come up with any so I wanted to sought help for myself.

If you want me to provide any more information please wait till tomorrow as it is night now.

I thank you all for helping me in advance.

Cheers

arduino12_5:
I want to take the example of a room with a light but it will switch of after around 10 seconds.

10 seconds after what event? What causes it to turn on?

can anyone please share a code which can work and i can modify it thereafter.

And from chapter 16 of the same link

  • we don't mind honest questions toward understanding, but we don't like being conned into doing someone's homework).

So first off we would need to see your attempt at writing code, not finding someone else’s code and pretending it is yours. We would probably ask you why you did certain bits. Then we could tell you in words what you need to do. You then have a go at writing that, testing it, and saying what it did and what you wanted it to do, along with posting the code.

So let’s start with you trying to do this for just one LED not three. Hint look up how to use the millis function to time things.

I want to take the example of a room with a light but it will switch of after around 10 seconds.

So a room with a light. Does this mean one room and three buttons where each button can switch on all three LEDs
or does it mean three different rooms each room with one button and one LED where the button can switch on the LED which is mounted inside that one room?

I'm willing to help. But you should do two things:

  1. provide a much more precsie description of what you want to do
  2. posting some keywords that you think would guide to code that is at least similar to your project

best regards Stefan

Can't find code for your homework I guess...

You haven't provided a wiring diagram or a good description of what the code is supposed to do.

@arduino12_5

TOPIC MERGED.

DO NOT CROSS POST / DUPLICATE

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.

as you can see,
the users in this forum can see only one kind of things through the "glas-sphere": homework.

No it's not a glas-sphere it is logical conclusion. If somebody is really interested in coding she/he will always do one of two things:

  1. make a first attempt in writing code
  2. asking for a tutorial to read and learn how to do it.

Asking for code described just by a too short description is in 99,999% of all cases a lazy student that wants others to do his homework.

There is a small chance that this is not true in your case. So if you want to get rid of this wrong impression do one of the two things already mentioned above

  1. make a first attempt in writing code
  2. asking for a tutorial to read and learn how to do it.

best regards Stefan

arduino12_5:
I have googled and tinkered about many sources but none solved my problem can anyone please share a code which can work and i can modify it thereafter.

There probably is code out there that does what you need. Finding it is another thing, especially when it's difficult to describe what you want and to understand what you see.

Get a grounding in the basics of what needs to happen by working through several things at once and the first five demos in IDE -> file/examples/digital.

Make a flowchart/state diagram of what needs to happen for all conditions, ie. when button pressed timer starts.

Follow DougP's advice. I would also suggest making it work for one button and one LED. i.e., press the button, light up the LED for 10 seconds, if you press while it is lit extend that for 10 more seconds. That should be a rather rudimentary exercise. Once that works you can duplicate for the other buttons and LEDs...assuming I understand your assignment correctly.

I am using this code but it doesn't do what i want it to do and i just wrote it with my understanding. It does this that when i press button no. 3 it switches on all 3 leds at the same time for 5 seconds( I am using 5 seconds delay time instead of 10 now). Please tell me what i should add or remove. (This is not a homework or assignment i just want to do this for my own understanding). I am a newbie as i just started learning arduino 3 weeks back so i am hovering over ideas for projects.

const int ledPin1 =  6;
const int buttonPin1 = 13;
const int ledPin2 =  5;
const int buttonPin2 = 12;
const int ledPin3 =  3;
const int buttonPin3 = 11;

int programState = 0;

int buttonState;
long buttonMillis = 0;
const long intervalButton = 2000;

long ledMillis = 0;
const long intervalLed = 5000;  

void setup() {
pinMode(ledPin1, OUTPUT);      
pinMode(buttonPin1, INPUT); 
pinMode(ledPin2, OUTPUT);      
pinMode(buttonPin2, INPUT); 
pinMode(ledPin3, OUTPUT);      
pinMode(buttonPin3, INPUT);      
digitalWrite(buttonPin1, HIGH);
digitalWrite(buttonPin2, HIGH);
digitalWrite(buttonPin3, HIGH);
}

void loop()
{
unsigned long currentMillis = millis();
buttonState = digitalRead(buttonPin1);
buttonState = digitalRead(buttonPin2);
buttonState = digitalRead(buttonPin3);

if (buttonState == LOW && programState == 0) {
  buttonMillis = currentMillis;
  programState = 1;
}
else if (programState == 1 && buttonState == HIGH) {
      programState = 0; //reset
}
if(currentMillis - buttonMillis > intervalButton && programState == 1) {
  programState = 2;
  ledMillis = currentMillis;

  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, HIGH);
}

if(currentMillis - ledMillis > intervalLed && programState == 2) {
  programState = 0;

  digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, LOW);
}
}

t switches on all 3 leds at the same time

Yes it does because that is exactly what you have written that code to do.

How are your switches wired? That has a vital effect on what you need to write.

You need the timing variables buttonMillis, intervalButton and programState to be different for each LED, for example buttonMillis1, buttonMillis2, buttonMillis3.
Or better still use an array if you know how to use them.

aarg:
10 seconds after what event? What causes it to turn on?

a button press turns it on. there are 3 such buttons which control 3 leds.

this is the new piece of code written by me but this too does not work as when i press the first button the first led should light up but instead of that the last led turns on and what’s worse it does not turn off at all. Please tell me what i have done wrong.

const byte BUTTON1=13;
const byte BUTTON2=12;
const byte BUTTON3=11;
const byte LED1=6;
const byte LED2=5;
const byte LED3=3;
 
unsigned long buttonPushedMillis;
unsigned long ledTurnedOn;
unsigned long turnOnDelay = 20; 
unsigned long turnOffDelay = 5000; 
bool ledReady = false; 
bool ledState = false; 
 
void setup() {
 pinMode(BUTTON1, INPUT_PULLUP);
 pinMode(BUTTON2, INPUT_PULLUP);
 pinMode(BUTTON3, INPUT_PULLUP);
 pinMode(LED1, OUTPUT);
 pinMode(LED2, OUTPUT);
 pinMode(LED3, OUTPUT);
 digitalWrite(LED1, LOW);
 digitalWrite(LED2, LOW);
 digitalWrite(LED3, LOW);
}
 
void loop() {
 unsigned long currentMillis = millis(); 
 
 if (digitalRead(BUTTON1) == LOW) {
  buttonPushedMillis = currentMillis;
  ledReady = true;
 }
 if (digitalRead(BUTTON2) == LOW) {
  buttonPushedMillis = currentMillis;
  ledReady = true;
 }
 if (digitalRead(BUTTON3) == LOW) {
  buttonPushedMillis = currentMillis;
  ledReady = true;
 }

 if (ledReady) {
   if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
     digitalWrite(LED1, HIGH);
     ledState = true;
     ledTurnedOn = currentMillis;
     ledReady = false;
   }
 }

 if (ledReady) {
  if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
    digitalWrite(LED2, HIGH);
    ledState = true;
    ledTurnedOn = currentMillis;
    ledReady = false;
  }
 }

 if (ledReady) {
  if ((unsigned long)(currentMillis - buttonPushedMillis) <= turnOnDelay) {
    digitalWrite(LED3, HIGH);
    ledState = true;
    ledTurnedOn = currentMillis;
    ledReady = false;
  }
 }
  
 if (ledState) {
   if ((unsigned long)(currentMillis - ledTurnedOn) >= turnOffDelay) {
     ledState = false;
     digitalWrite(LED1, LOW);
   }
 }

 if (ledState) {
  if ((unsigned long)(currentMillis - ledTurnedOn) <= turnOffDelay) {
    ledState = false;
    digitalWrite(LED2, LOW);
  }
 }

 if (ledState) {
  if ((unsigned long)(currentMillis - ledTurnedOn) <= turnOffDelay) {
    ledState = false;
    digitalWrite(LED3, LOW);
  }
 }
}

well done as a first attempt. most important t elements are already inside
he basic principle of your code is correct.

In this version you use variables for all three button/LED combinations where it should be
three different sets of variables. On set of variables per Button/LED

You use the variables like if you would expect from

const byte LED_Pin = 5
digitalWrite(LED_Pin,HIGH)

to alternatively switch on LED1 or LED2 or LED3

Each LED needs its own set of variables.
You could name them just like the LED_Pins add a suffix “1” “2” “3”
to the variablename.

for each LED:

If a buttonpress is detected

  • set a bool-variable to true
  • store snapshot of millis() as ButtonPressStartTime

then your loop has another if-condition that is checking
buttonpress == true && is currentTime - ButtonPressStartTime bigger that the onTime

if yes set buttonpress to false

as long as
If buttonpress == true
switch LED on
else
switch LED off

This is enough for a functionality liek a stairway-light. Press Button light switches on for a certain time then switch light of
If the button is pressed while the light is on the countdown starts new

So modify your code and post your new version.

best regards Stefan

could you please give more examples?- this code does not work as it does not switch on any led please give me more examples or a video or document that can make me understand this concept more. All and all thanks a lot Stefan. Please point out my mistake and suggestions.

const byte BUTTON_1=13;
const byte BUTTON_2=12;
const byte BUTTON_3=11;
const byte LED_1=6;
const byte LED_2=5;
const byte LED_3=3;

unsigned long buttonPushedMillis;
unsigned long ledTurnedOn;
unsigned long turnOnDelay = 20; 
unsigned long turnOffDelay = 5000; 
bool ledReady = false; 
bool ledState = false;
bool buttonPress = false;

void setup() {
pinMode(BUTTON_1, INPUT_PULLUP);
pinMode(BUTTON_2, INPUT_PULLUP);
pinMode(BUTTON_3, INPUT_PULLUP);
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(LED_3, OUTPUT);;
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, LOW);
}

void loop() {
unsigned long currentMillis = millis(); 

if (digitalRead(BUTTON_1) == LOW) {
 buttonPress == true;
 ledReady == true;
}
if (digitalRead(BUTTON_2) == LOW) {
 buttonPress == true;
 ledReady == true;
}
if (digitalRead(BUTTON_3) == LOW) {
 buttonPress == true;
 ledReady == true;
}

if (buttonPress) {
  if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
    digitalWrite(LED_1, HIGH);
    ledState = true;
    ledTurnedOn = currentMillis;
    ledReady = false;
  }
}

if (buttonPress) {
 if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
   digitalWrite(LED_2, HIGH);
   ledState == true;
   ledTurnedOn == currentMillis;
   ledReady == false;
 }
}

if (buttonPress) {
 if ((unsigned long)(currentMillis - buttonPushedMillis) <= turnOnDelay) {
   digitalWrite(LED_3, HIGH);
   ledState == true;
   ledTurnedOn == currentMillis;
   ledReady == false;
 }
}
 
if (ledState) {
  if ((unsigned long)(currentMillis - ledTurnedOn) >= turnOffDelay) {
    ledState == false;
    digitalWrite(LED_1, LOW);
  }
}

if (ledState) {
 if ((unsigned long)(currentMillis - ledTurnedOn) <= turnOffDelay) {
   ledState == false;
   digitalWrite(LED_2, LOW);
 }
}

if (ledState) {
 if ((unsigned long)(currentMillis - ledTurnedOn) <= turnOffDelay) {
   ledState == false;
   digitalWrite(LED_3, LOW);
 }
}
}

you already managed to post code as a code-section in post #14
please re-edit your post #19 to get the code into a code-section

for each LED you need its own variable

For LED1 you need:

buttonPress1
ledstate1
ledTurnedOn1
ledready1

For LED2 you need:

buttonPress2
ledstate2
ledTurnedOn2
ledready2

For LED3 you need:

buttonPress3
ledstate3
ledTurnedOn3
ledready3

as button-bouncing occurs only very short in this application button-debouncing is not nescessary
You can take out the debouncing
Then you to take a carefull look at your if-conditions some >= and <= are wrong.
the best way to find out what your code is really doing is to add serial debug-output

  if (ButtonPressed) {
    Serial.println("variable ButtonPressed is true"); // print text
    Serial.print("result of currentMillis - buttonPushedMillis is "); // print text
    Serial.print(result of currentMillis - buttonPushedMillis);
    Serial.print(" turnOnDelay has value ");
    Serial.println(turnOnDelay); 
    
    if ( (currentMillis - buttonPushedMillis) >= turnOnDelay) {
      Serial.println("if ( (currentMillis - buttonPushedMillis) >= turnOnDelay) is true"); // print text
      digitalWrite(LED2, HIGH);
      ledState = true;
      ledTurnedOn = currentMillis;
      ButtonPressed = false;
    }
  }

the function Serial.print() with double-hyphens prints the characters written
the function Serial.print(NameOfVariabe) without double-hyphens prints the value of the variable

function setup needs adding

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");

best regards Stefan

I'm not permanently online. So whenever you have posted a question and are waiting for an aswer
work through some chapters of this tutorial

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan