// Halloween Jack-O-Lantern Sketch for Arno Digital RGB Add-On // olympiacircuits.com // 2013 /* Based on code from ### teldredge ### www.funkboxing.com ### teldredge1979@gmail.com */ #include #define NUM_LEDS 3 int BOTTOM_INDEX = 0; int TOP_INDEX = int(NUM_LEDS/2); int EVENODD = NUM_LEDS%2; int ledOrder[3] = { 2,0,1}; //set order of LEDs on board int piezo = 12; //set pin for piezo float pi = 3.14159; int SW1 = 1; //set pin for left button int SW2 = 4; //set pin for right button #define PIN 16 //set pin for Arno communication to Add-On board int POT1 = A0; // define pins for analog RGB on Arno int redLED = 9; int greenLED = 10; int blueLED = 11; // float RGB1[3]; float RGB2[3]; float INC[3]; int red, green, blue; // Set RGB Order struct CRGB { unsigned char g; unsigned char r; unsigned char b; }; struct CRGB *leds; //int ledsX[NUM_LEDS][3]; //-ARRAY FOR COPYING WHATS IN THE LED STRIP CURRENTLY (FOR CELL-AUTOMATA, ETC) //SerialCommand sCmd; //-SETUP SerialCommand OBJECT int ledMode = 14; //-START IN DEMO MODE //int ledMode = 5; //-PERISTENT VARS int ihue = 0; //-HUE (0-360) int ibright = 0; //-BRIGHTNESS (0-255) int isat = 0; //-SATURATION (0-255) float tcount = 0.002; //-INC VAR FOR SIN LOOPS float tspot = 1; //------------------SETUP------------------ void setup() { pinMode(SW1,INPUT); pinMode(SW2,INPUT); // Serial.begin(115200); // SETUP HARDWARE SERIAL (USB) FastSPI_LED.setLeds(NUM_LEDS); FastSPI_LED.setChipset(CFastSPI_LED::SPI_WS2811); //FastSPI_LED.setChipset(CFastSPI_LED::SPI_TM1809); FastSPI_LED.setPin(PIN); FastSPI_LED.setDataRate(7); //-IF LEDS FLICKER, PLAY WITH THIS (0-7) FastSPI_LED.init(); FastSPI_LED.start(); leds = (struct CRGB*)FastSPI_LED.getRGBData(); FastSPI_LED.show(); //setup for RGB Fade randomSeed(analogRead(A1)); pinMode(redLED,OUTPUT); pinMode(greenLED,OUTPUT); pinMode(blueLED,OUTPUT); RGB1[0] = random(256); RGB1[1] = random(256); RGB1[2] = random(256); RGB2[0] = random(256); RGB2[1] = random(256); RGB2[2] = random(256); } //////////////////////////////////////////////////// void loop(){ if(digitalRead(SW1)==LOW) strobe(); //if(digitalRead(SW2)==LOW) flame(); if(digitalRead(SW2)==LOW) fade(); } // Functions are set up with while loops to let you use the pot to stop the function. // This can be removed for continuous operation. ///////////////////////////////////////////////////////// //flame function void flame(){ int acolor[3]; //set saturation int sat = 255; //set LED hue 1 yellow, 1 red, 1 orange int yHue = 50; int rHue = 0; int oHue = 32; //set LED position int yLED = 1; int rLED = 0; int oLED = 2; // set value to each LED int readPot = analogRead(POT1); while (readPot < 342) { int yVal = random(180,255); int rVal = random (180,255); int oVal = random (180,255); //set yellow HSVtoRGB(yHue,255,yVal,acolor); int pick = yLED; leds[pick].r = acolor[0]; leds[pick].g = acolor[1]; leds[pick].b = acolor[2]; FastSPI_LED.show(); delay (random (25,150)); //set red HSVtoRGB(rHue,sat,rVal,acolor); pick = rLED; leds[pick].r = acolor[0]; leds[pick].g = acolor[1]; leds[pick].b = acolor[2]; FastSPI_LED.show(); delay (random (25,150)); //set orange HSVtoRGB(oHue,sat,oVal,acolor); pick = oLED; leds[pick].r = acolor[0]; leds[pick].g = acolor[1]; leds[pick].b = acolor[2]; FastSPI_LED.show(); delay (random (25,150)); readPot = analogRead(POT1); } clearAll(); } ////////////////////////////////////////////////////// //Strobe void strobe () { int readPot = 0; int acolor[3]; while (readPot < 342) { int hue = 0; int sat = 0; //flash on for(int i = 0; i < 3; i++) { HSVtoRGB(hue,sat,255,acolor); leds[i].r = acolor[0]; leds[i].g = acolor[1]; leds[i].b = acolor[2]; } FastSPI_LED.show(); tone(piezo,5000); delay (50); noTone(piezo); //flash off for(int i = 0; i < 3; i++) { HSVtoRGB(hue,sat,0,acolor); leds[i].r = acolor[0]; leds[i].g = acolor[1]; leds[i].b = acolor[2]; } FastSPI_LED.show(); delay (25); readPot = analogRead(POT1); } } ////////////////////////////////////////////////////// //RGB fade void fade(){ int readPot = 0; while (readPot < 342) { for (int x=0; x<3; x++) { INC[x] = (RGB1[x] - RGB2[x]) / 256; } for (int x=0; x<256; x++) { green = int(RGB1[0]); red = int(RGB1[1]); blue = int(RGB1[2]); // write levels to analog LED analogWrite (redLED, red); analogWrite (greenLED, green); analogWrite (blueLED, blue); //load data and write to digital RGB for(int i = 0; i < 3; i++) { leds[i].r = red; leds[i].g = green; leds[i].b = blue; } FastSPI_LED.show(); // end of digital portion delay(10); RGB1[0] -= INC[0]; RGB1[1] -= INC[1]; RGB1[2] -= INC[2]; } for (int x=0; x<3; x++) { RGB2[x] = random(556)-300; RGB2[x] = constrain(RGB2[x], 0, 255); delay(10); } readPot = analogRead (POT1); } // shut off all LEDs clearAll(); analogWrite (redLED,0); analogWrite (greenLED,0); analogWrite (blueLED,0); } ////////////////////////////////////////////////////// //------------------------------------- UTILITY FXNS -------------------------------------- //-CONVERT HSV VALUE TO RGB void HSVtoRGB(int hue, int sat, int val, int colors[3]) { // hue: 0-359, sat: 0-255, val (lightness): 0-255 int r, g, b, base; if (sat == 0) { // Achromatic color (gray). colors[0]=val; colors[1]=val; colors[2]=val; } else { base = ((255 - sat) * val)>>8; switch(hue/60) { case 0: r = val; g = (((val-base)*hue)/60)+base; b = base; break; case 1: r = (((val-base)*(60-(hue%60)))/60)+base; g = val; b = base; break; case 2: r = base; g = val; b = (((val-base)*(hue%60))/60)+base; break; case 3: r = base; g = (((val-base)*(60-(hue%60)))/60)+base; b = val; break; case 4: r = (((val-base)*(hue%60))/60)+base; g = base; b = val; break; case 5: r = val; g = base; b = (((val-base)*(60-(hue%60)))/60)+base; break; } colors[0]=r; colors[1]=g; colors[2]=b; } } void clearAll(){ noTone(piezo); for(int i = 0; i < 3; i++) { leds[i].r = 0; leds[i].g = 0; leds[i].b = 0; } FastSPI_LED.show(); }