Components and supplies
NeoPixel strip
LED strip connector
Arduino UNO
Breadboard (generic)
Jumper wires (generic)
Speaker: 0.25W, 8 ohms
Tools and machines
Hot glue gun (generic)
Apps and platforms
Arduino Web Editor
Project description
Code
Eyes + Candle
arduino
This is the sketch to control the LEDs used in the eyes of pumpkin as well as the candle.
1#include <Adafruit_NeoPixel.h> 2 3//initialize the candle, and variables related to the candle 4//modified from: https://codebender.cc/sketch:67754#Neo%20Candle.ino 5#define lPIN 6 //this is the pin the candle LEDs are connected to 6int numpix = 4; //set the number of Pixels you have/want in the candle - more = brighter 7int randNumber; int color; int wait; int randpix; boolean x = true; unsigned long prevCandleTime; //additional variables used to operate candle 8Adafruit_NeoPixel cstrip = Adafruit_NeoPixel(numpix, lPIN, NEO_GRB + NEO_KHZ800); //define the candle led strip 9 10//initialize the eyes, and variables used to control eyes 11// modified from: http://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/#fadeinout_rgb 12#define fadePin 8 //this is the pin the glowing eye LEDs are connected to 13#define NUM_LEDS 4 //set the number of Pixels you have/want in the eyes - more = brighter 14int fadeWait = 3; // sets speed of eye fade. smaller = faster, bigger = slower 15unsigned long currentFadeTime = 0; int j = 0; int k = 0; boolean fadeIn = true; 16Adafruit_NeoPixel fadeStrip = Adafruit_NeoPixel(NUM_LEDS, fadePin, NEO_GRB + NEO_KHZ800); //define the led strip for the eyes 17 18void setup() { 19 pinMode(2, INPUT); //use pin 2 as input for the random seed - pick an unused analog read pin 20 randomSeed(analogRead(2)); //Set random number generator seed from analog pin 2 21 22 cstrip.begin(); 23 cstrip.show(); // Initialize all candle pixels to 'off' 24 25 fadeStrip.begin(); 26 fadeStrip.show(); // Initialize all eye pixels to 'off' 27} 28 29void loop(){ 30 candle(); 31 RGBLoop(); 32} //end main loop 33 34void candle(){ 35 if(x){ 36 wait = random(80, 120); //set a random wait period 37 randpix = random(0, numpix + 1); //choose a random number of pixels 38 color = random(0, 2); //Pick either yellow or orange 39 for (int i = 0; i < numpix / 2; i++){ //so it leaves a certain number of yellow pixels on (number of pixels/3) 40 cstrip.setPixelColor(i, 255, 120, 0); //set the number of pixels to turn on and color value (yellowish) 41 } 42 cstrip.show(); //turn pixels on 43 } 44 45 if (color == 0){ //if red was chosen 46 flickred(cstrip.Color(115, 50, 0), wait, randpix); // call flickred and pass it the red (orangeish) color values - change values to change color 47 } else { // otherwise use yellow 48 flickYellow(cstrip.Color(180, 80, 0), wait, randpix); // call flickYellow and pass it the yellow color values (change values to change color), and wait time and random pixel count 49 } 50} 51 52// Function for when red is chosen 53void flickred(uint32_t c, uint8_t wait, uint8_t p) { 54 if(x){ 55 for (int i = 0; i < p - 2; i++){ //loop for given random pixel count (passed from loop) 56 cstrip.setPixelColor(i, c); 57 } 58 cstrip.show(); //turn pixels on 59 x = false; 60 }else{ 61 if(millis()-prevCandleTime>wait){ 62 prevCandleTime = millis(); 63 for (int i = 0; i < p; i++) { 64 cstrip.setPixelColor(i, 0); //turn pixel off 65 } 66 x = true; 67 } 68 } 69} //end flickred 70 71// function for when yellow is chosen 72void flickYellow(uint32_t c, uint8_t wait, uint8_t p) { 73 if(x){ 74 for (int i = 0; i < p ; i++){ //loop for given random pixel count (passed from loop) 75 cstrip.setPixelColor(i, c); 76 } 77 cstrip.show(); //turn pixels on 78 x = false; 79 }else{ 80 if(millis()-prevCandleTime>wait){ 81 prevCandleTime = millis(); 82 for (int i = 0; i < p; i++) { 83 cstrip.setPixelColor(i, 0); //turn pixel off 84 } 85 x = true; 86 } 87 } 88} //end flickYellow 89 90void RGBLoop(){ 91 if(millis()-currentFadeTime>fadeWait){ 92 currentFadeTime = millis(); 93 if(fadeIn){ 94 switch(j) { 95 case 0: setAll(k,0,0); break; 96 case 1: setAll(0,k,0); break; 97 case 2: setAll(0,0,k); break; 98 } 99 fadeStrip.show(); 100 if(k==255){ 101 fadeIn=false; 102 } else { 103 k++; 104 } 105 } else { // Fade OUT 106 107 switch(j) { 108 case 0: setAll(k,0,0); break; 109 case 1: setAll(0,k,0); break; 110 case 2: setAll(0,0,k); break; 111 } 112 fadeStrip.show(); 113 if(k==0){ 114 fadeIn = true; 115 j++; 116 j%=3; 117 } else { 118 k--; 119 } 120 } 121 } 122} 123 124void setAll(byte red, byte green, byte blue) { 125 for(int i = 0; i < NUM_LEDS; i++ ) { 126 fadeStrip.setPixelColor(i, fadeStrip.Color(red, green, blue)); 127 } 128 fadeStrip.show(); 129}
Song
arduino
This code is for the song played by the pumpkin
1int speakerPin = 9; //change to match pin for speaker 2int numTones = 291; //change this to reflect the number of notes in your song 3int cs6 = 1109; // frequencies of the notes used in the song 4int fs5 = 740; 5int d6 = 1175; 6int c6 = 1047; 7int f5 = 698; 8int b5 = 988; 9int e5 = 659; 10int bf5 = 932; 11int ef5 = 622; 12int b4 = 494; 13int g5 = 784; 14int wait = 200; // length of the notes 15 16int tones[] = { cs6, fs5, fs5, cs6, fs5, fs5, cs6, fs5, d6, fs5, //notes in the song 17 cs6, fs5, fs5, cs6, fs5, fs5, cs6, fs5, d6, fs5, 18 cs6, fs5, fs5, cs6, fs5, fs5, cs6, fs5, d6, fs5, 19 cs6, fs5, fs5, cs6, fs5, fs5, cs6, fs5, d6, fs5, 20 cs6, fs5, fs5, cs6, fs5, fs5, cs6, fs5, d6, fs5, 21 cs6, fs5, fs5, cs6, fs5, fs5, cs6, fs5, d6, fs5, 22 c6, f5, f5, c6, f5, f5, c6, f5, cs6, f5, 23 c6, f5, f5, c6, f5, f5, c6, f5, cs6, f5, 24 cs6, fs5, fs5, cs6, fs5, fs5, cs6, fs5, d6, fs5, 25 cs6, fs5, fs5, cs6, fs5, fs5, cs6, fs5, d6, fs5, 26 c6, f5, f5, c6, f5, f5, c6, f5, cs6, f5, 27 c6, f5, f5, c6, f5, f5, c6, f5, cs6, f5, 28 b5, e5, e5, b5, e5, e5, b5, e5, c6, e5, 29 b5, e5, e5, b5, e5, e5, b5, e5, c6, e5, 30 bf5, ef5, ef5, bf5, ef5, ef5, bf5, ef5, b5, ef5, 31 bf5, ef5, ef5, bf5, ef5, ef5, bf5, ef5, b5, ef5, 32 b5, e5, e5, b5, e5, e5, b5, e5, c6, e5, 33 b5, e5, e5, b5, e5, e5, b5, e5, c6, e5, 34 bf5, ef5, ef5, bf5, ef5, ef5, bf5, ef5, b5, ef5, 35 bf5, ef5, ef5, bf5, ef5, ef5, bf5, ef5, b5, ef5, 36 fs5, b4, b4, fs5, b4, b4, fs5, b4, g5, b4, 37 fs5, b4, b4, fs5, b4, b4, fs5, b4, g5, b4, 38 fs5, b4, b4, fs5, b4, b4, fs5, b4, g5, b4, 39 fs5, b4, b4, fs5, b4, b4, fs5, b4, g5, b4, 40 fs5, b4, b4, fs5, b4, b4, fs5, b4, g5, b4, 41 fs5, b4, b4, fs5, b4, b4, fs5, b4, g5, b4, 42 fs5, b4, b4, fs5, b4, b4, fs5, b4, g5, b4, 43 fs5, b4, b4, fs5, b4, b4, fs5, b4, g5, b4, 44 fs5, b4, b4, fs5, b4, b4, fs5, b4, g5, b4}; 45 46unsigned long current_song_time = 0; unsigned long prev_song_time = 0; int note_count = 0; int on_off = 0; //variables for running the song 47 48void setup(){ 49 50} 51 52void loop() 53{ 54 current_song_time = millis(); 55 if(on_off == 0) 56 { 57 if (current_song_time - prev_song_time >= 20){ 58 if(note_count+1==numTones){ 59 wait = 10000; 60 on_off++; 61 on_off %= 2; 62 } else{ 63 prev_song_time = current_song_time; 64 tone(speakerPin, tones[note_count]); 65 on_off++; 66 on_off %= 2; 67 } 68 } 69 } else { 70 if (current_song_time - prev_song_time >= wait){//delays[note_count] 71 prev_song_time = current_song_time; 72 noTone(speakerPin); 73 note_count++; 74 note_count %= numTones; 75 on_off++; 76 on_off %= 2; 77 wait = 200; 78 } 79 } 80} 81 82 83 84
Eyes + Candle
arduino
This is the sketch to control the LEDs used in the eyes of pumpkin as well as the candle.
1#include <Adafruit_NeoPixel.h> 2 3//initialize the candle, and variables related to the candle 4//modified from: https://codebender.cc/sketch:67754#Neo%20Candle.ino 5#define lPIN 6 //this is the pin the candle LEDs are connected to 6int numpix = 4; //set the number of Pixels you have/want in the candle - more = brighter 7int randNumber; int color; int wait; int randpix; boolean x = true; unsigned long prevCandleTime; //additional variables used to operate candle 8Adafruit_NeoPixel cstrip = Adafruit_NeoPixel(numpix, lPIN, NEO_GRB + NEO_KHZ800); //define the candle led strip 9 10//initialize the eyes, and variables used to control eyes 11// modified from: http://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/#fadeinout_rgb 12#define fadePin 8 //this is the pin the glowing eye LEDs are connected to 13#define NUM_LEDS 4 //set the number of Pixels you have/want in the eyes - more = brighter 14int fadeWait = 3; // sets speed of eye fade. smaller = faster, bigger = slower 15unsigned long currentFadeTime = 0; int j = 0; int k = 0; boolean fadeIn = true; 16Adafruit_NeoPixel fadeStrip = Adafruit_NeoPixel(NUM_LEDS, fadePin, NEO_GRB + NEO_KHZ800); //define the led strip for the eyes 17 18void setup() { 19 pinMode(2, INPUT); //use pin 2 as input for the random seed - pick an unused analog read pin 20 randomSeed(analogRead(2)); //Set random number generator seed from analog pin 2 21 22 cstrip.begin(); 23 cstrip.show(); // Initialize all candle pixels to 'off' 24 25 fadeStrip.begin(); 26 fadeStrip.show(); // Initialize all eye pixels to 'off' 27} 28 29void loop(){ 30 candle(); 31 RGBLoop(); 32} //end main loop 33 34void candle(){ 35 if(x){ 36 wait = random(80, 120); //set a random wait period 37 randpix = random(0, numpix + 1); //choose a random number of pixels 38 color = random(0, 2); //Pick either yellow or orange 39 for (int i = 0; i < numpix / 2; i++){ //so it leaves a certain number of yellow pixels on (number of pixels/3) 40 cstrip.setPixelColor(i, 255, 120, 0); //set the number of pixels to turn on and color value (yellowish) 41 } 42 cstrip.show(); //turn pixels on 43 } 44 45 if (color == 0){ //if red was chosen 46 flickred(cstrip.Color(115, 50, 0), wait, randpix); // call flickred and pass it the red (orangeish) color values - change values to change color 47 } else { // otherwise use yellow 48 flickYellow(cstrip.Color(180, 80, 0), wait, randpix); // call flickYellow and pass it the yellow color values (change values to change color), and wait time and random pixel count 49 } 50} 51 52// Function for when red is chosen 53void flickred(uint32_t c, uint8_t wait, uint8_t p) { 54 if(x){ 55 for (int i = 0; i < p - 2; i++){ //loop for given random pixel count (passed from loop) 56 cstrip.setPixelColor(i, c); 57 } 58 cstrip.show(); //turn pixels on 59 x = false; 60 }else{ 61 if(millis()-prevCandleTime>wait){ 62 prevCandleTime = millis(); 63 for (int i = 0; i < p; i++) { 64 cstrip.setPixelColor(i, 0); //turn pixel off 65 } 66 x = true; 67 } 68 } 69} //end flickred 70 71// function for when yellow is chosen 72void flickYellow(uint32_t c, uint8_t wait, uint8_t p) { 73 if(x){ 74 for (int i = 0; i < p ; i++){ //loop for given random pixel count (passed from loop) 75 cstrip.setPixelColor(i, c); 76 } 77 cstrip.show(); //turn pixels on 78 x = false; 79 }else{ 80 if(millis()-prevCandleTime>wait){ 81 prevCandleTime = millis(); 82 for (int i = 0; i < p; i++) { 83 cstrip.setPixelColor(i, 0); //turn pixel off 84 } 85 x = true; 86 } 87 } 88} //end flickYellow 89 90void RGBLoop(){ 91 if(millis()-currentFadeTime>fadeWait){ 92 currentFadeTime = millis(); 93 if(fadeIn){ 94 switch(j) { 95 case 0: setAll(k,0,0); break; 96 case 1: setAll(0,k,0); break; 97 case 2: setAll(0,0,k); break; 98 } 99 fadeStrip.show(); 100 if(k==255){ 101 fadeIn=false; 102 } else { 103 k++; 104 } 105 } else { // Fade OUT 106 107 switch(j) { 108 case 0: setAll(k,0,0); break; 109 case 1: setAll(0,k,0); break; 110 case 2: setAll(0,0,k); break; 111 } 112 fadeStrip.show(); 113 if(k==0){ 114 fadeIn = true; 115 j++; 116 j%=3; 117 } else { 118 k--; 119 } 120 } 121 } 122} 123 124void setAll(byte red, byte green, byte blue) { 125 for(int i = 0; i < NUM_LEDS; i++ ) { 126 fadeStrip.setPixelColor(i, fadeStrip.Color(red, green, blue)); 127 } 128 fadeStrip.show(); 129}
Downloadable files
This is the wiring diagram for the LED strips and speaker
This is the wiring diagram for the LED strips and speaker
This is the wiring diagram for the LED strips and speaker
This is the wiring diagram for the LED strips and speaker
Comments
Only logged in users can leave comments
aams86
0 Followers
•0 Projects
+2
Work attribution
Table of contents
Intro
19
0