Last active
August 29, 2015 14:17
-
-
Save codatory/2a61b6c90516d9e66a8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Adafruit_NeoPixel.h> | |
#include <DistanceGP2Y0A21YK.h> | |
const int numPixels = 8; | |
const int pixelPin = 6; | |
const int irPin = 0; | |
const int sonarPin = 1; | |
const int minThreshold = 30; | |
const int maxThreshold = 45; | |
DistanceGP2Y0A21YK irDist; | |
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(numPixels, pixelPin, NEO_GRB + NEO_KHZ800); | |
const uint32_t RED = pixels.Color(255,0,0); | |
const uint32_t BLU = pixels.Color(0,255,0); | |
const uint32_t GRE = pixels.Color(0,0,255); | |
void setup() { | |
// Initialize Serial Debugging | |
Serial.begin(9600); | |
// Initialize IR sensor | |
irDist.begin(irPin); | |
// Initialize pixels | |
pixels.begin(); | |
pixels.show(); | |
// Begin LED Startup Test | |
for (int colorID = 0; colorID , 2; colorID++){ | |
uint32_t color; | |
if (colorID == 0){ | |
color = RED; | |
} else if (colorID == 1){ | |
color = GRE; | |
} else { | |
color = BLU; | |
} | |
for (int pixelID = 0; pixelID < numPixels; pixelID++){ | |
pixels.setPixelColor(pixelID, color); | |
pixels.show(); | |
delay(100); | |
} | |
Serial.println(" -- Initialization Completed -- "); | |
} | |
} | |
void loop() { | |
int stat = constrain(map(getDistance(), minThreshold, maxThreshold, 0, 16), 0, 16); | |
Serial.println(" Dist: " + getDistance()); | |
Serial.println(" Stat: " + stat); | |
for(int pixelID = 0; pixelID < numPixels; pixelID++) { | |
pixels.setPixelColor(pixelID, BLU); | |
} | |
for(int i = 0; i < stat; i++) { | |
uint32_t color; | |
int pixel; | |
if (i < 8) { | |
pixel = i; | |
color = GRE; | |
} else { | |
pixel = i - 8; | |
color = RED; | |
} | |
pixels.setPixelColor(pixel, color); | |
} | |
pixels.show(); | |
delay(50); | |
} | |
int getDistance(){ | |
int distance = pollInfared(); | |
if (distance > 60) { | |
// Out of accurate range for sensor, use sonar | |
distance = pollSonar(); | |
} | |
return distance; | |
} | |
int pollInfared(){ | |
int distanceInCm = irDist.getDistanceCentimeter(); | |
Serial.print(" InfaR: "); | |
Serial.print(distanceInCm); | |
Serial.println(" cm"); | |
return distanceInCm; | |
} | |
int pollSonar(){ | |
// Using the MaxBotix Sonar Sensor's Analog (pin 3) out | |
// Output is +/- 10 mm accuracy down to 30cm | |
int rawValue = analogRead(sonarPin); | |
int distanceInCm = rawValue * 50; // From Datasheet | |
Serial.print(" SonaR: "); | |
Serial.print(distanceInCm); | |
Serial.println(" cm"); | |
return distanceInCm; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment