Hello i would like to ask for help
i am doing project that relay is controlled by movements sensor its not problem is running well.
but also i want make button by able handle same relay which is controlled by movements sensor. but in this case when i pressed button it just blinking and not do what i wanted.
i want when i press button the relay will go on when i press button again rely will go to normal state which is that is controlled with movements sensor.
here is my code please help me
#include <Button.h>
const int pirPin = A1;
const int pirPin2 = A3;
const int reley = A2;
const int reley2 = A0;
Button button1 = Button(A4, PULLUP);
Button button2 = Button(A5, PULLUP);
Button button3 = Button(A6, PULLUP);
Button button4 = Button(A7, PULLUP);
void setup() {
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(pirPin2, INPUT);
pinMode(reley, OUTPUT);
pinMode(reley2, OUTPUT);
}
void loop() {
int pirVal = digitalRead(pirPin);
if (pirVal == HIGH) { //was motion detected
digitalWrite(reley, LOW); // Turns ON Relays 1
Serial.println("Light ON");
for (int x = 0; x < 1000; x++) { // Wait for 1 second
delay(0);
}
} else {
digitalWrite(reley, HIGH); // Turns Relay Off
Serial.println("Light OFF");
for (int x = 0; x < 1000; x++) { // Wait for 1 second
delay(0);
}
}
int pirVal2 = digitalRead(pirPin2);
if (pirVal2 == HIGH) { //was motion detected
digitalWrite(reley2, LOW); // Turns ON Relays 1
Serial.println("Light2 ON");
for (int x = 0; x < 1000; x++) { // Wait for 1 second
delay(0);
}
} else {
digitalWrite(reley2, HIGH); // Turns Relay Off
Serial.println("Light2 OFF");
for (int x = 0; x < 1000; x++) { // Wait for 1 second
delay(0);
}
}
if (button1.isPressed()) {
digitalWrite(A2, LOW);
for (int x = 0; x < 5000; x++) {
// Wait for 1 second
delay(0);
}
}
if (button2.isPressed()) {
digitalWrite(A0, LOW);
for (int x = 0; x < 5000; x++) {
// Wait for 1 second
delay(0);
}
}
}
Thanks for help
moderator: added code tags and used CTRL-T to auto-indent the code