Friday, April 28, 2017

Final Arduino Night Light Project

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.



Fritzing Picture

Link to my Fritzing: http://fritzing.org/projects/night-light-sensor

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.

Monday, March 6, 2017

My Arduino Game
     "Stop that LED!"
Description of My Game
The object of my game is to stop the light on the blue LED. To play, press down on button and watch the lights go back and forth. Release your finger off of the button when you think the LED will stop on the blue. If it lands on any other LED but the blue, your score will be subtracted 10 points. If it lands on the blue LED, your score will be added 10 more points. Keep playing to get your score up to 50. It's very hard and will take a while, but it's fun when you have nothing to do!
Example of light not on the blue LED. This means the player will have 10 points subtracted from their score.
Example of light on the blue LED. This means the player will have 10 points added to their score. 

CODE
int ledpin = 13; //starts here
int ledpin2 = 12;
int ledpin3 = 11;
int ledpin4 = 10;
int ledpin5 = 9; //variables for when button is released, stops at this pin^for all of the above
int buttonPin = 2; //see if button is pressed
int buttonState = 0; //
int counter = 0; //count button (score)
int ledpinall = (9, 10, 11, 12, 13);
int count = 0; //declared variables ^

void setup() { //runs once
  Serial.begin(9600); //set up serial library at 9600 bps
  pinMode(buttonPin, INPUT); //initialize as input

} //WHEN BUTTON IS RELEASED, TRY TO LIGHT UP BLUE LED

void loop() { //runs over and over again
    buttonState = digitalRead(buttonPin); //check to see if button is pressed here
    if (buttonState == LOW)
    {
      Serial.println("Press button, then release and try to light up the blue LED!"); //prints "Button Released" on Serial Monitor
      {
        pinMode(ledpin, INPUT); //initialize ledpin as input
        digitalWrite(ledpin, LOW); //turn led off
        }
        if (ledpin!=10) //if ledpin does not land on 10, you lose
        {
          Serial.println ("Loser!"); //print "Loser!" when you do not land on 10
        }
        if (ledpin==10) //if ledpin does land on ten, you win
        {
          Serial.println("Winner!"); //print "Winner!" when you do land on 10
        }
        if (ledpin == 13) //if led pin is 13, then stay on after button is released, same for 4 statements below
        {
          pinMode(ledpin, OUTPUT); //initialize as output
          digitalWrite(ledpin, HIGH);  //turn ledpin 13 on    
        }
        if (ledpin2 == 12) //if ledpin is 12, stay on
        {
          pinMode(ledpin, OUTPUT); //initialize as output
          digitalWrite(ledpin, HIGH);      //turn ledpin 12 on
        }
        if (ledpin3 == 11) //if ledpin is 11, stay on
        {
          pinMode(ledpin, OUTPUT); //initialize as output
          digitalWrite(ledpin, HIGH);    //turn ledpin11 on    
        }
        if (ledpin4 == 10) //if ledpin is 10, stay on after button is released
        {
          pinMode(ledpin, OUTPUT); //initialize as output
          digitalWrite(ledpin, HIGH); //turn ledpin10 on      
        }
        if (ledpin5 == 9) //if ledpin is 9, stay on after button is released
        {
          pinMode(ledpin, OUTPUT); //initialize as output
          digitalWrite(ledpin, HIGH); //turn ledpin9 on
        }
        delay(1000); //wait
    buttonState=digitalRead(buttonPin); //check to see if button is pressed here
    if (ledpin==10 && buttonState==HIGH) //if led pin is 10 and is on, then...
    {
      count=count+10; //add 10 to the score if you land on 10 (blue LED)
      Serial.print("Score="); //writes "Score=" to player's score
      Serial.println(count);
    }
    buttonState=digitalRead(buttonPin); //check to see if button is pressed here
    if (ledpin!=10 && buttonState==HIGH) //if led pin is anything but 10 and is on, then...
    {
      count=count-10; //subtract 1 from the score if you land on anything but 10
      Serial.print("Score="); //writes "score=" to player's score
      Serial.println(count);
    }
 
 
  delay(100); //delay for 100 ms
  buttonState = digitalRead(buttonPin); //check to see if button is pressed here
  if (buttonState == HIGH)
  { //starting of if statement
    Serial.println("Release button when you think your blue LED will light up!"); //prints button pressed with ending line break
    buttonState = digitalRead(buttonPin);
    while (ledpin >9 && buttonState == HIGH) //while led pin is greater than 9, and button is on, then...
   
    {
   
       buttonState = digitalRead(buttonPin);
      pinMode (ledpin, OUTPUT); //initialize as output
      digitalWrite(ledpin, HIGH); //turn led on  
      delay(100); //wait 100 ms
      digitalWrite(ledpin, LOW); //turn led off
      ledpin=ledpin-1; //subtract one from 9 to go down line
    } //end of if statement
  {
    Serial.println("Waiting"); //prints waiting to serial monitor
 
    while (ledpin <13 && buttonState == HIGH) //while ledpin is less than 13, and button is on, then...

    {
     buttonState = digitalRead(buttonPin);
     
      pinMode (ledpin, OUTPUT); //initialize as output
      digitalWrite(ledpin, HIGH); //turn led on
      delay(100); //wait 100 ms
      digitalWrite(ledpin, LOW); //turn led off
      ledpin=ledpin+1; //add to 13
    }
  }
//delay(1000);
  }
}

URL to Video
https://www.youtube.com/watch?v=sJk36eqXYGU&feature=youtu.be



Tuesday, February 28, 2017

Interesting Push Button
   My "Interesting Push Button" causes the LEDs to light up from one end to the other. As the electricity conducts through the scissors, wire cutter, and screw driver, the lights start at LED 13 and goes to LED 9. Then they start at LED 9 again and back to LED 13. Only one light is lit at one time. Once the wire is lifted off of the screw driver, the sequence stops. It starts all over again once the wire is placed back on the screw driver. Every time the LED 13 lights up, the light on the RedBoard, 13, will light up.

CODE
int ledpin = 9;
int buttonPin = 2;
int buttonState = 0;
void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);

}

void loop() {
  delay(100);
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH)
  {
    Serial.println("Button Pressed");
    while (ledpin >=9)
    {
      pinMode (ledpin, OUTPUT);
      digitalWrite(ledpin, HIGH);
      delay(100);
      digitalWrite(ledpin, LOW);
      ledpin=ledpin-1;
    }
  {
    Serial.println("Waiting");
    while (ledpin <= 13)
    {
     pinMode (ledpin, OUTPUT);
     digitalWrite(ledpin, HIGH);
     delay(100);
      digitalWrite(ledpin, LOW);
      ledpin=ledpin+1;
    }
  }
  ledpin = 9;
  ledpin = 13;
  }
}

URL to Video
https://www.youtube.com/watch?v=3bdYIU7NU1o&feature=youtu.be

Tuesday, January 17, 2017

Arduino Projects



     The Word Clock
     The first Arduino Project I thought was extremely cool was "The Word Clock". One day, a husband wanted to give his wife a beautiful gift. Knowing she was an English teacher, he thought it would be unique to make her a word clock. This clock is all words instead of numbers, and the lights go to the correct words that correlate with the time. I think this clock is extremely cool because it has the power to tell the time in many different ways. For example, instead of saying 9:35, the clock says, "It's twenty five minutes to ten." I can't imagine how he got this to work, but it's one of the coolest things I have ever seen.

Link: http://www.instructables.com/id/The-Word-Clock-Arduino-version/

Addressable Milk Bottles (LED Lighting + Arduino)
     The second Arduino project I found interesting was the addressable milk bottles. Used PPE milk bottles can be used as LED lights and are environmentally friendly. This helps with reusing milk bottles and uses less than 3 watts, but still bright enough to see by. I think this is a cool project because along with helping the environment, it's also very aesthetically pleasing. The light glow it gives is very calming and peaceful. It's a beautiful and useful way to reuse a simple product like a milk bottle.

Link: http://www.instructables.com/id/Milk-Bottle-LED-Lights-Arduino-Controlled/