Want more features on Pastebin? Sign Up, it's FREE!
Guest

Bluetooth Arduino RGB LED Pixels

By: CBud on Apr 2nd, 2012  |  syntax: C++  |  size: 3.71 KB  |  views: 69  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <MeetAndroid.h>
  2. #include "SPI.h"
  3. #include "WS2801.h"
  4.  
  5. int dataPin = 2;
  6. int clockPin = 3;
  7.  
  8. byte flag;
  9. MeetAndroid meetAndroid;
  10.  
  11.  
  12. void setup() {
  13.    
  14.   strip.begin();
  15.  
  16.   Serial.begin(9600);
  17.  
  18.   meetAndroid.registerFunction(RANDColorBT, 'A');
  19.   meetAndroid.registerFunction(RANDColorStrobeBT, 'B');  
  20.   meetAndroid.registerFunction(rainbowBT, 'C');  
  21.   meetAndroid.registerFunction(rainbowCycleBT, 'D');
  22.  
  23.   strip.show();
  24. }
  25.  
  26.  
  27. void loop()
  28. {
  29.   Serial.println(flag);
  30.   switch(flag) {
  31.     case 65:
  32.       RANDColor(-1,24,50);
  33.       break;
  34.     case 66:
  35.       RANDColorStrobe(50,200);
  36.       break;
  37.     case 67:
  38.       rainbow(20);
  39.       break;
  40.     case 68:
  41.       rainbowCycle(20);
  42.       break;
  43.     default:
  44.       meetAndroid.receive();
  45.   }
  46.   meetAndroid.receive(); // you need to keep this in your loop() to receive events
  47. }
  48.  
  49. void RANDColorBT(byte flag, byte numOfValues) {
  50.   RANDColor(-1,24,50);
  51. }
  52. void RANDColorStrobeBT(byte flag, byte numOfValues){
  53.   RANDColorStrobe(50,200);
  54. }
  55. void rainbowBT(byte flag, byte numOfValues) {
  56.   rainbow(20);
  57. }
  58. void rainbowCycleBT(byte flag, byte numOfValues) {
  59.   rainbowCycle(20);
  60. }
  61.  
  62. void RANDColor(uint32_t c, uint32_t flashes, uint8_t wait) {
  63.   int i, j, k, m;
  64.   int pixPic[strip.numPixels()];
  65.   k = 0;
  66.   j = 0;
  67.   m = 0;
  68.   while(k < flashes) {
  69.     for (i=0; i < strip.numPixels(); i++) {
  70.       pixPic[i] = 1;
  71.     }
  72.     for(i=0; i < strip.numPixels(); i++) {
  73.       while(j==0) {
  74.         m = random(0,strip.numPixels());
  75.         j = pixPic[m];
  76.       }
  77.       j = 0;
  78.       pixPic[m] = 0;
  79.       if (c == -1) {
  80.       strip.setPixelColor(m,Wheel(random(0,255)));
  81.       }
  82.       else {
  83.       strip.setPixelColor(m,c);
  84.       }
  85.       Serial.println(Serial.read());
  86.       strip.show();
  87.       delay(wait);
  88.       k++;
  89.     }
  90.   }
  91. }
  92.  
  93. void RANDColorStrobe(uint32_t flashes, uint8_t wait) {
  94.   int i, j, k;
  95.   for(k=0; k < flashes; k++) {
  96.     for(j=0; j < strip.numPixels(); j++) {
  97.       strip.setPixelColor(j,Wheel(random(0,255)));
  98.     }
  99.     if (Serial.read() != -1){
  100.     flag = Serial.read();
  101.     break;
  102.     }
  103.     strip.show();
  104.     delay(wait);
  105.   }
  106. }
  107.  
  108. void rainbow(uint8_t wait) {
  109.   int i, j, k;
  110.   k=0;
  111.    
  112.   for (j=0; j < 256; j++) {     // 3 cycles of all 256 colors in the wheel
  113.     for (i=0; i < strip.numPixels(); i++) {
  114.       strip.setPixelColor(i, Wheel( (i + j) % 255));
  115.  
  116.       if(k < strip.numPixels()) {
  117.         meetAndroid.receive();
  118.         strip.show();
  119.         delay(wait);
  120.         k++;
  121.       }
  122.     }  
  123.     meetAndroid.receive();
  124.     strip.show();   // write all the pixels out
  125.     delay(wait);
  126.   }
  127. }
  128.  
  129. void rainbowCycle(uint8_t wait) {
  130.   int i, j, k;
  131.   k=0;
  132.  
  133.   for (j=0; j < 256 * 5; j++) {     // 5 cycles of all 25 colors in the wheel
  134.     for (i=0; i < strip.numPixels(); i++) {
  135.       strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
  136.       if(k < strip.numPixels()) {
  137.         meetAndroid.receive();
  138.         strip.show();   // write all the pixels out
  139.         delay(wait);
  140.         k++;
  141.       }
  142.     }
  143.    meetAndroid.receive();
  144.     strip.show();   // write all the pixels out
  145.     delay(wait);
  146.   }
  147. }
  148.  
  149. // Create a 24 bit color value from R,G,B
  150. uint32_t Color(byte r, byte g, byte b)
  151. {
  152.   uint32_t c;
  153.   c = r;
  154.   c <<= 8;
  155.   c |= g;
  156.   c <<= 8;
  157.   c |= b;
  158.   return c;  
  159. }
  160.  
  161. //Input a value 0 to 255 to get a color value.
  162. //The colours are a transition r - g -b - back to r
  163. uint32_t Wheel(byte WheelPos)
  164. {
  165.   if (WheelPos < 85) {
  166.    return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  167.   } else if (WheelPos < 170) {
  168.    WheelPos -= 85;
  169.    return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  170.   } else {
  171.    WheelPos -= 170;
  172.    return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  173.   }
  174. }
clone this paste RAW Paste Data