-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotion_activated_fogger_controller.ino
59 lines (52 loc) · 2.25 KB
/
motion_activated_fogger_controller.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Handy defines for readability
#define TURN_LED_ON HIGH
#define TURN_LED_OFF LOW
#define TURN_FOGGER_ON LOW
#define TURN_FOGGER_OFF HIGH
#define MOTION_DETECTED HIGH
#define NO_MOTION_DETECTED LOW
#define SAMPLE_DELAY_MS 10
int LED_OUTPUT_PIN = 13; // specify output pin for LED, for debugging purposes while fogger not attached
int FOGGER_RELAY_OUTPUT_PIN = 3; // output pin for relay to turn fogger on
int IR_SENSOR_INPUT_PIN = 7; // input pin for motion sensor
int previousSensorState = NO_MOTION_DETECTED; // start with no motion detected
int currentSensorValue = 0; // variable for reading IR sensor status
int FOGGER_ON_TIME_MS = 30000; // 30 seconds "on" time
int FOGGER_REPLENISH_TIME_MS = 60000; // timeout while motion won't be acted on
void setup()
{
pinMode(LED_OUTPUT_PIN, OUTPUT);
pinMode(FOGGER_RELAY_OUTPUT_PIN, OUTPUT);
pinMode(IR_SENSOR_INPUT_PIN, INPUT);
Serial.begin(9600);
}
// Loop to detect and act on motion. If motion is detected, turn on fogger for FOGGER_ON_TIME.
// Then, force a delay of FOGGER_REPLENISH_TIME to ensure fogger will be ready before motion is acted upon.
void loop()
{
currentSensorValue = digitalRead(IR_SENSOR_INPUT_PIN);
if (currentSensorValue == MOTION_DETECTED) {
digitalWrite(LED_OUTPUT_PIN, TURN_LED_ON);
digitalWrite(FOGGER_RELAY_OUTPUT_PIN, TURN_FOGGER_ON);
if (previousSensorState == NO_MOTION_DETECTED)
{
previousSensorState = MOTION_DETECTED;
Serial.println("Turning on fogger");
delay(FOGGER_ON_TIME_MS);
Serial.println("Turning off fogger");
}
delay(SAMPLE_DELAY_MS);
}
else
{
digitalWrite(LED_OUTPUT_PIN, TURN_LED_OFF);
digitalWrite(FOGGER_RELAY_OUTPUT_PIN, TURN_FOGGER_OFF);
if (previousSensorState == MOTION_DETECTED)
{
previousSensorState = NO_MOTION_DETECTED;
Serial.println("Wait for fogger to replenish");
delay(FOGGER_REPLENISH_TIME_MS);
Serial.println("Fogger is ready");
}
}
}