Hi!
I am trying to make a circuit with 3 push buttons connected to pin 14, 15 and 16
I basically copied the code and circuit from Jeremy Blum's Arduino tutorial 2
This code controls 1 pushbutton and resolves debouncing issues. (around 8.00 till 15.20min in the video)
To control 3 buttons I basically copied the code 3 times. But it does not work!
The circuit should be fine. I used external pulldown resistors in the circuit.
I want pin 0 to give 5V if I press the button connected to pin 14,
pin 1 give 5V if I press button connected to 15
pin 2 give 5V if I press button connected to 16
I am really stuck here! I hope someone can/wants to help me!
Why don't you look closely at the errors you get with the code you have now? You are trying to redefine variables from int to boolean. A variable can only have one type. If you want different types you need different names (e.g. switch01_pin).
You can keep them to yourself, or you can share them. Which do you suppose will produce the most help?
if (last != current);
If last is not equal to current, do nothing. Otherwise, do nothing. Well, OK.
wire01_ON = !wire01_On;
Case matters!
if (lastButton == LOW && currentButton == HIGH);
You probably do not want that ; on the end. With it there, it IS the body of the if statement, and is all that will be executed if the statement is true. By itself, a ; is a do-nothing statement.
You are debouncing one switch, then toggling all three LEDs. Is that really what you want?
I will try to explain what I want:
if I press the button connected to pin 14, I want pin 0 to give 5V,
if I press the button connected to pin 15, I want pin 1 give 5V and
if I press the button connected to pin 16, I want pin 2 give 5V.
The lights have to stay on until the button is pressed again.
I thought I could resolve the debounce issue in 1 statement. Is that not possible? I did have a feeling that I needed to change something in the variables "boolean lastButton = LOW;" and "boolean currentButton = LOW;" But I can't figure out what...
This is the adjusted code without errors
// INPUTS
int switch01_Pin = 14;
int switch02_Pin = 15;
int switch03_Pin = 16;
// OUTPUTS
int wire01_Pin = 0;
int wire02_Pin = 1;
int wire03_Pin = 2;
// VARIABLES
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean wire01_On = false;
boolean wire02_On = false;
boolean wire03_On = false;
// VOID SETUP
void setup ()
{
pinMode(switch01_Pin, INPUT);
pinMode(switch02_Pin, INPUT);
pinMode(switch03_Pin, INPUT);
pinMode(wire01_Pin, OUTPUT);
pinMode(wire02_Pin, OUTPUT);
pinMode(wire03_Pin, OUTPUT);
}
boolean debounce(boolean last)
{
boolean current = digitalRead(switch01_Pin);
if (last != current)
boolean current = digitalRead(switch02_Pin);
if (last != current)
boolean current = digitalRead(switch03_Pin);
if (last != current)
{
delay(5);
current = digitalRead(switch01_Pin);
delay(5);
current = digitalRead(switch02_Pin);
delay(5);
current = digitalRead(switch03_Pin);
}
return current;
}
// VOID LOOP
void loop ()
{
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
{
wire01_On = !wire01_On;
wire02_On = !wire02_On;
wire03_On = !wire03_On;
}
lastButton = currentButton;
digitalWrite(wire01_Pin, wire01_On);
digitalWrite(wire02_Pin, wire02_On);
digitalWrite(wire03_Pin, wire03_On);
}
I thought I could resolve the debounce issue in 1 statement.
You can, for one switch. You can't debounce three switches in one function call. At least, not easily. You need different last states for each switch, for one thing.
The debounce() function should take a pin number as input, too, so you don't need 3 functions.