Hi. I am really new to arduino and posting in this forum so please be easy on me.
My concept is if button #1 is pressed, led #1 should light up and if button #2 is pressed, led #2 should light up. Its like choosing an answer where if you pressed button #1, led #1 should light up and led #2 should not, meaning only one led will light up depending on what button was pressed. I really thought that it would be easy since the idea is similar to a radio button when making a form.
My code is just a modified from the debounce example since I need the led to be lit up until the other button is pressed. So I tried to duplicate almost all of the variables. I don't know if I'm doing it correctly.
I actually don't know if debounce actually works in the code because honestly, its the first time I encountered "debounce". I have minimal experience or knowledge on arduino, and by 'minimal', I mean I only have knowledge on blinking led.
//Multiple Choice with 2 buttons and 2 pins
const int buttonPin = 13;
const int buttonPin2 = 12; // the number of the pushbutton pin
const int ledPin = 11; // the number of the LED pin
const int ledPin2 = 10;
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int ledState2 = HIGH;
int buttonState; // the current reading from the input pin
int buttonState2; // the current reading from the input pin
int lastButtonState = LOW;
int lastButtonState2 = LOW; // the previous reading from the input pin
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
int reading2 = digitalRead(buttonPin2);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed: [[DIri ako mag-utro]]
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH [[o diri?]]
if (buttonState == HIGH) {
if (ledState == LOW)
{
digitalWrite(ledState,HIGH);
digitalWrite(ledState2,LOW);
ledState = LOW;
ledstate2 = HIGH;
}
}
}
lastButtonState = reading;
if (reading2 != buttonState) {
buttonState2 = reading2;
// only toggle the LED if the new button state is HIGH [[o diri?]]
if (buttonState2 == HIGH) {
if (ledState2 == HIGH)
{
digitalWrite(ledState,LOW);
digitalWrite(ledState2,HIGH);
ledState = HIGH;
ledstate2 = LOW;
}
}
}
}
if (buttonState2 && buttonState == HIGH)
{
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
lastButtonState2 = reading2;
}
//END
My plan for this project is that once I get this running, I will add one more button and led until I can make a project involving many buttons and pins where led #4 will light up if button #4 is pressed.
Thank you in advance for looking into my problem.