Hi all,
I have a question regarding my code. I have created a toggle function to a momentary switch that switches outputs when it is pressed, after a break; press1: LED1 ON/LED2 OFF, press2: LED1 & 2 OFF, press3: LED1 OFF/LED2, press4: LED1 & 2 OFF, repeat.
The issue I have encountered is that when I try to have two buttons to each have control of two LEDs each, the LEDs seem to bypass the second button and flash in an unexpected manner.
The files compile fine without errors, so I am hoping for some advice and critique.
I have included the code for both single and double button iterations.
Thank you:)
int ledPin1 = 10;
int ledPin2 = 9;
int switchPin = 2;
int count = 0;
boolean lastButton;
boolean currentButton = false;
boolean ledOn = false;
void setup() {
pinMode(switchPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
count = 0;
}
//debounce function to stabilise the button
boolean debounce(boolean last)
{
boolean current = digitalRead(switchPin);
if (last != current)
{
delay(5);
current = digitalRead(switchPin);
}
return current;
}
void loop() {
lastButton = currentButton;
currentButton = debounce(lastButton);
if (lastButton == false && currentButton == true)
{
if (count == 0)
{
count++;
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
}
else if (count == 1)
{
count++;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
else if (count == 2)
{
count++;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
}
else if (count == 3)
{
count = 0;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
}
}
int ledPin1 = 10;
int ledPin2 = 9;
int ledPin3 = 8;
int ledPin4 = 7;
int switchPin = 2;
int switchPin2 = 3;
int count = 0;
boolean lastButton;
boolean currentButton = false;
boolean ledOn = false;
boolean lastButton2;
boolean currentButton2 = false;
boolean ledOn2 = false;
void setup() {
pinMode(switchPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(switchPin2, INPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
count = 0;
}
//debounce function to stabilise the button1
boolean debounce(boolean last)
{
boolean current = digitalRead(switchPin);
if (last != current)
{
delay(5);
current = digitalRead(switchPin);
}
return current;
//debounce function to stabilise the button2
}
boolean debounce2(boolean last2)
{ boolean current2 = digitalRead(switchPin2);
if (last2 != current2)
{
delay(5);
current2 = digitalRead(switchPin2);
}
return current2;
}
void loop() {
lastButton = currentButton;
currentButton = debounce(lastButton);
if (lastButton == false && currentButton == true)
{
if (count == 0)
{
count++;
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
}
else if (count == 1)
{
count++;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
else if (count == 2)
{
count++;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
}
else if (count == 3)
{
count = 0;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
}
lastButton2 = currentButton2;
currentButton2 = debounce(lastButton2);
if (lastButton2 == false && currentButton2 == true)
{
if (count == 0)
{
count++;
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, LOW);
}
else if (count == 1)
{
count++;
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
}
else if (count == 2)
{
count++;
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, HIGH);
}
else if (count == 3)
{
count = 0;
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
}
}
}