Hi, how can we program a push button with 2 LED such as green and red. When the button is not pushed the green light turn on and when the button is pushed, the red light will turn on and the green light will turn off
Read about lighting an LED using Arduino and a pushbutton...
https://docs.arduino.cc/built-in-examples/digital/Button/
Then, add another LED and keep track of which LED is on and which LED is off.
Hi @cookiesss,
this is the second most basic programming exercise that exists.
The first most basic exercise is to switch a single LED on off.
Presenting you the ready t run code would be serving you cooked fish.
This is not the main purpose of this forum.
The main purpose of this forum is:
Answering specific questions that arised from writing your own attempt to create whatever kind of functionality.
You are asking for the full and working code. Break down this question into specific details such as
- how can I switch on an LED?
- how can I make a code that reacts on a button press?
If you combine such questions with the word "arduino" google searching will be very successful
https://www.google.com/search?as_q=arduino+how+can+I+switch+on+an+LED
https://www.google.com/search?q=arduino+how+can+I+make+a+code+that+reacts+on+a+button+press
Whatever question comes to your mind. Combine it with the word "arduino" and google for it
There will be plenty of hits
If you take one of these online found programs
start to read it and if you do not understand what a certain line of code is doing
ask a specific question about this line of code
If you would like to learn programming in general this is one good point to start
Take a look into this tutorial:
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
Hello cookiesss
Welcome to the world's best Arduino forum ever.
Consider:
enum LedNames {Red,Green};
constexpr uint8_t LedPins[] {9,10}; // [port pin] -- -->|-- -- [GND]
constexpr uint8_t ButtonPin {A0}; // [port pin] -- [button] -- [GND]
void setup()
{
for (auto LedPin:LedPins) pinMode(LedPin,OUTPUT);
pinMode(ButtonPin,INPUT_PULLUP);
}
void loop()
{
digitalWrite(LedPins[Green],digitalRead(ButtonPin)?LOW:HIGH);
digitalWrite(LedPins[Red],digitalRead(LedPins[Green])?LOW:HIGH);
}
Have a nice day and enjoy coding in C++.
Thank you for your help!
Thank you, I learned something new to me, I didn't know "constexpr" so I'd neve thought using "constexpr" to do that! ![]()
By the way, I understand the first "constexpr" is useful when defining constant arrays, but how the second one differs from a standard "const uint8_t ButtonPin = A0;"?
i've often felt ignorant because i'm unwilling to use features such as this (i.e. auto). i am reminded of what Stroustrup wrote in the 3rd ed of his book, not to use features unnecessarily. (yes i realize this is a trivial feature)
but considering that some high school robotics students need to write controller code in Java, i think it better to use conventional coding common to various languages.
Well, each programming language has its features (aka pros and cons), so as far as I can see, that line:
for (auto LedPin:LedPins) pinMode(LedPin,OUTPUT);
is equivalent to a "foreach" widely known function (e.g. on C#, Java,...), but the above is the C++ standard, so who knows which comes forst, the ogg or the chicken (the C++ or C#/Java)? ![]()
PS: another thing I didn't know is "auto", I suppose it's like "var" on other languages, meaning "I don't know the data type, please let the compiler decide, acquiring the array element type" ;-)). In this case it isn't necessary, it could be "byte" (or "uint8_t").
foreach (byte LedPin in LedPins)
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.