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.
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.
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:
provide a much more precsie description of what you want to do
posting some keywords that you think would guide to code that is at least similar to your project
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:
make a first attempt in writing code
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
make a first attempt in writing code
asking for a tutorial to read and learn how to do it.
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.
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.
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.
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
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.
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
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.