While I assume this may be, but a simple oversight in my attempt at coding this(I'll plead ignorance here); I've scoured through this forum & on the web and have found little guidance as to a solution -- for the shear fact I don't entirely know what I'm looking for. Attached is a video that might better illustrate my problem.
At first I imagined it might be some form of a hardware issue on my part. However, the first of my four buttons works just fine, it sets the relay pin to HIGH when pressed and LOW when released.(I currently have the NC/NO contacts reversed so it isn't constantly buzzing) When the other buttons are pressed, the relays switch between HIGH/LOW very quickly - each other button does this at its own rate. It sounds like a broken keyboard.
After a bit of digging, the term 'debouncing' kept coming up to these sort of button related issues. Now.. I've read over explantations of this a hand full of times and can mostly grasp what is happening with the physical button, but all of the 'debouncing' code solutions I've found fail to include an explanation I can understand on how to implement it, in this type of project.
I apologize if this is yet another post to something that has already been covered, but I'm not certain what the issue even is at this point.
const int buttonPin_1 = 1; // the number of the pushbutton pin
const int buttonPin_2 = 4;
const int buttonPin_3 = 7;
const int buttonPin_4 = 10;
const int Light_1 = 2; // Relay 2 pin
const int Light_2 = 5; // Relay 4 pin
const int Light_3 = 8; // Relay 5 pin
const int Light_4 = 11; // Relay 7 pin
// variables will change:
boolean buttonState = LOW; // variable for reading the pushbutton status
boolean buttonState2 = LOW; //variable for reading the second buttons state
boolean buttonState3 = LOW;
boolean buttonState4 = LOW;
void setup() {
// initialize the LED pin as an output:
pinMode(Light_1, OUTPUT);
pinMode(Light_2, OUTPUT);
pinMode(Light_3, OUTPUT);
pinMode(Light_4, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin_1, INPUT);
pinMode(buttonPin_2, INPUT);
pinMode(buttonPin_3, INPUT);
pinMode(buttonPin_4, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin_1);
buttonState2 = digitalRead(buttonPin_2);
buttonState3 = digitalRead(buttonPin_3);
buttonState4 = digitalRead(buttonPin_4);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
// Light 1
if (buttonState == HIGH) {
// turn relay on:
digitalWrite(Light_1, HIGH);
}
else {
// turn relay off:
digitalWrite(Light_1, LOW);
}
// Light 2
if (buttonState2 == HIGH) {
// turn relay 2 on:
digitalWrite(Light_2, HIGH);
}
else {
// turn relay off:
digitalWrite(Light_2, LOW);
// Light 3
if (buttonState3 == HIGH) {
// turn relay 3 on:
digitalWrite(Light_3, HIGH);
}
else {
// turn turn off:
digitalWrite(Light_3, LOW);
}
// Light 4
if (buttonState4 == HIGH) {
// turn relay 4 on:
digitalWrite(Light_4, HIGH);
}
else {
// turn relay off:
digitalWrite(Light_4, LOW);
}
}
}
Demonstration*