#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#include <EEPROM.h>
#define PIN 12
#define BUTTON_PIN 11
#define echoPin 10
#define trigPin 9
#define max_dist 300
#define set_loc 1
#define NUM_LEDS 9
#define BRIGHTNESS 50
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRBW + NEO_KHZ800);
//Initialize glabal variables
int set_dist;
float duration, distance;
void setup() {
// Initialize neopixels
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
strip.setBrightness(BRIGHTNESS);
strip.begin();
strip.show();
// Initialize SR04
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize set_dist
set_dist = EEPROM.read(set_loc);
// Set for testing
// set_dist = 10;
Serial.begin(9600);
// This fixes a problem with the button reading high on startup
while(digitalRead(BUTTON_PIN) == LOW){
}
}
void set_lights(uint8_t distance) {
int total_distance = max_dist - set_dist;
/*
We will fill up the colors first with blue,
then green, then red when too far.
*/
// Deterimne dist from set_dist
int dif = distance - set_dist;
int half_dist = (float) total_distance / 2;
if (dif > half_dist) {
// Set lights to be blue
int to_lite = ((float) dif / half_dist - 1) * NUM_LEDS;
for (int i = 0; i < NUM_LEDS; i++) {
if (i > to_lite) {
strip.setPixelColor(i, strip.Color(0, 0, 255, 0));
} else {
strip.setPixelColor(i, strip.Color(0, 0, 0, 0));
}
}
strip.show();
return;
}
if (dif > 0) {
// Set lights to be blue and green
int to_lite = ((float) dif / half_dist) * NUM_LEDS;
for (int i = 0; i < NUM_LEDS; i++) {
if (i > to_lite) {
strip.setPixelColor(i, strip.Color(0, 255, 0, 0));
} else {
strip.setPixelColor(i, strip.Color(0, 0, 255, 0));
}
}
strip.show();
return;
}
if (dif <= 0) {
// Set all lights to red
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, strip.Color(255, 0, 0, 0));
}
strip.show();
}
Serial.println(dif);
// Setup button
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
int get_distance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration * .0343) / 2;
return (int) distance;
}
void flash(uint32_t c){
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, c);
}
strip.show();
delay(1000);
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0, 0));
}
strip.show();
}
void loop() {
// put your main code here, to run repeatedly:
set_lights(get_distance());
delay(100);
if (digitalRead(BUTTON_PIN) == LOW){
flash(strip.Color(0,0,0,255));
delay(1000);
flash(strip.Color(0,0,0,255));
delay(1000);
flash(strip.Color(255,0,0,0));
set_dist = get_distance();
EEPROM.update(set_loc, set_dist);
}
}
Comments