#include <Adafruit_NeoPixel.h>
int relay_D2 = 2;
int relay_D4 = 4;
#define brake_switch_pin 14
volatile int interruptCounter = 0;
// NeoPixel LED
#define left_neopixel_ring_brake 13
Adafruit_NeoPixel strip = Adafruit_NeoPixel(48, left_neopixel_ring_brake, NEO_GRB + NEO_KHZ800);
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.
void IRAM_ATTR handleInterrupt() {
interruptCounter++;
}
void init_relay_modules() {
pinMode(relay_D2, OUTPUT);
pinMode(relay_D4, OUTPUT);
}
void init_switches() {
pinMode(brake_switch_pin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(brake_switch_pin), handleInterrupt, FALLING);
}
void pulseLEDRing(uint32_t c, byte pulses, uint8_t wait, uint8_t brightness) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
for (int j = 0; j < pulses; j++) {
strip.setBrightness(brightness);
strip.show();
delay(wait);
strip.setBrightness(0);
strip.show();
delay(wait);
}
}
}
void brake_switch() {
static unsigned long prev_millis = 0;
unsigned long current_millis = millis();
int delay_time = 150UL;
int _interruptCounter = interruptCounter;
if (current_millis - prev_millis >= delay_time) {
if (interruptCounter > 0) {
interruptCounter = 0;
Serial.println("Brake Switch Pressed");
pulseLEDRing(strip.Color(255, 0, 0), 10, 2, 255);
}
prev_millis = current_millis;
}
}
void setup_hardware() {
init_switches();
init_relay_modules();
}
void colorWipe(uint32_t c, uint8_t wait, uint8_t brightness) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.setBrightness(brightness);
strip.show();
delay(wait);
}
}
void colorWipe2(uint32_t c, uint8_t wait) {
int n=int(strip.numPixels()/3);
for(uint16_t i=0; i<n; i++) {
strip.setPixelColor(i, c);
strip.setPixelColor(i+n, c);
strip.setPixelColor(i+2*n, c);
strip.show();
delay(wait);
}
}
void setup() {
// put your setup code here, to run once:
setup_hardware();
strip.begin();
strip.show(); // Initialize all pixels to 'off'
Serial.begin(115200);
Serial.println("Hello, ESP32!");
}
void loop() {
// put your main code here, to run repeatedly:
static bool led_init_brake_light = false;
if (led_init_brake_light == false) {
colorWipe(strip.Color(255, 0, 0), 30, 255); // Red
colorWipe2(strip.Color(255, 255, 0), 60); // Red
colorWipe(strip.Color(255, 0, 0), 30, 255); // Red
colorWipe(strip.Color(255, 0, 0), 5, 150); // Red
led_init_brake_light = true;
}
if (led_init_brake_light == true) {
brake_switch();
int read_brake_switch = digitalRead(brake_switch_pin);
if (read_brake_switch == LOW) {
Serial.println("Brake Switch Pressed");
colorWipe(strip.Color(255, 0, 0), 0, 255); // Red
// delay(100);
// interruptCounter++;
} else {
colorWipe(strip.Color(255, 0, 0), 0, 150); // Red
}
}
//digitalWrite(relay_D2, HIGH);
//delay(1000);
//digitalWrite(relay_D2, LOW);
//delay(1000);
//delay(10); // this speeds up the simulation
}