i did a security system with arduino mega and there was a problem writing the code, the led light last turned on with the monitor, mega restart or arduino
int lithLed = 8; int redLed = 7; int yellowLed = 6; int greenLed = 5;
int sensor = 22; int sensor1 = 24; int sensor2 = 26; int sensor3 = 28;
int state = LOW; int state1 = LOW; int state2 = LOW; int state3 = LOW;
int val = 0; int val1 = 0; int val2 = 0; int val3 = 0;
void setup() {
Serial.begin(9600);
pinMode(redLed, OUTPUT); pinMode(yellowLed, OUTPUT); pinMode(greenLed, OUTPUT); pinMode(lithLed, OUTPUT);
pinMode(sensor, INPUT); pinMode(sensor1, INPUT); pinMode(sensor2, INPUT); pinMode(sensor3, INPUT);
}
void loop() {
val = digitalRead(sensor);
if (val == HIGH) {
digitalWrite(redLed, HIGH);
delay(50);
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH;
}
}
else {
digitalWrite(redLed, LOW);
delay(100);
if (state == HIGH) {
Serial.println("Motion stopped!");
state = LOW;
}
}
val1 = digitalRead(sensor1);
if (val1 == HIGH) {
digitalWrite(yellowLed, HIGH);
delay(50);
if (state1 == LOW) {
Serial.println("Motion detected! 1");
state1 = HIGH;
}
else {
digitalWrite(yellowLed, LOW);
delay(100);
if (state1 == HIGH) {
Serial.println("Motion stopped!");
state1 = LOW;
}
}
val2 = digitalRead(sensor2);
if (val2 == HIGH) {
digitalWrite(greenLed, HIGH);
delay(50);
if (state2 == LOW) {
Serial.println("Motion detected! 2");
state2 = HIGH;
}
}
else {
digitalWrite(greenLed, LOW);
delay(100);
if (state2 == HIGH) {
Serial.println("Motion stopped!");
state2 = LOW;
}
}
val3 = digitalRead(sensor3);
if (val3 == HIGH) {
digitalWrite(lithLed, HIGH);
delay(50);
if (state3 == LOW) {
Serial.println("Motion detected! 3");
state3 = HIGH;
}
}
else {
digitalWrite(lithLed, LOW);
delay(100);
if (state3 == HIGH) {
Serial.println("Motion stopped!");
state3 = LOW;
}
}
}
i add breadboard.