Skip to content

Instantly share code, notes, and snippets.

@cristianmiranda
Created June 16, 2018 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cristianmiranda/5c927a2404b3c9ae2d1465f4de2cf87b to your computer and use it in GitHub Desktop.
Save cristianmiranda/5c927a2404b3c9ae2d1465f4de2cf87b to your computer and use it in GitHub Desktop.
//#include <SoftwareSerial.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
//#define RxD 2
//#define TxD 1
#define LED 5
#define DEBUG_ENABLED 1
#define blueToothSerial Serial
//SoftwareSerial blueToothSerial(RxD, TxD);
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, LED, NEO_GRB + NEO_KHZ800);
int led = LED;
int loadingIndex = 0;
int onTrack = 1;
int onesecond = 1000;
int waiting = 250;
void setup()
{
strip.begin();
strip.clear();
strip.show();
// pinMode(RxD, INPUT);
// pinMode(TxD, OUTPUT);
setupBlueToothConnection();
}
void loop()
{
String hex;
char recvChar;
// colorWipe(strip.Color(0, 0, 255), 50); // Blue
while (1)
{
//check if there's any data sent from the remote bluetooth shield
if (blueToothSerial.available())
{
recvChar = blueToothSerial.read();
hex = String(recvChar, HEX);
if(hex == "0")
{
loading(2);
colorWipe(strip.Color(0, 0, 0), 0); // Off
}
if(hex == "1")
{
turnRight();
}
if(hex == "2")
{
turnLeft();
}/*
if(hex == "3")
{
continueFront();
continueFront();
continueFront();
}
if(hex == "4")
{
commingCall();
commingCall();
commingCall();
}
if(hex == "5")
{
dangerZone();
dangerZone();
}
if(hex == "6")
{
turnRightDiagonalUp();
}
if(hex == "7")
{
turnRightDiagonalDown();
}*/
if(hex == "8")
{
turnLeftDiagonalUp();
}
if(hex == "9")
{
turnLeftDiagonalDown();
}
/*
if (hex == "1")
{
colorWipe(strip.Color(0, 0, 0), 0); // Off
delay(200);
onTrack = 1;
}
else if (hex == "2")
{
colorWipe(strip.Color(0, 0, 0), 0); // Off
delay(200);
onTrack = 0;
}
*/
}
else
{
//onTrack = 0;
//loading(1);
//delay(500);
//continueFront();
//loading(2);
/*
if (onTrack == 1)
{
colorBlink(strip.Color(0, 255, 0), 500, 3); // Green
}
else if (onTrack == 0)
{
loading(1);
colorWipe(strip.Color(0, 0, 0), 0); // Off
delay(500);
colorBlink(strip.Color(0, 255, 0), 500, 3); // Green
onTrack = 1;
}
*/
}
}
}
void setupBlueToothConnection()
{
blueToothSerial.begin(9600);
blueToothSerial.print("AT");
blueToothSerial.print("AT+ROLE0");
blueToothSerial.print("AT+UUID0xFFE0");
blueToothSerial.print("AT+CHAR0xFFE1");
blueToothSerial.print("AT+NAMERingo_BLE");
}
// Fill the dots one after the other with a color
void colorBlink(uint32_t c, uint8_t wait, int times)
{
/* for (int j = 0; j < times; j++)
{
for (uint16_t i = 0; i < strip.numPixels(); i++)
{
if (i == 0 || i == 1 || i == 15)
{
strip.setPixelColor(i, c);
}
}
strip.show();
delay(wait);
colorWipe(strip.Color(0, 0, 0), 0); // Off
delay(wait);
}*/
}
void continueFront()
{
/*uint32_t c = strip.Color(0, 255, 0); //Green
int off = strip.Color(0, 0, 0); //off
int wait = 150;
strip.setPixelColor(14, c);
strip.setPixelColor(2, c);
strip.show();
delay(wait);
colorWipe(off, 0); // Off
strip.setPixelColor(15, c);
strip.setPixelColor(1, c);
strip.show();
delay(wait);
colorWipe(off, 0); // Off
strip.setPixelColor(0, c);
strip.show();
delay(wait);
colorWipe(off, 0); // Off */
}
void turnRight()
{
/* uint32_t white = strip.Color(255, 255, 255); //White
uint32_t green = strip.Color(0, 255, 0); //Green
int off = strip.Color(0, 0, 0); //off
// Turn on all leds in white
for(uint16_t init = 0; init < 9; init++)
{
strip.setPixelColor(init, white);
strip.show();
}
delay(500);
uint16_t i = 0;
//Now, leds must change in pairs to green.
while(i <= 4)
{
strip.setPixelColor(0+i, green); // From first to fourth led
strip.setPixelColor(8-i, green); // From eighth to fifth led
strip.show();
delay(waiting);
i++;
}
colorWipe(off, 0); // Off
delay(500);
i = 0;
while(i < 3)
{
for(uint16_t init = 0; init < 9; init++)
{
strip.setPixelColor(init, green);
strip.show();
}
delay(onesecond);
colorWipe(off, 0); // Off
delay(500);
i++;
}*/
}
void turnRightDiagonalUp()
{
/* uint32_t white = strip.Color(255, 255, 255); //White
uint32_t green = strip.Color(0, 255, 0); //Green
int off = strip.Color(0, 0, 0); //off
// Turn on all leds in white
for(uint16_t init = 0; init < 5; init++)
{
strip.setPixelColor(init, white);
strip.show();
}
delay(500);
uint16_t i = 0;
//Now, leds must change in pairs to green.
while(i <= 2)
{
strip.setPixelColor(0+i, green); // From first to third led
strip.setPixelColor(4-i, green); // From fifth to fourth led
strip.show();
delay(waiting);
i++;
}
colorWipe(off, 0); // Off
delay(500);
i = 0;
while(i < 3)
{
for(uint16_t init = 0; init < 5; init++)
{
strip.setPixelColor(init, green);
strip.show();
}
delay(onesecond);
colorWipe(off, 0); // Off
delay(500);
i++;
}*/
}
void turnRightDiagonalDown()
{
/* uint32_t white = strip.Color(255, 255, 255); //White
uint32_t green = strip.Color(0, 255, 0); //Green
int off = strip.Color(0, 0, 0); //off
// Turn on all leds in white
for(uint16_t init = 4; init < 9; init++)
{
strip.setPixelColor(init, white);
strip.show();
}
delay(500);
uint16_t i = 0;
//Now, leds must change in pairs to green.
while(i <= 2)
{
strip.setPixelColor(4+i, green); // From fourth to sixth led
strip.setPixelColor(8-i, green); // From eighth to seventh led
strip.show();
delay(waiting);
i++;
}
colorWipe(off, 0); // Off
delay(500);
i = 0;
while(i < 3)
{
for(uint16_t init = 4; init < 9; init++)
{
strip.setPixelColor(init, green);
strip.show();
}
delay(onesecond);
colorWipe(off, 0); // Off
delay(500);
i++;
}*/
}
void turnLeft()
{
uint32_t white = strip.Color(255, 255, 255); //White
uint32_t green = strip.Color(0, 255, 0); //Green
int off = strip.Color(0, 0, 0); //off
// Turn on all leds in white
for (uint16_t init = 16; init >= 8; init--)
{
if(init == 16)
{
strip.setPixelColor(0, white);
}
else
{
strip.setPixelColor(init, white);
}
strip.show();
}
delay(500);
uint16_t i = 16;
// Now, leds must change in pairs to green.
while(i >= 12)
{
if(i == 16)
{
strip.setPixelColor(0, green); // Just for first
}
else
{
strip.setPixelColor(0+i, green); // From fifteenth to twelfth led
}
strip.setPixelColor(24-i, green); // From eighth to eleventh led
strip.show();
delay(waiting);
i--;
}
colorWipe(off, 0); // Off
delay(500);
i = 0;
while(i < 3)
{
for(uint16_t init = 16; init >= 8; init--)
{
if(init == 16)
{
strip.setPixelColor(0, green);
}
else
{
strip.setPixelColor(init, green);
}
strip.show();
}
delay(onesecond);
colorWipe(off, 0); // Off
delay(500);
i++;
}
}
void turnLeftDiagonalUp()
{
uint32_t white = strip.Color(255, 255, 255); //White
uint32_t green = strip.Color(0, 255, 0); //Green
int off = strip.Color(0, 0, 0); //off
// Turn on all leds in white
for (uint16_t init = 16; init >= 12; init--)
{
if(init == 16)
{
strip.setPixelColor(0, white);
}
else
{
strip.setPixelColor(init, white);
}
strip.show();
}
delay(500);
uint16_t i = 16;
// Now, leds must change in pairs to green.
while(i >= 14)
{
if(i == 16)
{
strip.setPixelColor(0, green); // Just for first
}
else
{
strip.setPixelColor(0+i, green); // From fifteenth to fourteenth led
}
strip.setPixelColor(28-i, green); // From twelfth to thirteenth led
strip.show();
delay(waiting);
i--;
}
colorWipe(off, 0); // Off
delay(500);
i = 0;
while(i < 3)
{
for(uint16_t init = 16; init >= 12; init--)
{
if(init == 16)
{
strip.setPixelColor(0, green);
}
else
{
strip.setPixelColor(init, green);
}
strip.show();
}
delay(onesecond);
colorWipe(off, 0); // Off
delay(500);
i++;
}
}
void turnLeftDiagonalDown()
{
uint32_t white = strip.Color(255, 255, 255); //White
uint32_t green = strip.Color(0, 255, 0); //Green
int off = strip.Color(0, 0, 0); //off
// Turn on all leds in white
for(uint16_t init = 8; init < 13; init++)
{
strip.setPixelColor(init, white);
strip.show();
}
delay(500);
uint16_t i = 0;
// Now, leds must change in pairs to green.
while(i <= 2)
{
strip.setPixelColor(12-i, green); // From twelfth to tenth led
strip.setPixelColor(8+i, green); // From eighth to ninth led
strip.show();
delay(waiting);
i++;
}
colorWipe(off, 0); // Off
delay(500);
i = 0;
while(i < 3)
{
for(uint16_t init = 12; init >= 8; init--)
{
strip.setPixelColor(init, green);
strip.show();
}
delay(onesecond);
colorWipe(off, 0); // Off
delay(500);
i++;
}
}
void turnOnU()
{
uint32_t white = strip.Color(255, 255, 255); //White
uint32_t green = strip.Color(0, 255, 0); //Green
int off = strip.Color(0, 0, 0); //off
// Turn on all leds in white
for(uint16_t init = 4; init < 9; init++)
{
strip.setPixelColor(init, white);
strip.show();
}
delay(500);
uint16_t i = 0;
//Now, leds must change in pairs to green.
while(i <= 2)
{
strip.setPixelColor(4+i, green); // From fourth to sixth led
strip.setPixelColor(8-i, green); // From eighth to seventh led
strip.show();
delay(waiting);
i++;
}
colorWipe(off, 0); // Off
delay(500);
i = 0;
while(i < 3)
{
for(uint16_t init = 4; init < 9; init++)
{
strip.setPixelColor(init, green);
strip.show();
}
delay(onesecond);
colorWipe(off, 0); // Off
delay(500);
i++;
}
}
void commingCall()
{
/* uint32_t c = strip.Color(0, 0, 255); //Blue
int off = strip.Color(0, 0, 0); //off
int wait = 170;
strip.setPixelColor(7, c);
strip.setPixelColor(9, c);
strip.show();
delay(wait);
colorWipe(off, 0); // Off
strip.setPixelColor(6, c);
strip.setPixelColor(10, c);
strip.show();
delay(wait);
colorWipe(off, 0); // Off
strip.setPixelColor(8, c);
strip.show();
delay(wait);
colorWipe(off, 0); // Off*/
}
void dangerZone()
{
/* uint32_t c = strip.Color(255, 0, 0); //Red
int off = strip.Color(0, 0, 0); //off
int wait = 250;
strip.setPixelColor(6, c);
strip.setPixelColor(8, c);
strip.setPixelColor(10, c);
strip.show();
delay(wait);
colorWipe(off, 0); // Off */
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait)
{
for (uint16_t i = 0; i < strip.numPixels(); i++)
{
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
int shouldBeOff(int i, int l)
{
if ((l < strip.numPixels() - 2) && (i >= l && i <= l + 2))
{
/* | 0 1 2 | 3 4 5 6 7 8 9 10 11 12 13 14 15 ----> 0 1 2 3 4 5 6 7 8 9 10 11 12 | 13 14 15 | */
return 1;
}
if (l == strip.numPixels() - 2 && (i == l || i == l + 1 || i == 0))
{
/* 0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 | 14 15 */
return 1;
}
if ((l == strip.numPixels() - 1) && (i == l || i == 0 || i == 1))
{
/* 0 1 | 2 3 4 5 6 7 8 9 10 11 12 13 14 | 15 */
return 1;
}
return 0;
}
void loading(int times)
{
for (int h = 0; h < times; h++)
{
for (int x = 0; x < strip.numPixels(); x++)
{
int wait = 100;
for (int i = 0; i < strip.numPixels(); i++)
{
if (shouldBeOff(i, loadingIndex))
{
strip.setPixelColor(i, strip.Color(0, 0, 0));
}
else
{
strip.setPixelColor(i, strip.Color(0, 0, 255));
}
}
strip.show();
delay(wait);
loadingIndex++;
if (loadingIndex == strip.numPixels())
{
loadingIndex = 0;
}
}
}
}
// 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)
{
WheelPos = 255 - WheelPos;
if (WheelPos < 85)
{
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170)
{
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment