Hello and thank you for taking the time to have a look at my problem.
I am new to Arduino and have a project which I am using to slowly learn coding. Unfortunately my progress has stalled as I have hit a snag. Two to be precise.
The first snag :
I am using PIR Sensors to trigger relays. Simple stuff I know.
I have 2 (two) PIR sensors connected to a uno wifi using Digi 6 (Sensor 1) & Digi 7 (Sensor 2). For some reason, once uploaded and running, it ignores Sensor 1 but always triggers on Sensor 2. I have swapped PIR to ensure it's not hardware related, and I have also removed Digi 7 from my code and have been able to trigger the program using Digi 6. However, when I have both as Inputs, it ignores 6 and only triggers on 7.
The second Snag:
I have a smoke machine on Digi 9 (Smoke), and I would like to have it turn on and off during the program. At the moment I can only get it to run at the start and then it stays on for the duration. I have tried to find some other examples, however when I implement the changes, my code seems to break. Ideally, I would like it to run as follows.
@ start - Smoke for 6 seconds then stop
@ 4 seconds - Turn on Laser & LED and run for 1min 02 Seconds
@ 9 seconds - Turn on Reaper & Fence and run for 58 Seconds
@ 10 seconds - Turn on smoke for 4 seconds then stop
@ 25 seconds - Turn on smoke for 4 seconds then stop
@ 40 seconds - Turn on smoke for 4 seconds then stop
@ 60 seconds - Turn on smoke for 4 seconds then stop
My code is below. Any help is appreciated
int Smoke = 9;
int Laser = 10;
int LED = 11;
int Reaper = 12;
int Fence = 13;
int sensor1 = 6; // the pin that the sensor1 is atteched to
int sensor2 = 7; // the pin that the sensor2 is atteched to
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)
void setup() {
pinMode(Smoke, OUTPUT); // initalize Smoke as an output
pinMode(Laser, OUTPUT); // initalize Laser as an output
pinMode(LED, OUTPUT); // initalize Smoke as an output
pinMode(Reaper, OUTPUT); // initalize Laser as an output
pinMode(Fence, OUTPUT); // initalize Smoke as an output
pinMode(sensor1, INPUT); // initialize sensor as an input
pinMode(sensor2, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
}
void loop(){
val = digitalRead(sensor1); // read sensor value
val = digitalRead(sensor2); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(Smoke, HIGH); // turn LED ON
delay(4000); // delay 4000 milliseconds
digitalWrite(Laser, HIGH); // turn LED ON
digitalWrite(LED, HIGH); // turn LED ON
delay(5000); // delay 5000 milliseconds
digitalWrite(Reaper, HIGH); // turn LED ON
digitalWrite(Fence, HIGH); // turn LED ON
delay(58000); // delay 58000 milliseconds
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(Smoke, LOW); // turn LED OFF
digitalWrite(Laser, LOW); // turn LED OFF
digitalWrite(LED, LOW); // turn LED OFF
digitalWrite(Reaper, LOW); // turn LED OFF
digitalWrite(Fence, LOW); // turn LED OFF
delay(62000); // delay 62000 milliseconds
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
}