I familiar enough about coding to know that I should be using a variable for the pixels and i++ them to make the code cleaner, but not enough to do that properly. Any help would be very appreciated. I'm using Flora and the Maxbotix EZ1. Also I'm using this sensor over others because it aesthetically fits better in the not of the tie more than a sharp, ping or other. Thanks for the help and I look forward to any suggestions.
Code: Select all
#include <Adafruit_NeoPixel.h>
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, 6, NEO_GRB + NEO_KHZ400);
int sonarPin = 9; //pin connected to analog out on maxsonar sensor
int inchesAway; // inches away from the maxsonar sensor
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
Serial.begin(9600);
}
void loop() {
inchesAway = analogRead(sonarPin) /2; // reads the maxsonar sensor and divides the value by 2
// approximate distance in inches
Serial.print(inchesAway); // prints the sensor information from the maxsonar to the serial monitor
Serial.println(" inches from sensor");
if (inchesAway < 19) { // if something is 24 inches away then make a 1khz sound
// turn LED on:
strip.setPixelColor(0, strip.Color(250, 0, 0));
strip.show();
strip.setPixelColor(1, strip.Color(250, 0, 0));
strip.show();
strip.setPixelColor(2, strip.Color(250, 0, 0));
strip.show();
strip.setPixelColor(3, strip.Color(250, 0, 0));
strip.show();
delay(100);
strip.setPixelColor(0, strip.Color(0, 0, 0));
strip.show();
strip.setPixelColor(1, strip.Color(0, 0, 0));
strip.show();
strip.setPixelColor(2, strip.Color(0, 0, 0));
strip.show();
strip.setPixelColor(3, strip.Color(0, 0, 0));
strip.show();
delay(100);
}
else if (inchesAway > 18){
strip.setPixelColor(1, strip.Color(0, 0, 0));
}
}