Thanks again for your reply.
Alright, below is the current code I have so far, incomplete as I am still figuring it out.
I am using the sonic HC-SR04 sensor.
ULTIMATE GOAL the project will work that 8 LED diodes and the Adafruit Neopixel Ring 16 will be fully lit, static, when the sensor is "undisturbed". When it senses a minimum distance of 36 inches - it calls the rainbow function from the strandtest PLUS (not currently in my code) randomly blink the 8 LED diodes. Then, when the distance is once again "cleared" all lighting components (8 LEDS and NeoPixel Ring) elegantly fade back to static.
Code: Select all
// Adafruit Pixel Ring
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#endif
#define LED_PIN 4
#define LED_COUNT 16
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Ultrasoninc Sensor HC-SR04
const int trigPin = 12;
const int echoPin = 11;
long duration, inches;
// LEDs info here
void setup() {
//Neopixel dataPin
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
//Serial Port
Serial.begin(9600);
}
void loop(){
// Ultrasoninc Sensor HC-SR04 calculate distance
pinMode (trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds (2);
digitalWrite (trigPin,HIGH);
delayMicroseconds (10);
digitalWrite (trigPin, LOW);
pinMode (echoPin, INPUT);
noInterrupts();
duration = pulseIn (echoPin, HIGH);
interrupts();
inches = microsecondsToInches(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.println();
delay(20);
if (inches > 36) {
colorWipe(strip.Color( 0, 0, 255), 50); // Blue;
}
else {
rainbow (1);
}
}
//Variable for Inches Conversion
long microsecondsToInches(long microseconds) {
return microseconds / 74 / 2;
}
//Color wheel
void rainbow (int wait) {
for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
strip.rainbow(firstPixelHue);
strip.show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}
// Fill strip pixels Blue
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}