#include
#include
#include
#define LED_PIN 5 // Defines the pin where the LED is connected
#define LED_COUNT 7 // Defines the number of LEDs on the strip
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); // Initializes the LED strip
const PROGMEM int sampleWindow = 50; // Global Variables for the Microphone
unsigned int sample;
void setup() {
strip.begin(); // Signals to the LED strip to start by tuning off all the LEDs
strip.show(); // Applies previous command to LED strip
Serial.begin(9600); // Establishes a 9600 bit/second rate of transfer for microphone
}
void loop() {
for (int i = 0; i < EEPROM.length(); i++) { // Clears EEPROM memory
EEPROM.write(i, 0);
}
unsigned long startMillis= millis(); // 19-37 is for Microphone readings
unsigned int peakToPeak = 0;
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
microphoneReadings(startMillis, peakToPeak, signalMax, signalMin);
if (peakToPeak >= 10) { // If Microphone detects a signal above 10, the LED strip turns on
strip.setPixelColor(3,255,255,255); // Initial white DRL with beginning animation
strip.show();
delay(75);
int *a = 4;
a = (int*) malloc(1);
int *b = 2;
b = (int*) malloc(1);
for (int i = 0; i < 3; i++) {
strip.setPixelColor(a,255,255,255);
strip.setPixelColor(b,255,255,255);
strip.show();
delay(75);
a += 1;
b -= 1;
}
free(a);
free(b);
delay(125);
full_LED(255,255,255);
delay(5000);
clearShow(); // Left and Right Turn Signals
turnSignal(0); // Right Turn Signal
turnSignal(1); // Left Turn Signal
full_LED(255,0,0); // Brake Lights
delay(5000);
brakeAnimation(); // Brake Lights with Ending Animation
full_LED(255,0,0);
brakeAnimation();
}
Serial.println(peakToPeak);
}
void brakeAnimation(void) {
int *c = 6;
c = (int*) malloc(1);
int *d = 0;
d = (int*) malloc(1);
for (int i = 0; i < 3; i++) {
strip.setPixelColor(c,0,0,0);
strip.setPixelColor(d,0,0,0);
strip.show();
delay(75);
c -= 1;
d += 1;
}
free(c);
free(d);
clearShow();
}
int microphoneReadings(long startMillis, int peakToPeak, int signalMax, int signalMin) {
while (millis() - startMillis < sampleWindow) {
sample = analogRead(0);
if (sample < 1024)
{
if (sample > signalMax) {
signalMax = sample;
} else if (sample < signalMin) {
signalMin = sample;
}
}
}
peakToPeak = signalMax - signalMin;
}
void full_LED(long color1, long color2,long color3) {
for (int j = 0; j < 7; j++) {
strip.setPixelColor(j,color1, color2, color3);
strip.show();
}
}
void clearShow(void) {
strip.clear();
strip.show();
}
void turnSignal(int left_right) {
if (left_right == 0) {
for (int i = 3; i < 7; i++) {
middleTurn();
strip.setPixelColor(i,255,255,0);
strip.show();
delay(100);
clearShow();
}
} else {
for (int j = 3; j > -1; j--) {
middleTurn();
strip.setPixelColor(j,255,255,0);
strip.show();
delay(100);
clearShow();
}
}
}
void middleTurn(void) {
strip.setPixelColor(3,0,0,0);
strip.show();
delay(100);
}
© 2022 Mustafa Omran