Hi, I've been working on a small project where I want the following things to happen:
-button1 turning on led1 and button2 turning on led2, when pressed
-If button state is "pressed" for a long period such as like 1 minute than i want to print a message (want done eventually not necessary right now)
-I want to be able to adjust code so that I can use it with n buttons and n leds
My first problem is that I've only been able to turn Led1 on with Button1 and not the other button/led pairing. I'm new to all this so can anyone tell me what the problem is and possible solution i can try?
Here is my code:
int led1 = 13; // Output 1; led1
int led2 = 12;
int button1 = 2; // Input 1; button 1
int button2 = 4;
int value1 = 0; // Reading pin status
int value2 = 0;
void setup() {
pinMode(led1, OUTPUT); // declare LED as output
pinMode(led2, OUTPUT); // declare LED as output
pinMode(button1, INPUT); // declare pushbutton as input
pinMode(button2, INPUT); // declare pushbutton as input
}
void loop(){
val1 = digitalRead(button1); // read input value
if (value1 == HIGH) { // check if the input is HIGH (button released)
digitalWrite(led1, LOW); // turn LED OFF
} else {
digitalWrite(led1, HIGH); // turn LED ON
}
val2 = digitalRead(button2); // read input value
if (value2 == HIGH) { // check if the input is HIGH (button released)
digitalWrite(led2, LOW); // turn LED OFF
} else {
digitalWrite(led2, HIGH); // turn LED ON
}
Right now that code doesn't compile. You declare and initialise variables value1 and value2, but later on you try to use them as val1 and val2. Variables val1 and val2 don't exist.
So how does it actually work at this stage, even for one button/led pair?
sorry i made some variable changes when posting my code on here and forgot to change those, the code compiles just simply forgot to change them when posting on here! Here is the code with the fixed variables:
int led1 = 13; // Output 1; led1
int led2 = 12;
int button1 = 2; // Input 1; button 1
int button2 = 4;
int value1 = 0; // Reading pin status
int value2 = 0;
void setup() {
pinMode(led1, OUTPUT); // declare LED as output
pinMode(led2, OUTPUT); // declare LED as output
pinMode(button1, INPUT); // declare pushbutton as input
pinMode(button2, INPUT); // declare pushbutton as input
}
void loop(){
value1 = digitalRead(button1); // read input value
if (value1 == HIGH) { // check if the input is HIGH (button released)
digitalWrite(led1, LOW); // turn LED OFF
} else {
digitalWrite(led1, HIGH); // turn LED ON
}
value2 = digitalRead(button2); // read input value
if (value2 == HIGH) { // check if the input is HIGH (button released)
digitalWrite(led2, LOW); // turn LED OFF
} else {
digitalWrite(led2, HIGH); // turn LED ON
}
Ok next you need to explain how the buttons are wired. Your comment that a high is released implies there's a pullup resistor and the switch is to ground, for a low when pressed. Since you have INPUT in your pinMode()'s and not INPUT_PULLUP which would enable the internal pullup, that further implies you have an external pullup resistor. Do you?
Without a pullup, either internal or external, you cannot guarantee the high-ness or low-ness of a pin.