-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutomatedPantryLight2.ino
79 lines (67 loc) · 2.77 KB
/
AutomatedPantryLight2.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Ultrasonic Sensor HC-SR04 and 2 LED Strip that utlizes NeoPixel library
by Daleon L
www.twitter.com/iamDaleon
www.github.com/iamDaleon
*/
#include <Adafruit_NeoPixel.h>
// #ifdef __AVR__
// #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
// #endif
// Which pin on the Arduino is connected to the NeoPixels?
#define NUMPIXELS = 60, STRIP1 = 11, STRIP2 = 12 // Strip light & NeoPixels defined
// defines pins numbers for the Ultrasonic Sensor HC-SR04
const int trigPin = 2, echoPin = 4;
// When setting up the NeoPixel library, we tell it how many pixels,
// and which pin to use to send signals. Note that for older NeoPixel
// strips you might need to change the third parameter -- see the
// strandtest example for more information on possible values.
Adafruit_NeoPixel pixels1(NUMPIXELS, STRIP1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixels2(NUMPIXELS, STRIP2, NEO_GRB + NEO_KHZ800);
#define DELAYVAL 20000 // Time (in milliseconds) to pause between pixels
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
pixels1.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
pixels2.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
if(distance >= 8){
pixels1.clear(); // Set all pixel colors to 'off'
pixels2.clear(); // Set all pixel colors to 'off'
// The first NeoPixel in a strand is #0, second is 1, all the way up
// to the count of pixels minus one.
for(int i=0; i<NUMPIXELS; i++) { // For each pixel...
// pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
// Here we're using a moderately bright bluish green color:
pixels1.setPixelColor(i, pixels1.Color(240, 237, 225));
pixels2.setPixelColor(i, pixels2.Color(240, 237, 225));
pixels1.show(); // Send the updated pixel colors to the hardware.
pixels2.show(); // Send the updated pixel colors to the hardware.
}
delay(DELAYVAL); // Pause before next pass through loop
}else if(distance <= 7){
pixels1.show();
pixels2.show();
pixels1.clear(); // Set all pixel colors to 'off'
pixels2.clear(); // Set all pixel colors to 'off'
}
}