int LEDRED = 11;
int LEDGREEN = 10;
int SENSOR_1 = 4;
int SENSOR_2 = 3;
int state_1 = LOW;
int state_2 = LOW;
void setup() {
// put your setup code here, to run once:
pinMode(LEDRED, OUTPUT);
pinMode(LEDGREEN, OUTPUT);
pinMode(SENSOR_1, INPUT);
pinMode(SENSOR_2, INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int S1 = digitalRead(SENSOR_1);
int S2 = digitalRead(SENSOR_2);
if(S1 == HIGH){
if(state_1 == LOW){
digitalWrite(LEDRED, HIGH);
Serial.println("Motion detected SENSOR_1 && LED RED HIGH !");
state_1 = HIGH;
}
}
if(S1 == LOW){
if(state_1 == HIGH){
digitalWrite(LEDRED, LOW);
Serial.println("NO Motion detected SENSOR_1 && LED RED LOW !");
state_1 = LOW;
}
}
if(S2 == LOW){
if(state_2 == HIGH){
digitalWrite(LEDGREEN, LOW);
Serial.println("NO Motion detected SENSOR_2 && LED GREEN LOW !");
state_2 = LOW;
}
}
if(S2 == HIGH){
if(state_2 == LOW){
digitalWrite(LEDGREEN, HIGH);
Serial.println("Motion detected SENSOR_2 && LED GREEN HIGH !");
state_2 = HIGH;
}
}
}
/*void loop() {
checkSensor(SENSOR_1, state_1, LEDRED, "Motion detected SENSOR_1 && LED RED HIGH !", "NO Motion detected SENSOR_1 && LED RED LOW !");
checkSensor(SENSOR_2, state_2, LEDGREEN, "Motion detected SENSOR_2 && LED GREEN HIGH !", "NO Motion detected SENSOR_2 && LED GREEN LOW !");
}
void checkSensor(int sensorPin, int &state, int ledPin, const char* motionMsg, const char* noMotionMsg) {
int sensorState = digitalRead(sensorPin);
if (sensorState == HIGH) {
if (state == LOW) {
digitalWrite(ledPin, HIGH);
Serial.println(motionMsg);
state = HIGH;
}
} else {
if (state == HIGH) {
digitalWrite(ledPin, LOW);
Serial.println(noMotionMsg);
state = LOW;
}
}
}*/