Hi, i am a brand new to this, and i am trying really hard working on control LED with both toggle switch and trigger switch. I got 2 LED attached to Pin 12 and 13, two toggle switches (one switch turn on for all the light and turn off all the light, the other is just turn on and off one LED only) and one trigger switch (which turn on all light when it's HIGH and turn off all light when it's LOW). I can get two toggle to works, but cannot get the trigger switch to work. When i released It will not turn off the lights. I have to used the toggle switch to turn if off. Can some pros help me with the code please "how to turn the light off after i release without interfere with other code of two toggle switches."
Here my code
#include <Bounce.h>
const int button2Pin = 2; // the number of the first pushbutton pin
const int button3Pin = 3; // the number of the first pushbutton pin
const int button4Pin = 4;
const int ledPin = 13; // the number of the LED pin
const int ledPin2 = 12;
// Variables will change:
int ledState = LOW;// the current state of the output pin
int ledState2 = LOW;
int lastButton2State = LOW; // the previous reading from the first push button pin
int lastButton3State = LOW; // the previous reading from the second push button pin
Bounce bouncer1 = Bounce(button2Pin,50); //setting up the debouncing for the first push button
Bounce bouncer2 = Bounce(button3Pin,50); //setting up the debouncing for the second push button
void setup() {
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(button4Pin, INPUT);
}
void loop() {
bouncer1.update ( ); // Update the debouncer for the first push button
bouncer2.update ( ); // Update the debouncer for the second push button
int value1 = bouncer1.read(); // Get the update value
int value2 = bouncer2.read(); // Get the update value
int reading1 = value1 == HIGH;
int reading2 = value2 == HIGH;
if (lastButton2State == HIGH && reading1 == LOW)
{
if (ledState == HIGH) {ledState = LOW;}
else {ledState = HIGH;}
digitalWrite(ledPin, ledState);
}
lastButton2State = reading1;
if (lastButton3State == HIGH && reading2 == LOW)
{
//if (ledState == HIGH) {ledState = HIGH;}
//else {ledState = HIGH;}
//digitalWrite(ledPin, ledState);
if (ledState2 == LOW || ledState == LOW) {
ledState2 = HIGH;
ledState = HIGH;
}
else {ledState2 = LOW;
ledState = LOW;
}
digitalWrite(ledPin2, ledState2);
// if(ledState == HIGH){ledState = LOW;}
//else {ledState = HIGH;}
digitalWrite(ledPin, ledState);
}
lastButton3State = reading2;
int reading3 = digitalRead(button4Pin);
if(reading3 == HIGH){
ledState = HIGH;
ledState2 = HIGH;
digitalWrite(ledPin, ledState);
digitalWrite(ledPin2, ledState2);
}
}
// Thanks for your help...