#define trig 3
#define echo 2
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUMPIXELS 16
Adafruit_NeoPixel pixels (NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
pinMode (trig,OUTPUT);
digitalWrite(trig,LOW);
pinMode (echo,INPUT);
Serial.begin(9600);
pixels.begin();
}
void loop()
{
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
long t = pulseIn(echo,HIGH);
float distance = t * 0.01715;
Serial.println("Distance = " + String(distance)+"cm");
delay(100);
if (distance > 0 and distance < 100 )
{
for (int i=0; i<NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(255, 0, 0));
pixels.show();
}
}
if (distance > 100 and distance < 200 )
{
for (int i=0; i<NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(0, 0, 255));
pixels.show();
}
}
if (distance > 200 and distance < 300 )
{
for (int i=0; i<NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(0, 255, 0));
pixels.show();
}
}
if (distance > 300 and distance < 400 )
{
for (int i=0; i<NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(160, 32, 240));
pixels.show();
}
}
}