You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
1.4 KiB
81 lines
1.4 KiB
4 years ago
|
#include <Adafruit_NeoPixel.h>
|
||
|
|
||
|
#define DIN 6
|
||
|
#define LEDS 50
|
||
|
#define SPEED 40 // 0-50
|
||
|
#define TRIG 4
|
||
|
#define ECHO 5
|
||
|
#define TRIG_DIST 100 // min trigger distance
|
||
|
#define SOUND 7
|
||
|
|
||
|
long duration, cm;
|
||
|
long oldDist = 0;
|
||
|
|
||
|
Adafruit_NeoPixel led = Adafruit_NeoPixel(LEDS, DIN, NEO_GRB + NEO_KHZ800);
|
||
|
|
||
|
void setup() {
|
||
|
Serial.begin(9600);
|
||
|
led.begin();
|
||
|
led.show();
|
||
|
pinMode(TRIG, OUTPUT);
|
||
|
pinMode(ECHO, INPUT);
|
||
|
pinMode(SOUND, OUTPUT);
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
|
||
|
digitalWrite(TRIG, LOW);
|
||
|
delayMicroseconds(2);
|
||
|
digitalWrite(TRIG, HIGH);
|
||
|
delayMicroseconds(10);
|
||
|
digitalWrite(TRIG, LOW);
|
||
|
duration = pulseIn(ECHO, HIGH);
|
||
|
cm = duration / 58;
|
||
|
|
||
|
Serial.println(cm);
|
||
|
|
||
|
if(cm <= TRIG_DIST) {
|
||
|
|
||
|
Serial.print(TRIG_DIST);
|
||
|
Serial.print("\t");
|
||
|
|
||
|
digitalWrite(SOUND, HIGH);
|
||
|
delay(100);
|
||
|
|
||
|
|
||
|
for(int d=0; d<20; d++){
|
||
|
led.setBrightness(255);
|
||
|
|
||
|
for(int i=0; i<=LEDS; i++) {
|
||
|
led.setPixelColor(i, 255, 255, 255);
|
||
|
led.show();
|
||
|
if(oldDist <= TRIG_DIST) {
|
||
|
delay((50-(d/2))-SPEED);
|
||
|
} else {
|
||
|
delay(5);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(oldDist <= TRIG_DIST) {
|
||
|
for(int i=0; i<=LEDS; i++) {
|
||
|
led.setPixelColor(0, 0, 0, 0);
|
||
|
led.setBrightness(0);
|
||
|
led.show();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
oldDist = cm;
|
||
|
|
||
|
} else {
|
||
|
|
||
|
for(int i=0; i<=LEDS; i++) {
|
||
|
led.setPixelColor(0, 0, 0, 0);
|
||
|
led.setBrightness(0);
|
||
|
led.show();
|
||
|
digitalWrite(SOUND, LOW);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|