/*
Made by PedroCorreia
October 2024
*/

#include <Ultrasonic.h>
#include <FastLED.h>

#define NUM_LEDS 64
#define DATA_PIN 2
#define COLOR_ORDER GRB
#define CHIPSET WS2812B

#define VOLTS 5
#define MAX_MAMPS 250

#define BRIGHTENESS 100
#define CORRECTION_FACTOR Halogen//Tungsten100W //UncorrectedColor
#define COLOR_TEMPERATURE Candle //
#define KELVIN2700COLOR 0xFFA957
#define TESTCOLOR 0xFFA957


CRGB leds[NUM_LEDS];


Ultrasonic ultrasonic(9, 8); //(TRIG, ECHO)

void setup() {

  FastLED.addLeds<CHIPSET,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps(VOLTS, MAX_MAMPS);
  FastLED.setCorrection(CORRECTION_FACTOR);
  FastLED.setTemperature(COLOR_TEMPERATURE);
  FastLED.setBrightness(BRIGHTENESS);
  FastLED.clear();
  FastLED.show();

  //ultrasonic.setTimeout(40000UL);
  ultrasonic.setTimeout(20000UL);
  
}

void loop() {
  int distance = ultrasonic.read();
  if (distance <= 250) { //Jack o' Sentinel
    flash_all(180, 3);//(ms delay, cycles)
    flash_all(250, 2);
    turn_on_all(TESTCOLOR);
    delay(3000);
  }
  else {
    turn_off_all(CRGB::Black);
  }
  delay(10);
}

void flash_all(int dVal, int cycles){ //(ms delay/half period, cycles)
  for(int i = 0 ;i <= cycles-1; i++){
    turn_on_all(TESTCOLOR);
    delay(dVal);
    turn_off_all(CRGB::Black);
    delay(dVal);
  }
}

void turn_on_all(CRGB color){
    for(int i = 0 ;i <= NUM_LEDS-1; i++){
     leds[i] = color;
  }
  FastLED.show();
}

void turn_off_all(CRGB color){
  for(int i = NUM_LEDS-1; i >= 0; i--){
     leds[i] = color;
  } 
  FastLED.show();
}
