#include "LPD8806.h"
#include "SPI.h"
#include <Tone.h>
#define Abby 5
#define Bre 6
#define pot A0
#define AbbyButtonLight 11
#define BreButtonLight 12
Tone tone1;
int nLEDs = 32;
int dataPin = 9;
int clockPin = 10;
char turn = 'A';
char flag = 'N';
char cheat = 'N';
int i = 0;
int j = 0;
byte AbbyPoints = 0;
byte BrePoints = 0;
byte ballSpeed = 0;
byte ballSpeedIncrease = 0;
int val = 0;
LPD8806 strip = LPD8806(32, dataPin, clockPin);
void setup()
{
pinMode(AbbyButtonLight, OUTPUT);
pinMode(BreButtonLight, OUTPUT);
tone1.begin(13);
pinMode(pot, INPUT);
strip.begin();
strip.show();
pinMode(Abby, INPUT);
pinMode(Bre, INPUT);
digitalWrite(AbbyButtonLight, HIGH);
}
void loop()
{
if(AbbyPoints > 6)
{
ballSpeedIncrease = 0;
tone1.play(NOTE_G5, 200);
delay(200);
tone1.play(NOTE_GS5, 200);
delay(200);
tone1.play(NOTE_A5, 200);
delay(200);
AbbyCelebrate();
}
if(BrePoints > 6)
{
ballSpeedIncrease = 0;
tone1.play(NOTE_G5, 200);
delay(200);
tone1.play(NOTE_GS5, 200);
delay(200);
tone1.play(NOTE_A5, 200);
delay(200);
BreCelebrate();
}
if(flag != 'F' && flag != 'R')
{
val = analogRead(pot);
val = map(val, 0, 1023, 15, 22);
ballSpeed = val - ballSpeedIncrease;
}
if(flag == 'F')
{
ballSpeedIncrease += 2;
ballSpeed = val - ballSpeedIncrease;
if(ballSpeed < (val - 14))
ballSpeed = (val - 14);
if(val > 21)
ballSpeed = 22;
tone1.play(NOTE_F5, 50);
delay(50);
chaseForward(strip.Color(127, 0, 0), ballSpeed);
}
if(flag == 'R')
{
ballSpeedIncrease += 2;
ballSpeed = val - ballSpeedIncrease;
if(ballSpeed < (val - 14))
ballSpeed = (val - 14);
if(val > 21)
ballSpeed = 22;
tone1.play(NOTE_F5, 50);
delay(50);
chaseReverse(strip.Color(0, 0, 127), ballSpeed);
}
int AbuttonState = digitalRead(Abby);
if(AbuttonState == HIGH && turn == 'A' && flag != 'F' && flag != 'R')
{
ballSpeed = val;
ballSpeedIncrease = 0;
i = 0;
digitalWrite(AbbyButtonLight, LOW);
chaseForward(strip.Color(0, 127, 0), ballSpeed); // White
}
int BbuttonState = digitalRead(Bre);
if(BbuttonState == HIGH && turn == 'B' && flag != 'F' && flag != 'R')
{
ballSpeed = val;
ballSpeedIncrease = 0;
i = 32;
digitalWrite(BreButtonLight, LOW);
chaseReverse(strip.Color(127, 0, 127), ballSpeed); // Red
}
/*chaseReverse(strip.Color(127, 0, 0), 15); // Red
chaseForward(strip.Color(127, 127, 0), 15); // Yellow
chaseReverse(strip.Color( 0, 127, 0), 15); // Green
chaseForward(strip.Color( 0, 127, 127), 15); // Cyan
chaseReverse(strip.Color( 0, 0, 127), 15); // Blue
chaseForward(strip.Color(127, 0, 127), 15); // Violet
chaseReverse(strip.Color(100, 100, 10), 15); // Blue */
}
void chaseForward(uint32_t c, uint8_t wait) {
// Start by turning all pixels off:
//for(i=0; i < 32; i++) strip.setPixelColor(i, 0);
for(i; i < 32; i++)
{
if(digitalRead(Bre) == HIGH)
cheat = 'Y';
strip.setPixelColor(i, c); // Set new pixel 'on'
strip.show(); // Refresh LED states
strip.setPixelColor(i, 0); // Erase pixel, but don't refresh!
delay(wait);
if(i > 28 && cheat != 'Y')
{
int BbuttonState = digitalRead(Bre);
if(BbuttonState == HIGH)
{
//i = 32;
turn = 'B';
flag = 'R';
return;
//chaseReverse(strip.Color(127, 0, 0), 50);
}
}
}
AbbyPoints++;
if(AbbyPoints < 7)
strip.show();
tone1.play(NOTE_B3, 500);
delay(500);
digitalWrite(AbbyButtonLight, HIGH);
flag = 'S';
cheat = 'N';
// Refresh to turn off last pixel
}
void chaseReverse(uint32_t c, uint8_t wait) {
// Start by turning all pixels off:
//for(i=0; i < 32; i++) strip.setPixelColor(i, 0);
for(i; i >= 0; i--)
{
if(digitalRead(Abby) == HIGH)
cheat = 'Y';
strip.setPixelColor(i, c); // Set new pixel 'on'
strip.show(); // Refresh LED states
strip.setPixelColor(i, 0); // Erase pixel, but don't refresh!
delay(wait);
if(i < 5 && cheat != 'Y')
{
int BbuttonState = digitalRead(Abby);
if(BbuttonState == HIGH)
{
//i = 0;
turn = 'A';
flag = 'F';
return;
//chaseForward(strip.Color(127, 127, 127), 50);
}
}
}
BrePoints++;
strip.show();
if(BrePoints < 7)
tone1.play(NOTE_B3, 500);
delay(500);
digitalWrite(BreButtonLight, HIGH);
flag = 'S';
cheat = 'N';
// Refresh to turn off last pixel
}
void AbbyCelebrate()
{
//rainbow(10);
rainbowCycleAbby(0);
AbbyPoints = 0;
BrePoints = 0;
}
void BreCelebrate()
{
//rainbow(10);
rainbowCycleBre(0);
AbbyPoints = 0;
BrePoints = 0;
}
void rainbowCycleAbby(uint8_t wait) {
int m = 32;
uint16_t k, j;
digitalWrite(AbbyButtonLight, LOW);
for (j=0; j < 384 * 3; j++) { // 5 cycles of all 384 colors in the wheel
for (k=0; k < strip.numPixels(); k++) {
// 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(k, Wheel( ((k * 384 / strip.numPixels()) + j) % 384) );
}
strip.show(); // write all the pixels out
delay(wait);
}
for(m; m >= 0; m--)
{
strip.setPixelColor(m, 0);
strip.show();
}
digitalWrite(AbbyButtonLight, HIGH);
}
void rainbowCycleBre(uint8_t wait) {
int m = 0;
uint16_t k, j;
digitalWrite(BreButtonLight, LOW);
for (j=384 * 3; j > 0; j--) { // 5 cycles of all 384 colors in the wheel
for (k=0; k < strip.numPixels(); k++) {
// 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(k, Wheel( ((k * 384 / strip.numPixels()) + j) % 384) );
}
strip.show(); // write all the pixels out
delay(wait);
}
for(m; m < 32; m++)
{
strip.setPixelColor(m, 0);
strip.show();
}
digitalWrite(BreButtonLight, HIGH);
}
uint32_t Wheel(uint16_t WheelPos)
{
byte r, g, b;
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));
}