// Mbed program to test LPD8806-based RGB LED Strips // (c) 2011 Jelmer Tiete // This library is ported from the Arduino implementation of Adafruit Industries // found at: http://github.com/adafruit/LPD8806 // and their strips: http://www.adafruit.com/products/306 // Released under the MIT License: http://mbed.org/license/mit // // standard connected to 1st hardware SPI // LPD8806 <> MBED // DATA -> P5 // CLOCK -> p7 /*****************************************************************************/ #include "LPD8806.h" #include "SRF05.h" #include #include //Set up an analogue input pin19 AnalogIn ain(p19); // Create the UltraSonic Ranger object SRF05 srf(p14, p15); // Create the interrupt receiver object on pin 26 InterruptIn interrupt(p26); // Setup the i2c bus on pins 9 and 10 I2C i2c(p28, p27); // Setup the Mpr121: Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); // constructor for the LED strip LPD8806 strip = LPD8806(50); bool stateTrigger; // true if hit , false if released int buttonValue; // value of the pin touched. // Chase a dot down the strip // good for testing purposes void colorChase(uint32_t c, uint8_t delay) { int i; for (i=0; i < strip.numPixels(); i++) { strip.setPixelColor(i, 0); // turn all pixels off } for (i=0; i < strip.numPixels(); i++) { strip.setPixelColor(i, c); if (i == 0) { strip.setPixelColor(strip.numPixels()-1, 0); } else { strip.setPixelColor(i-1, 0); } strip.show(); wait_ms(delay); } } // fill the dots one after the other with said color // good for testing purposes void colorWipe(uint32_t c, uint8_t delay) { int i; for (i=0; i < strip.numPixels(); i++) { strip.setPixelColor(i, c); strip.show(); //wait_ms(delay); } } //Input a value 0 to 384 to get a color value. //The colours are a transition r - g -b - back to r uint32_t Wheel(uint16_t WheelPos) { uint8_t b=0; uint8_t g=0; uint8_t r = 0; switch (WheelPos / 128) { case 0: r = 127 - WheelPos % 128; //Red down g = WheelPos % 128; // Green up b = 0; //blue off break; case 1: g = 127 - WheelPos % 128; //green down b = WheelPos % 128; //blue up r = 0; //red off break; case 2: b = 127 - WheelPos % 128; //blue down r = WheelPos % 128; //red up g = 0; //green off break; } return(strip.Color(r,g,b)); } void rainbow(uint8_t delay, float* newV, float oldV) { int i, j; // j is the number 0 to 384 *newV = (0.65*oldV) + ((0.35)*(*newV)) ; // smoothing transition out. j = ((*newV) - 0.25) / 0.0078125; // range is 0.25 - 3.25 V ==> base of 0.25 for (i=0; i < strip.numPixels(); i++) { strip.setPixelColor(i, Wheel(j % 384)); // % excludes 384 and allows rotation around the wheel. } strip.show(); // write all the pixels out wait_ms(delay); } // Slightly different, this one makes the rainbow wheel equally distributed // along the chain void rainbowCycle(uint8_t delay) { uint16_t i, j; for (j=0; j < 384 * 5; j++) { // 5 cycles of all 384 colors in the wheel for (i=0; i < strip.numPixels(); i++) { // tricky math! we use each pixel as a fraction of the full 384-color wheel // (thats the i / strip.numPixels() part) // Then add in j which makes the colors go around per pixel // the % 384 is to make the wheel cycle around strip.setPixelColor(i, Wheel( ((i * 384 / strip.numPixels()) + j) % 384) ); } strip.show(); // write all the pixels out wait_ms(delay); } } // "Larson scanner" = Cylon/KITT bouncing light effect void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t delay) { int i, j, pos, dir; pos = 0; dir = 1; for (i=0; i<((strip.numPixels()-1) * 8); i++) { // Draw 5 pixels centered on pos. setPixelColor() will clip // any pixels off the ends of the strip, no worries there. // we'll make the colors dimmer at the edges for a nice pulse // look strip.setPixelColor(pos - 2, strip.Color(r/4, g/4, b/4)); strip.setPixelColor(pos - 1, strip.Color(r/2, g/2, b/2)); strip.setPixelColor(pos, strip.Color(r, g, b)); strip.setPixelColor(pos + 1, strip.Color(r/2, g/2, b/2)); strip.setPixelColor(pos + 2, strip.Color(r/4, g/4, b/4)); strip.show(); wait_ms(delay); // If we wanted to be sneaky we could erase just the tail end // pixel, but it's much easier just to erase the whole thing // and draw a new one next time. for (j=-2; j<= 2; j++) strip.setPixelColor(pos+j, strip.Color(0,0,0)); // Bounce off ends of strip pos += dir; if (pos < 0) { pos = 1; dir = -dir; } else if (pos >= strip.numPixels()) { pos = strip.numPixels() - 2; dir = -dir; } } } // Key hit/release interrupt routine for MPR121 void fallInterrupt() { stateTrigger = !stateTrigger; // invert global variable if (stateTrigger == true) { buttonValue = mpr121.read(0x00); buttonValue += mpr121.read(0x01)<<8; } } int main() { float V, SRF; // voltage on pin as a float + distnace float oV = 0.25; // just the start value for old Voltage. int i; // for for loop; stateTrigger = false; buttonValue = 0; strip.begin(); // Start up the LED strip. strip.show(); // Update the strip, to start they are all 'off' for (i=0; i<12; i++) { // Set up for sensitivity of Electrodes. mpr121.setElectrodeThreshold(i, 65, 65); } interrupt.fall(&fallInterrupt); // Attach interrupt and its mode respectively. interrupt.mode(PullUp); while (1) { SRF = srf.read(); // distance in cm from the UltraSonic Ranger (updt. every 0.2s) if (stateTrigger == true) { switch (buttonValue) { //r,g,b case 1 : colorChase(strip.Color(127,0,0), 10); break; // red (instantaneously!!! , delay = 20ms) case 2 : colorChase(strip.Color(127,64,0), 10); break; // pale-red (instantaneously!!! , delay = 0) case 4 : colorChase(strip.Color(127,127,0), 10); break; // orange (instantaneously!!! , delay = 0) case 8 : colorChase(strip.Color(64,127,0), 10); break; // pale-orange (instantaneously!!! , delay = 0) case 16 : colorChase(strip.Color(0,127,0), 10); break; // green (instantaneously!!! , delay = 0) case 32 : colorChase(strip.Color(0,127,64), 10); break; // pale-teal (instantaneously!!! , delay = 0) case 64 : colorChase(strip.Color(0,127,127), 10); break; // teal (instantaneously!!! , delay = 0) case 128 : colorChase(strip.Color(0,64,127), 10); break; // pale-blue (instantaneously!!! , delay = 0) case 256 : colorChase(strip.Color(0,0,127), 10); break; // blue (instantaneously!!! , delay = 0) case 512 : colorChase(strip.Color(64,0,127), 10); break; // pale-violet (instantaneously!!! , delay = 0) case 1024 : colorChase(strip.Color(127,0,127), 10); break; // violet (instantaneously!!! , delay = 0) case 2048 : colorChase(strip.Color(127,127,127), 10); break; // white (instantaneously!!! , delay = 0) default : colorChase(strip.Color(127,127,0), 100); // orange (2buttons) SOMETHING WRONG IF THIS OCCURS! sloooow. } } else { if (SRF > 100.00) { // will enter and stay here only if subject is 1m away at least. printf("MODE1 Distance = %.1f\n", SRF); //testing purposes ------------------- if (SRF > 250.00) { scanner(127,0,127, 25); // violet, slow } else { scanner(0,127,127, 10); // teal, fast } } else { printf("MODE2 Distance = %.1f\n", SRF); //testing purposes ----------------- V = ain.read() * 3.3 ; // get real voltage if (V <= 0.25) V = 0.25 ; // safety range (for operation) lower limit if (V >= 3.25) V = 3.25 ; // safety range (for operation) upper limit rainbow(50, &V, oV); // execute rainbow oV = V; // store old Voltage } // small if (distance) } // big if (buttonPressed) } // while } // main