Created
October 3, 2019 18:53
-
-
Save jakabo27/f9a892f59625bbcefc7b438a57761a14 to your computer and use it in GitHub Desktop.
Light Stop Game for Clemson Makerspace Hackerday 2019
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FastLED Setup | |
#include <FastLED.h> | |
#define NUM_LEDS 40 | |
#define DATA_PIN 5 | |
#define BUTTON_PIN 8 | |
#define MOVINGSPOT CRGB(0,0,255); | |
CRGB leds[NUM_LEDS]; | |
volatile boolean waitingForButton = true; | |
byte globalBright = 60; //The max brightness for the strip | |
byte levelSpeeds[] = {200,150,100,50,40,30,20,15,14,13,12,10,9,8,7,6,5,4,3,2,1,0}; | |
byte numLevels = 22; | |
byte currentLevel = 0; | |
byte stopPosition; | |
byte dullValue = 20; //The brightness of the dull amount | |
int currentSpot; | |
unsigned long restTime, loopStart, restTimeout; | |
void setup() { | |
//FastLED setup | |
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); | |
FastLED.setCorrection(TypicalLEDStrip); | |
//FastLED.setMaxPowerInVoltsAndMilliamps(5,maxMilliamps); | |
FastLED.setBrightness(globalBright); | |
fill_solid(leds,NUM_LEDS, CRGB::Black); //Turn off all the lights | |
//Initialize digital inputs/outputs | |
pinMode(BUTTON_PIN,INPUT); | |
//Start Serial port | |
Serial.begin(9600); | |
} | |
void loop() { | |
//Set strip to a nice dull color in the color spectrum | |
byte currentHue = map(currentLevel,0,numLevels,0,255); | |
fill_solid(leds,NUM_LEDS,CHSV( currentHue, 255, dullValue)); | |
//Determine the stop position and set it to bright white | |
stopPosition = random(0,NUM_LEDS); | |
leds[stopPosition] = CRGB(255,255,255); | |
FastLED.show(); | |
Serial.print("Current level: "); | |
Serial.println(currentLevel); | |
//Run a single light down the strip until the button is pressed | |
while(waitingForButton) | |
{ | |
currentSpot++; | |
leds[currentSpot-1] = CHSV( currentHue, 255, dullValue); //Fill the previous spot with the dull color it should be | |
if(currentSpot >= NUM_LEDS) currentSpot = 0; | |
leds[currentSpot] = MOVINGSPOT; //Set it to whatever the moving spot should look like | |
leds[stopPosition] = CRGB(255,255,255);//Set this back to the right color in case we overwrote it | |
FastLED.show(); | |
//Wait the amount of time while looking for a button press | |
loopStart = millis(); | |
restTime = levelSpeeds[currentLevel]; //Change how long we're waiting based on the level | |
bool buttonAnimate = true; | |
bool buttonPressed = 0; | |
restTimeout = 0; | |
while(buttonAnimate) | |
{ | |
restTimeout = millis() - loopStart; | |
if(digitalRead(BUTTON_PIN)==1) //Check if the button was pressed | |
{ buttonPressed = 1; | |
buttonAnimate = false; | |
} | |
if(restTimeout>=restTime) break; | |
} | |
if(buttonPressed) | |
{ | |
waitingForButton = false; | |
//Check if it was in the winning spot or not | |
bool correctSpot = false; | |
if(currentSpot == stopPosition) | |
correctSpot = true; | |
//WIN | |
if(correctSpot) | |
{ | |
Serial.println("Congrats!! You did the thing!!"); | |
//Set the spot you stopped on to green for good | |
leds[currentSpot] = CRGB(0,255,0); | |
FastLED.show(); | |
delay(1000); | |
currentLevel++; | |
} | |
//LOSE | |
else | |
{ | |
Serial.println("FAILURE") ; | |
//Set the spot you stopped to just red because bad | |
leds[currentSpot] = CRGB(255,0,0); | |
FastLED.show(); | |
delay(1000); | |
//Reset the level to 0 | |
currentLevel = 0; | |
} | |
} | |
else //Button was not pressed | |
{ | |
} | |
} | |
} | |
void all_on() { | |
fill_solid(leds,NUM_LEDS, CRGB(255,255,255)); | |
FastLED.show(); | |
} | |
void all_off() { | |
fill_solid(leds,NUM_LEDS, CRGB(0,0,0)); | |
FastLED.show(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment