#include "FastLED.h"
const int buttonPin = 2;
int ledPin = 13;
const int Beacon = 5;
#define PIN 4
int flag = 0;
int sflag = 0;
#define NUM_LEDS 16
#define COLOR_ORDER GRB
int fadeAmount = 20;
int brightness = 0;
int reset = 0;
CRGB leds[NUM_LEDS];
enum State {
IDLE,
START,
ONE,
TWO,
WON,
LOST,
};
State state = IDLE;
int i = 0;
int a = 0;
volatile int buttonState = 0; // variable for reading the pushbutton status
void setup() {
Serial.begin(115200);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
// Attach an interrupt to the ISR vector
attachInterrupt(0, pin_ISR, CHANGE);
FastLED.addLeds<WS2812B, PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.clear();
FastLED.show();
}
void loop() {
delay(40);
Serial.println(state);
if ( (digitalRead(buttonPin) == LOW) && (flag == 0) && (sflag == 0)) //change button state to
{
flag = 1;
sflag = 1;
delay(50);
}
switch (state) {
case IDLE:
i = 0;
Serial.println("Idle");
delay(40);
state = START;
break;
case START:
Serial.println("no button pressed"); // endless loop until button pressed for first time
break;
case ONE:
Serial.println(" button pressed");
delay(2000);
flag = 1; //change button state to pressed once
sflag = 0;
delay(50);
state = TWO;
break;
case TWO:
for (int i = 0; i < NUM_LEDS; i++) // turn on 1 LED at a time using fade in (brightness increase)
{
for (int brightness = 0; brightness < 160;) // increase brightness from 0 to 160
{
leds[i].setRGB(40, 160, 80);
leds[i].maximizeBrightness(brightness);
FastLED.show();
brightness = brightness + fadeAmount;
delay(50);
}
for (int a = 0; a < 12;) { // LED pause 510ms before going on with the next
a++;
brightness = reset; //reset brightness to 0
delay(5);
}
a=0;
}
state = LOST;
break;
case WON:
Serial.println("WON"); // button has been pressed twice WON
FastLED.clear();
FastLED.show();
flag = 0; //reset button
sflag = 0;
delay(5000);
delay(2000);
state = IDLE;
break;
case LOST:
digitalWrite(Beacon, HIGH); // button was not pressed again LOST Beacon light turns on
flag = 0; //reset button
sflag = 0;
Serial.println("LOST!!");
delay(10000);
digitalWrite(Beacon, LOW);
FastLED.clear();
FastLED.show();
delay(50);
state = IDLE;
break;
}
}
void pin_ISR() {
if ( (digitalRead(buttonPin) == HIGH) && (flag == 1) && (sflag == 1)) // if there is nothing in front of the sensor
{
state = ONE;
}
if ( (digitalRead(buttonPin) == LOW) && (flag == 1) && (sflag == 0))
{
state = WON;
}
}