-
Notifications
You must be signed in to change notification settings - Fork 0
/
MotionRGBLED.c
133 lines (102 loc) · 2.99 KB
/
MotionRGBLED.c
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
\ Name: MotionRGBLED.c
/ Author: Kasey Eljoundi
\
/ Description: Activate LED lights using a motion sensor.
\
/ Requirements:
\ Arduino board, PIR sensor, LEDs
/ Minimum 9v power supply recommended
\ ControlLED library by Kasey Eljoundi
*/
#include <ControlLED.h>
int inputPin = 13; // input pin (for PIR sensor)
int inputVal = 0; // variable for reading the pin status
int pirState = LOW;
int calibrationTime = 20000; //20 sec calibration delay
bool motionState = false;
ControlLED lightsR = ControlLED(3); /* LED lights to activate */
ControlLED lightsG = ControlLED(5); /* LED lights to activate */
ControlLED lightsB = ControlLED(6); /* LED lights to activate */
ControlLED statusLED = ControlLED(17); /* status LED - pro mini RX/TX LED */
unsigned long currentMillis = 0; //variable to hold current machine uptime (milliseconds)
unsigned long lightsTurnOnTime = 0;
unsigned int lightsInterval = 30000; //30 seconds
unsigned long statusLedOn = 0;
unsigned int statusLedInterval = 2000; //2 seconds
void setup() {
pinMode(inputPin, INPUT); // declare pir sensor as input
//delay after start for sensor "calibration"
delayForCalibration();
}
void loop(){
inputVal = digitalRead(inputPin); // read input value from motion sensor
currentMillis = millis();
//blink status led
blinkStatusLED();
if (inputVal == HIGH) {
/* show motion detection by LED */
if (pirState == LOW) {
motionState = true;
statusLED.on();
pirState = HIGH; // We only want to print on the output change, not state
}
//turn on lights
lightsR.linearFadeIn(2000);
lightsG.linearFadeIn(2000);
lightsB.linearFadeIn(2000);
lightsTurnOnTime = currentMillis; //restart led interval timer
} else{
//turn LED OFF **if interval is up**
if( currentMillis - lightsTurnOnTime > lightsInterval ){
lightsR.linearFadeOut(2000);
lightsG.linearFadeOut(2000);
lightsB.linearFadeOut(2000);
}
/* show motion detection by LED */
if (pirState == HIGH){
motionState = false;
statusLED.off();
pirState = LOW; // We only want to print on the output change, not state
}
}
}
/*
\ Name: void blinkStatusLED()
/
\ Description: blink statusLED on even interval using background time comparisons,
/ blinking pauses if sensor is currently detecting motion
\
*/
void blinkStatusLED(){
if( !motionState ){
if( currentMillis - statusLedOn > statusLedInterval ){
statusLedOn = currentMillis;
statusLED.toggle();
}
}
}
/*
\ Name: void delayForCalibration()
/
\ Description: pause execution of program for sensor calibration,
/ manufacturer recommends "rest" after powering on sensor (10-60s)
\
*/
void delayForCalibration(){
statusLED.on();
delay(calibrationTime);
statusLED.toggle();
delay(200);
statusLED.toggle();
delay(200);
statusLED.toggle();
delay(200);
statusLED.toggle();
delay(200);
statusLED.toggle();
delay(200);
statusLED.toggle();
delay(200);
statusLED.off();
}