Nicole Kruszka
Electro-Blue Night Light Sensor
Description of My Project
My project is a night light sensor. Once you plug in the Arduino, the ringed-shaped light will automatically light up. Once you put the night light in the dark, the rest of the LED lights will light up, creating a brighter atmosphere for a blue night light. If the project is in the light, the night light will not be turned all the way on. If you do not have a laptop or computer to plug the Arduino into, plug it into any electrical outlet; it comes with a plug. So basically, it's a two-in-one project: you can use it as a day light and a nightlight.
Detailed Description of How the Program Works
To set up my NeoPixel, I used the code from Chapter 8 from the Electronics Class Website. This code turned the NeoPixel all the way on. I changed it back and forth from turning on one pixel at a time, to turning all the way on all at once. The NeoPixel color was at first green, but by changing the code from (0, 255, 0=green) to (0, 0, 255), the color of the ring changed blue. First I had to download the new Arduino Program to access the NeoPixel code. For the code, I included Adafruit_NeoPixel, the official coding name for getting the NeoPixel to work. The NeoPixel wire is attached to ~6 (PWM) to work. Next, the Adafruit_NeoPixel had to understand how many pixels and which pin to send the signals to. I then delayed the value of changing the LEDs for half of a second. To get the 6 LEDs to light up by using a photo resistor, I had to create a variable called sensorPin, which is attached to A0. This senses light or darkness and turns the LEDs on or off. I added 6 LEDs and gave them variables to light them up (and made sure to add a resistor to each of them as well). I attached the LEDs to 12, 13, 8, 7, 2 and 4. The sensorValue variable makes sure when it is dark to turn on the LEDs. If it is less than 40, the LEDs will be turned on and vice versa.
In the void setup, there is a code that ends the special code for the NeoPixels. Then I began my Serial monitor for the LEDs. It is at 9600 baud and all LEDs are initialized as an output. Once darkness is above the photo resistor, the numbers on the Serial monitor go from the 100s to the 50s. Also included in the void setup, is the pixels.begin function, which initializes the NeoPixel library.
In the void loop, I created a function to turn the NeoPixels on and make sure they stay on. Next, I created a variable for the photo resistor to read the value, meaning when I touch or the room is dark over the photo resistor, it will read the value of darkness and turn the LED on. The Serial monitor then prints the value to monitor. Then, I repeat this same code 6 times to correspond to the 6 LEDs: if the value is less than the sensorValue, then turn the LED on. But if the value is greater than the sensorValue, it will turn the LED off. Lastly, I included my void neoPixelsOn() function to make sure the NeoPixel stayed on. The for statement shows that if the first NeoPixel is 0, the second is 1, and all the way up to the count of pixels minus 1. This is for when I originally had the NeoPixel turning on one pixel at a time. For will keep repeating what ever is in the brackets as long as the condition is true. Under the for statement, I set the pixel color to blue and then used pixels.show() to send the updated pixel color to the hardware.
Pictures
The light with my finger over it. |
The sensor in adequate lighting. |
The sensor in complete darkness. |
The sensor in full on light. |
I added a wooden box to the project to keep the Arduino wiring more secure and taped the breadboard to the bottom. |
Code
#include <Adafruit_NeoPixel.h> //include the NeoPixel library (new one 2)#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 6 //neopixel is attached to pin 6
#define NUMPIXELS 16 //16 neopixels are attached to arduino
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); //how many pixels
//and which pin to use to send signals to
int delayval = 500; // delay for half a second
int sensorPin = A0; //photoresistor pin is at A0
int ledPin = 12; //led pin at 12
int ledPin2 = 13; //led pin at 13
int ledPin3 = 8; //led pin at 8
int ledPin4 = 7; //led pin at 7
int ledPin5 = 2; //led pin at 2
int ledPin6= 4; //led pin at 4
int sensorValue = 40; //sensor value at 40
void setup() {
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// end of trinket special code
Serial.begin(9600); //serial monitor at 9600 baud
pinMode(ledPin, OUTPUT); //initialize pin as output
Serial.begin(9600); //serial monitor
pinMode(ledPin2, OUTPUT); //initialize pin at output
Serial.begin(9600); //serial monitor
pinMode(ledPin3, OUTPUT); //initialize pin at output
Serial.begin(9600); //serial monitor
pinMode(ledPin4, OUTPUT); //initialize pin at output
Serial.begin(9600); //serial monitor
pinMode(ledPin5, OUTPUT); //initialize pin at output
Serial.begin(9600); //serial monitor
pinMode(ledPin6, OUTPUT); //initialize pin at output
pixels.begin(); // initializes the NeoPixel library.
}
void loop() {
neoPixelsOn(); //turn neoPixel on
delay(1000); //delay for 1000 ms
// neoPixelsOff(); //turn neoPixel off
//delay(1000); //delay for 1000 ms
int val = analogRead(sensorPin); //read the value at the sensorpin(photoresistor)
Serial.println(val); //print to serial monitor
if (val < sensorValue) //if value is less than sensor value
digitalWrite(ledPin, HIGH); //turn pin on
else
digitalWrite(ledPin, LOW); //turn pin off
if (val < sensorValue) //if value is less than sensor value
digitalWrite(ledPin2, HIGH); //turn pin on
else
digitalWrite(ledPin2, LOW); //turn pin off
if (val < sensorValue) //if value is less than sensor value
digitalWrite(ledPin3, HIGH); //turn pin on
else
digitalWrite(ledPin3, LOW); //turn pin off
if (val < sensorValue) //if value is less than sensor value
digitalWrite(ledPin4, HIGH); //turn pin on
else
digitalWrite(ledPin4, LOW); //turn pin off
if (val < sensorValue) //if value is less than sensor value
digitalWrite(ledPin5, HIGH); //turn pin on
else
digitalWrite(ledPin5, LOW); //turn pin off
if (val < sensorValue) //if value is less than sensor value
digitalWrite(ledPin6, HIGH); //turn pin on
else
digitalWrite(ledPin6, LOW); //turn pin off
}
void neoPixelsOn() //turn NeoPixel on
{
for(int i=0;i<NUMPIXELS;i++){ //first neopixel is 0, second is 1, all the way up to the count of pixels minus 1
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(0,0,255)); // neopixel lights up blue
pixels.show(); // sends the updated pixel color to the hardware.
}
}
//void neoPixelsOff()
//{
//for(int i=0;i<NUMPIXELS;i++){ //first neopixel is 0, second is 1, all the way up to the count of pixels minus 1
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
//pixels.setPixelColor(i, pixels.Color(0,0,0)); // neopixel lights up blue
//pixels.show(); // sends the updated pixel color to the hardware.
//}
//}
Problems I Encountered and How I Overcame Them
I encountered many problems with this project. Knowing I would have problems, I started very early. At first, I could not figure out how the code for the light sensor worked. It kept flickering on and off and wouldn't stay on the whole time. I looked back at 6.4 and figured it out by watching the video again. I originally had a delay set for the LEDs and didn't turn them all the way on and off; they were only fading LEDs. I needed them either all on or all off. I had to change the value numbers and have an else statement to create an "if then statement". Another problem I encountered while working on this project was figuring out how to work the NeoPixel along with the LEDs. The NeoPixel was no big deal for me to figure out, but when I added my code from the photo resistor, both of them would not work at the same time. To include the NeoPixel with the photo resistor, I had to add a new function under void loop, called void neoPixelson. I overcame this challenge by asking Mr. Dickie for help and then added both codes together. It finally started working how I wanted it to. I originally had a neoPixelsoff function as well, but I did not want to have the light blinking on and off, so I just added it to my comments in case I would like to change it someday. Even though this project may look very easy to the average Electronics/Arduino person, it was difficult for me because this was my very first class learning about coding and even knowing what an Arduino was. Figuring out how to make the light do what I wanted it to do was very frustrating and time consuming, but overall I am very pleased with my work.
What I Would Like to do if I had more Time/Money
If I had more money, I would have liked to buy a more expensive bulb to put on top of the lights. I was looking at really cool glass light covers with cool designs and I would have liked to buy them. If I had more time, I probably would have tried to figure out how to incorporate the NeoPixel with the photo resistor. Maybe I would have figured out how to turn the NeoPixel on when darkness was over the photo resistor as well as the LEDs.
No comments:
Post a Comment