Hi All
New to Arduino and amazed by the great projects that everyone creates and looking for a little help
I've started with a few basic things like button, debounce etc but finding it difficult adding extra code or modding the code to add extra items like inputs and outputs
My circuit is kinda simple, 3 inputs 2 outputs
1st input (switch) triggers 1st output using debounce to keep an redLED on
pressed again LED is off
2nd input (switch) triggers 2nd output using debounce to keep an greenLED on
pressed again LED is off
3rd input (switch) is to turn off the LED's if on
the 3rd input is where I am going wrong and cant figure out the code, I would like the 3rd input to turn off either LED if on
or further to this - the 1st and 2nd inputs can only turn ON and latch the corresponding LEDs and the 3 input turns them off,
the 1st and 2nd inputs cannot be activated if another LED is on - if RED led is on then GREEN cannot activate until RED led is turned off by input 3 and same for the GREEN Led
if anyone has any pointers, reference to projects, reference to code id be grateful for the direction
ive posted the code below for what I have so far
BR
const int redbutton = 3; //First input
const int greenbutton = 4; //Second input
const int stopbutton = 5; //Third input
const int redled = 9; //First output
const int greenled = 10; //Second output
int redledState = HIGH;
int greenledState = HIGH;
int redbuttonState;
int greenbuttonState;
int stopbuttonState;
int redlastButtonState = LOW;
int greenlastButtonState = LOW;
int stoplastButtonState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;
void setup() {
pinMode(redbutton, INPUT);
pinMode(redled, OUTPUT);
digitalWrite(redled, redledState);
pinMode(greenbutton, INPUT);
pinMode(greenled, OUTPUT);
digitalWrite(greenled, greenledState);
pinMode(stopbutton, INPUT);
pinMode(redled, OUTPUT);
pinMode(greenled,OUTPUT);
digitalWrite(redled, redledState);
digitalWrite(greenled, greenledState);
}
void loop() {
int reading = digitalRead(redbutton);
if (reading != redlastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != redbuttonState) {
redbuttonState = reading;
if (redbuttonState == HIGH) {
redledState = !redledState;
}
}
}
digitalWrite(redled, redledState);
redlastButtonState = reading;
{
int reading = digitalRead(greenbutton);
if (reading != greenlastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != greenbuttonState) {
greenbuttonState = reading;
if (greenbuttonState == HIGH) {
greenledState = !greenledState;
}
}
}
digitalWrite(greenled, greenledState);
greenlastButtonState = reading;
{
int reading = digitalRead(stopbutton);
if (reading != stoplastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != stopbuttonState) {
stopbuttonState = reading;
if (stopbuttonState == HIGH) {
greenledState = !greenledState;
}
}
}
digitalWrite(greenled, greenledState);
stoplastButtonState = reading;
}
{
int reading = digitalRead(stopbutton);
if (reading != stoplastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != stopbuttonState) {
stopbuttonState = reading;
if (stopbuttonState == HIGH) {
redledState = !redledState;
}
}
}
digitalWrite(redled, redledState);
stoplastButtonState = reading;
}
}
}