Hi Everyone, you have all heard of a Newbie but I am the ultra mega newbie! I am 74 years old and am just trying to learn. Have read the books that are out and most is lost on me. What I am trying to do is have one switch turn on one led, have it stay on until a second switch turns it off and turns on another led. and a third led high when neither switch is pressed. I have a real uno r3. have tried many programs I have found on different sites but their purpose appears to be how to make errors. I have been careful about getting it entered correctly. Been working on this since sept 23. any help would be appreciated.
Thanks, Ray
From your description, it sounds like you want to achieve the following behavior:
- Press the first switch (let's call it Switch A) to turn on LED 1.
- While LED 1 is on, pressing the second switch (Switch B) should turn off LED 1 and turn on LED 2.
- If neither switch is pressed, LED 3 should remain on.
To achieve this, you can connect each switch to a digital input pin on the Arduino, and each LED to a digital output pin. You can then use the digitalRead()
function to check the state of each switch, and the digitalWrite()
function to control each LED.
Here's a sample code snippet that implements this behavior:
// Define the pins connected to the switches and LEDs
#define SWITCH_A_PIN 2
#define SWITCH_B_PIN 3
#define LED_1_PIN 8
#define LED_2_PIN 9
#define LED_3_PIN 10
// Variables to hold the state of each switch
bool switchAState = false;
bool switchBState = false;
// Variable to hold the state of LED 3
bool led3State = true;
void setup() {
// Set the switch pins as inputs
pinMode(SWITCH_A_PIN, INPUT);
pinMode(SWITCH_B_PIN, INPUT);
// Set the LED pins as outputs
pinMode(LED_1_PIN, OUTPUT);
pinMode(LED_2_PIN, OUTPUT);
pinMode(LED_3_PIN, OUTPUT);
}
void loop() {
// Read the state of each switch
switchAState = digitalRead(SWITCH_A_PIN);
switchBState = digitalRead(SWITCH_B_PIN);
// Control LED 1 based on the state of Switch A
if (switchAState == HIGH) {
digitalWrite(LED_1_PIN, HIGH);
} else {
digitalWrite(LED_1_PIN, LOW);
}
// Control LED 2 based on the state of Switch B
if (switchBState == HIGH) {
digitalWrite(LED_2_PIN, HIGH);
} else {
digitalWrite(LED_2_PIN, LOW);
}
// Control LED 3 based on the state of both switches
if (!switchAState && !switchBState) {
digitalWrite(LED_3_PIN, HIGH);
} else {
digitalWrite(LED_3_PIN, LOW);
}
}
In this code, the loop()
function continuously checks the state of each switch and controls each LED accordingly. If Switch A is pressed, LED 1 is turned on. If Switch B is pressed while LED 1 is on, LED 1 is turned off and LED 2 is turned on. If neither switch is pressed, LED 3 remains on.
Please note that this code assumes that a HIGH signal from the switch indicates that the switch is pressed. Depending on how your switches are wired, you may need to reverse the logic (i.e., a LOW signal indicates that the switch is pressed). Please adjust the code accordingly if that's the case.
Also, please ensure that you have connected the ground pins of your switches and LEDs to the ground pin of the Arduino, and that you have added appropriate resistors in series with each LED to limit the current flowing through them.
Remember to replace the pin numbers in the #define
statements with the actual pins you have connected your switches and LEDs to Source 1.
So what turns it on and off...?
Note that switches are normally used with INPUT_PULLUP and they are electrically noisy.
Result can be they will switch erratically so some form of debounce will be required which is usually written into the software.
What you've described is feasible, and reasonable for a beginner.
Usually, you can "cut and paste" to avoid typos. There is a lot of bad information and questionable projects out there but as far as I know all of the "official" Arduino Examples are OK.
Some comments & hints -
I usually start with the basic Blink LED Example (or another simple example). It gives me an outline/structure, and it confirms that everything is working and that I can compile & load the code, etc. (Of course you can take-out the blink stuff when you no longer need it.)
"Develop"' your code, adding one or two lines of code at a time, test-compiling and test-running as you go. Even pros don't write the whole program at once. This isn't as easy as it sounds because you can't just start at the top and work-down. The compiler needs to see a "complete program". (i.e. If you delete the last half of program, it won't compile.)
Take advantage of the Serial Monitor (like the Analog Read Serial Example). You can "print-out" variables or little messages like "button one pressed" or "turning on red LED", etc. It's a way to "see" what your program is doing before it's done or especially for debugging when it's not doing what you think it should be doing. Again you can take out or "comment-out" those statements when you no-longer need them. And once you get your project going you can blink the LEDs as in a sequence as a little start-up self test, etc. (I've got a project with some "plug-in" LEDs and it does something like that.)
Work on your input (buttons/switches) and output (LEDs) separately and then put them together after you can read the inputs and control the LEDs.
The two most important concepts in programming are conditional execution, mostly
if-statements, often combined with logical operators like and and or which you will probably need.
The other important concept is loops... Doing something over-and-over, usually until some condition is met. In addition to the main loop() there are for() loops, while() loops, and do-while() loops.
Once you understand conditional execution and loops you can start making useful programs. Overall, C++ is super complex but that will get you started. (And the Arduino makes it pretty easy to get started.)
it's never too soon to start expanding the ways to express what you want.
Here's a diagram that shows the only sense I can make of your words. Give drawing your own diagram of what you want to achieve a shot.
Be sure to account for and let the diagram show
What happens when all LEDs are off and you press button B?
What happens if you press button A again right away after having done?
And same for B, what happens if you press it again?
When exactly was the third LED supposed to be on? Right away neither the first or second is on, so should it be on at the start, and go off as soon as you press any button?
What would make the third LED go on again?
I know, I know. A thousand questions. But if you do not answer them and plan, whatever you code will do something, and that may not be what you wanted.
Imagine your next project, with more pesky details. The more you can anticipate and plan for, the fewer surprises.
HTH and good luck on the learning curve. We all started knowing nothing about nothing, so don't feel bad if it takes a minute to get traction.
a7
Have you gotten a solitary LED to go on and off with two switches?
Got any code to share?
Are your switches like pushbuttons (turn ON when pressed and turn OFF when released) or like toggle switches (turnON and stay ON until flipped OFF)?
Hi Ray, welcome.
By far the best way to achieve your objective would be to start from the "official" examples.
In the IDE
File - examples -
01 Basics - digitalreadSerial
02 Digital (all of them) except the "tone" examples, and maybe
05 Control - SwitchCase
they will give you the basic tools you need, and you can then do a "stepwise" development towards the behaviour you want.
Hello crazyray
Welcome to the world's best Arduino forum ever.
I have read the task description and recommend carrying out the following abstraction before starting the programming:
Based on the IPO model, these three basic software modules are identified:
Input: reading buttons, debouncing and saving status.
Processing: Evaluate button status and define actions.
Output: Execute actions.
Which programming method is used for programming, whether procedural or object-orientated, is your decision.
Have a nice day and enjoy coding in C++.
buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.
i believe the following does what you ask. pressing either button turns just one LED on and the other 2 off. but as soon as neither button is pressed, the 3rd LED is turned back on
const byte PinBut1 = A2;
const byte PinBut2 = A3;
const byte PinLed1 = 11;
const byte PinLed2 = 12;
const byte PinLed3 = 13;
void
loop (void)
{
if (LOW == digitalRead (PinBut1)) {
digitalWrite (PinLed3, LOW); // off
digitalWrite (PinLed2, LOW); // off
digitalWrite (PinLed1, HIGH); // on
}
else if (LOW == digitalRead (PinBut2)) {
digitalWrite (PinLed3, LOW); // off
digitalWrite (PinLed1, LOW); // off
digitalWrite (PinLed2, HIGH); // on
}
else {
digitalWrite (PinLed3, HIGH); // on
}
}
void
setup (void)
{
Serial.begin (9600);
pinMode (PinBut1, INPUT_PULLUP);
pinMode (PinBut2, INPUT_PULLUP);
pinMode (PinLed1, OUTPUT);
pinMode (PinLed2, OUTPUT);
pinMode (PinLed3, OUTPUT);
}
I have no idea how to express my gratitude to all who have help me!
Thus, a giant Thank you....I now have something I can change and modify to see what happens and learn from it.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.