Created
December 16, 2018 20:27
-
-
Save natendaben/17679b4872482820a20ce86869c2426b to your computer and use it in GitHub Desktop.
Final Code for Virtual Nature Box
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Adafruit_NeoPixel.h> | |
#ifdef __AVR__ | |
#include <avr/power.h> | |
#endif | |
//pin that the strip is connected to | |
#define PIN 5 | |
//first argument is # pixels - change to how many you haveAdafruit_NeoPixel strip = Adafruit_NeoPixel(10, PIN, NEO_GRB + NEO_KHZ800); | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(10, PIN, NEO_GRB + NEO_KHZ800); | |
int switchVal = 0; //for power switch | |
int lightVal = 0; //for light potentiometer | |
int brightness = 0; //brightness of star LEDs | |
int red, green, blue, yellow = 0; //variables for colors | |
//stuff for MP3 shield | |
#include <SPI.h> | |
#include <SdFat.h> | |
#include <SdFatUtil.h> | |
#include <SFEMP3Shield.h> | |
SdFat sd; | |
SFEMP3Shield MP3player; //start mp3player object | |
void setup() { | |
Serial.begin(9600); | |
strip.begin(); //start neopixels | |
strip.show(); | |
pinMode(A0, INPUT); //light pot pin | |
pinMode(A5, OUTPUT); //power LED | |
pinMode(A1, INPUT); //power switch | |
pinMode(5, OUTPUT); //neopixels | |
pinMode(10, OUTPUT); //star LEDs | |
//start the shield | |
sd.begin(SD_SEL, SPI_HALF_SPEED); | |
MP3player.begin(); | |
} | |
void loop() { | |
lightVal = analogRead(A0); //read light potentiometer val | |
switchVal = analogRead(A1); //read whether power switch is on | |
if(switchVal>700) //If POWER IS ON | |
{ | |
digitalWrite(A5, HIGH); //turn on power LED | |
//start playing bird track | |
MP3player.playTrack(2); | |
analogWrite(10, brightness); //write to star LEDs | |
//NEOPIXELS & TIME OF DAY | |
if(lightVal >= 0 && lightVal < 175) //midnight | |
{ | |
brightness = 20; //for star LEDs, repeated in later ones with different brightnesses | |
blue = map(lightVal, 0, 174, 10, 255); | |
yellow = map(lightVal, 0, 174, 0, 20); | |
for(int i=0; i<10; i=i+2) | |
{ | |
strip.setPixelColor(i, 0, 0, blue); | |
} | |
for(int i=1; i<10; i=i+2) | |
{ | |
strip.setPixelColor(i, yellow, yellow, 0); | |
} | |
} | |
if(lightVal >= 175 && lightVal < 256) //going to dawn | |
{ | |
brightness = 10; | |
red = map(lightVal, 175, 255, 0, 255); | |
yellow = map(lightVal, 175, 255, 20, 100); | |
for(int i=0; i<10; i=i+2) | |
{ | |
strip.setPixelColor(i, red, 0, blue); | |
} | |
for(int i=1; i<10; i=i+2) | |
{ | |
strip.setPixelColor(i, yellow, yellow, 0); | |
} | |
} | |
if(lightVal >= 256 && lightVal < 325) //going from dawn | |
{ | |
brightness = 0; | |
blue = map(lightVal, 256, 324, 255, 0); | |
yellow = map(lightVal, 256, 324, 100, 150); | |
for(int i=0; i<10; i=i+2) | |
{ | |
strip.setPixelColor(i, 255, 0, blue); | |
} | |
for(int i=1; i<10; i=i+2) | |
{ | |
strip.setPixelColor(i, yellow, yellow, 0); | |
} | |
} | |
if(lightVal >= 325 && lightVal < 512) //going to noon | |
{ | |
brightness = 0; | |
green = map(lightVal, 325, 511, 0, 255); | |
yellow = map(lightVal, 325, 511, 150, 255); | |
for(int i=0; i<10; i=i+2) | |
{ | |
strip.setPixelColor(i, 255, green, 0); | |
} | |
for(int i=1; i<10; i=i+2) | |
{ | |
strip.setPixelColor(i, yellow, yellow, 0); | |
} | |
} | |
if(lightVal >= 512 && lightVal < 700) //going from noon | |
{ | |
brightness = 0; | |
green = map(lightVal, 512, 699, 255, 0); | |
yellow = map(lightVal, 512, 699, 255, 150); | |
for(int i=0; i<10; i=i+2) | |
{ | |
strip.setPixelColor(i, 255, green, 0); | |
} | |
for(int i=1; i<10; i=i+2) | |
{ | |
strip.setPixelColor(i, yellow, yellow, 0); | |
} | |
} | |
if(lightVal >= 700 && lightVal < 768) //going to dusk | |
{ | |
brightness = 0; | |
blue = map(lightVal, 700, 767, 0, 255); | |
yellow = map(lightVal, 700, 767, 150, 100); | |
for(int i=0; i<10; i=i+2) | |
{ | |
strip.setPixelColor(i, 255, 0, blue); | |
} | |
for(int i=1; i<10; i=i+2) | |
{ | |
strip.setPixelColor(i, yellow, yellow, 0); | |
} | |
} | |
if(lightVal >= 768 && lightVal < 830) //going from dusk | |
{ | |
brightness = 10; | |
red = map(lightVal, 768, 829, 255, 0); | |
yellow = map(lightVal, 768, 829, 100, 20); | |
for(int i=0; i<10; i=i+2) | |
{ | |
strip.setPixelColor(i, red, 0, 255); | |
} | |
for(int i=1; i<10; i=i+2) | |
{ | |
strip.setPixelColor(i, yellow, yellow, 0); | |
} | |
} | |
if(lightVal >= 830 && lightVal < 1024) //going to midnight | |
{ | |
brightness = 20; | |
blue = map(lightVal, 830, 1023, 255, 10); | |
yellow = map(lightVal, 830, 1020, 20, 0); | |
for(int i=0; i<10; i=i+2) | |
{ | |
strip.setPixelColor(i, 0, 0, blue); | |
} | |
for(int i=1; i<10; i=i+2) | |
{ | |
strip.setPixelColor(i, yellow, yellow, 0); | |
} | |
} | |
strip.show(); | |
} | |
if(switchVal < 200) | |
{ | |
analogWrite(A5, 0); //turn off power LED | |
for(int i=0; i<10; i++) //turn off neopixels | |
{ | |
strip.setPixelColor(i, 0, 0, 0); | |
} | |
strip.show(); | |
MP3player.stopTrack(); //stop birds | |
digitalWrite(10, LOW); //turn off stars | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment