#include <MeetAndroid.h>
#include "SPI.h"
#include "WS2801.h"
int dataPin = 2;
int clockPin = 3;
byte flag;
MeetAndroid meetAndroid;
void setup() {
strip.begin();
Serial.begin(9600);
meetAndroid.registerFunction(RANDColorBT, 'A');
meetAndroid.registerFunction(RANDColorStrobeBT, 'B');
meetAndroid.registerFunction(rainbowBT, 'C');
meetAndroid.registerFunction(rainbowCycleBT, 'D');
strip.show();
}
void loop()
{
Serial.println(flag);
switch(flag) {
case 65:
RANDColor(-1,24,50);
break;
case 66:
RANDColorStrobe(50,200);
break;
case 67:
rainbow(20);
break;
case 68:
rainbowCycle(20);
break;
default:
meetAndroid.receive();
}
meetAndroid.receive(); // you need to keep this in your loop() to receive events
}
void RANDColorBT(byte flag, byte numOfValues) {
RANDColor(-1,24,50);
}
void RANDColorStrobeBT(byte flag, byte numOfValues){
RANDColorStrobe(50,200);
}
void rainbowBT(byte flag, byte numOfValues) {
rainbow(20);
}
void rainbowCycleBT(byte flag, byte numOfValues) {
rainbowCycle(20);
}
void RANDColor(uint32_t c, uint32_t flashes, uint8_t wait) {
int i, j, k, m;
int pixPic[strip.numPixels()];
k = 0;
j = 0;
m = 0;
while(k < flashes) {
for (i=0; i < strip.numPixels(); i++) {
pixPic[i] = 1;
}
for(i=0; i < strip.numPixels(); i++) {
while(j==0) {
m = random(0,strip.numPixels());
j = pixPic[m];
}
j = 0;
pixPic[m] = 0;
if (c == -1) {
strip.setPixelColor(m,Wheel(random(0,255)));
}
else {
strip.setPixelColor(m,c);
}
Serial.println(Serial.read());
strip.show();
delay(wait);
k++;
}
}
}
void RANDColorStrobe(uint32_t flashes, uint8_t wait) {
int i, j, k;
for(k=0; k < flashes; k++) {
for(j=0; j < strip.numPixels(); j++) {
strip.setPixelColor(j,Wheel(random(0,255)));
}
if (Serial.read() != -1){
flag = Serial.read();
break;
}
strip.show();
delay(wait);
}
}
void rainbow(uint8_t wait) {
int i, j, k;
k=0;
for (j=0; j < 256; j++) { // 3 cycles of all 256 colors in the wheel
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel( (i + j) % 255));
if(k < strip.numPixels()) {
meetAndroid.receive();
strip.show();
delay(wait);
k++;
}
}
meetAndroid.receive();
strip.show(); // write all the pixels out
delay(wait);
}
}
void rainbowCycle(uint8_t wait) {
int i, j, k;
k=0;
for (j=0; j < 256 * 5; j++) { // 5 cycles of all 25 colors in the wheel
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
if(k < strip.numPixels()) {
meetAndroid.receive();
strip.show(); // write all the pixels out
delay(wait);
k++;
}
}
meetAndroid.receive();
strip.show(); // write all the pixels out
delay(wait);
}
}
// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
uint32_t c;
c = r;
c <<= 8;
c |= g;
c <<= 8;
c |= b;
return c;
}
//Input a value 0 to 255 to get a color value.
//The colours are a transition r - g -b - back to r
uint32_t Wheel(byte WheelPos)
{
if (WheelPos < 85) {
return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if (WheelPos < 170) {
WheelPos -= 85;
return Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}