#include "FastLED.h"
#include "Target.h"
// Which pin on the Arduino is connected to the NeoPixels?
#define LED_PIN 5
#define LED_COUNT 40
#define LED_TYPE WS2812
#define LED_GLOBAL_BRIGHTNESS 255
#define LED_CORRECTION TypicalLEDStrip
CRGB leds[LED_COUNT];
#define TGT_COUNT 1
bool gameOver = false;
//States declaration
enum class targetStateOLD : uint8_t {
INACTIVE,
ACTIVE,
SHOT
};
//Target setup # pin minLed maxLed
Target t_1( 1, 34, 0, 4 );
Target t_2( 2, 35, 5, 9 );
Target t_3( 3, 32, 10, 14 );
Target t_4( 4, 33, 15, 19 );
Target t_5( 5, 25, 20, 24 );
Target t_6( 6, 26, 25, 29 );
Target t_7( 7, 27, 30, 34 );
Target t_8( 8, 14, 35, 39 );
void setup() {
Serial.begin(9600);
//Strip initialization
FastLED.addLeds<LED_TYPE, LED_PIN>(leds, LED_COUNT);
FastLED.setBrightness(LED_GLOBAL_BRIGHTNESS);
FastLED.setCorrection(LED_CORRECTION);
//initialize all leds to off
for (int i = 0; i < LED_COUNT; i++) {
leds[i] = CHSV(0, 0, 0);
}
//Target init
t_1.hue = 0;
t_2.hue = 30;
t_3.hue = 60;
t_4.hue = 90;
t_5.hue = 120;
t_6.hue = 150;
t_7.hue = 180;
t_8.hue = 210;
//Display
FastLED.show();
//Initial random time to wait before the timer static_assert
delay(random(1500,5000));
}
void loop() {
//for(int i=0; i < LED_COUNT; i++){
// leds[i] = CHSV(hue+10*i, saturation, value);
//}
//EVERY_N_MILLISECONDS(7) {
// hue++;
// }
static unsigned long timer = millis();
//Process the target states
t_1.process();
t_2.process();
t_3.process();
t_4.process();
t_5.process();
t_6.process();
t_7.process();
t_8.process();
//Update led values for each target
{
for (int i = t_1.minLed ; i < t_1.maxLed + 1 ; i++ ){
leds[i] = CHSV(t_1.hue, t_1.saturation, t_1.value);
}
for (int i = t_2.minLed ; i < t_2.maxLed + 1 ; i++ ){
leds[i] = CHSV(t_2.hue, t_2.saturation, t_2.value);
}
for (int i = t_3.minLed ; i < t_3.maxLed + 1 ; i++ ){
leds[i] = CHSV(t_3.hue, t_3.saturation, t_3.value);
}
for (int i = t_4.minLed ; i < t_4.maxLed + 1 ; i++ ){
leds[i] = CHSV(t_4.hue, t_4.saturation, t_4.value);
}
for (int i = t_5.minLed ; i < t_5.maxLed + 1 ; i++ ){
leds[i] = CHSV(t_5.hue, t_5.saturation, t_5.value);
}
for (int i = t_6.minLed ; i < t_6.maxLed + 1 ; i++ ){
leds[i] = CHSV(t_6.hue, t_6.saturation, t_6.value);
}
for (int i = t_7.minLed ; i < t_7.maxLed + 1 ; i++ ){
leds[i] = CHSV(t_7.hue, t_7.saturation, t_7.value);
}
for (int i = t_8.minLed ; i < t_8.maxLed + 1 ; i++ ){
leds[i] = CHSV(t_8.hue, t_8.saturation, t_8.value);
}
}
//display
FastLED.show();
if (countActive() == 0 && !gameOver) {
//stop the timer
double result = millis() - timer;
result = round(result/10);
result = result/100;
//Display result = amount of time necessary to shot all targets
Serial.println(F(""));
Serial.println(F("*************************************"));
Serial.println(F(" Game over ! "));
Serial.println(F("*************************************"));
Serial.println(F(""));
Serial.print (F("Score : "));
Serial.print (result);
Serial.println(F("s"));
gameOver = true;
}
}
int countActive() {
uint8_t currCount = 0;
if (t_1.currState == targetState::ACTIVE) { currCount++;}
if (t_2.currState == targetState::ACTIVE) { currCount++;}
if (t_3.currState == targetState::ACTIVE) { currCount++;}
if (t_4.currState == targetState::ACTIVE) { currCount++;}
if (t_5.currState == targetState::ACTIVE) { currCount++;}
if (t_6.currState == targetState::ACTIVE) { currCount++;}
if (t_7.currState == targetState::ACTIVE) { currCount++;}
if (t_8.currState == targetState::ACTIVE) { currCount++;}
return currCount;
}