#include <Adafruit_NeoPixel.h>
#define PIN 13
#define KEYPIN 2
Adafruit_NeoPixel horse = Adafruit_NeoPixel(64, PIN, NEO_GRB + NEO_KHZ800);
uint32_t horseCount,lastRead,flashCount;
uint8_t key,previous;
void setup() {
Serial.begin(115200);
horseCount=0xfc000000; //0b11111100000000000000000000000000;
horse.begin();
horse.show();
pinMode(KEYPIN,INPUT_PULLUP);
lastRead=0;
displayHorse();
}
void loop(){
if((millis()-lastRead)>20){ //read key every 20mS
lastRead=millis();
key=digitalRead(KEYPIN);
if(key==0 && previous==1){
progressHorse(); //move horse along
displayHorse(); //update the display
flashCount=millis(); //just to ensure the flash starts lit
}
previous=key; //keep a copy for next read
}
if(horseCount==0xfffffffc){ //all 30 LEDs lit?
if((millis()-flashCount)>50){ //flash the display at 10Hz
clearDisplay();
}
if((millis()-flashCount)>100){ //the 50 and 100 control the flash rate
displayHorse();
flashCount=millis();
}
}
}
void progressHorse(){ //move the horse on
if(horseCount == 0xfffffffc){ //or if completed
horseCount=0xfc000000; //reset to first 6 LEDs
return;
}
horseCount>>=1;
horseCount|=0x80000000;
}
void displayHorse(){
uint32_t temp=horseCount;
uint8_t i;
for(i=0;i<32;i++){
if(temp&0x80000000)
horse.setPixelColor(i,horse.Color(25,25,25));
else
horse.setPixelColor(i,horse.Color(0,0,0));
temp<<=1;
}
for(i=0;i<32;i++)
horse.setPixelColor(i+32,horse.Color(0,0,0));
horse.show();
}
void clearDisplay(){
uint8_t i;
for(i=0;i<64;i++)
horse.setPixelColor(i,horse.Color(0,0,0));
horse.show();
}