Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: (untagged)
I am new to Arduino and have done only a little coding. The code I am looking for is to do the following:
I have one push button as input, five led's as outputs.
The first press should power led1, the second press should shut off led1 and power led2, the third press should shut off led2 and power led3, and so on.
Thanks for your guidance . It looks simple but it is beyond my current capability!
Posted
Updated 9-Nov-20 3:04am

Why don't you look at my Arduino examples on this site (scroll down to hardware programming);
http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=557325[^]

and also have a look at some of the Arduino examples on my website;
http://www.dave-auld.net/index.php?option=com_content&view=category&id=53:arduino-input-output-basics&Itemid=107&layout=default[^]

There should be enough info in amongst those various articles (particularly the ones on my website) to get you going. :)
 
Share this answer
 
What you are trying to do represents a textbook application of a finite state machine (FSM). Read a bit about this concept, maybe on this site or on wikipedia.
The state of your application will be the number of button presses. Remember that buttons will likely bounce, i.e. pressing it once may cause multiple presses to be detected by your program.
From the current number of button presses, you will have to determine the LED to power on, while powering all others off. This is done by calculating the remainder of the number of button presses when divided by the number of LEDs.

There is an important caveat with arduino programming: Think carefully, whether your code should go into the setup() or the loop() function. It may be desirable to leave loop() empty and put everything into setup(); this may give you better control over the way you like to write your code.
 
Share this answer
 
Comments
Richard MacCutchan 9-Nov-20 9:08am    
I think that after eight years the OP is probably not 'new' any more.
noiseland 9-Nov-20 9:18am    
didnt notice the date; this question appeared as a suggestion.
But anyway, somebody may find it helpful.
Richard MacCutchan 9-Nov-20 9:27am    
Unfortunately old questions get popped up when spammers try to use them. It is always worth looking first at the unanswered queue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900