1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
#include <Adafruit_NeoPixel.h> #define LED_PIN 3 #define LED_COUNT 8 Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); #define Trig A4 #define Echo A5 #define PIEZO A3 long distances=0; int cnt=0; void setup() { strip.begin(); strip.show(); strip.setBrightness(5); Serial.begin(9600); pinMode(Trig, OUTPUT); pinMode(Echo, INPUT); } void loop() { digitalWrite(Trig, LOW); delayMicroseconds(2); digitalWrite(Trig, HIGH); delayMicroseconds(10); digitalWrite(Trig, LOW); distances=(pulseIn(Echo, HIGH) / 29.1) / 2.0; Serial.print("Distance : "); Serial.println(distances); cnt=map(distances, 0, 100, 1, 8); if (distances < 10) { colorWipe(strip.Color(255, 0, 0)); tone(PIEZO, 330); delay(200); } else if (10 <= distances && distances < 30) { colorWipe(strip.Color(255, 255, 0)); tone(PIEZO, 330); delay(distances*2); noTone(PIEZO); delay(distances*2); } else if (30 <= distances && distances < 100) { colorWipe(strip.Color(0, 255, 0)); tone(PIEZO, 330); delay(100); noTone(PIEZO); delay(100); } else { colorWipe(strip.Color(0, 0, 0)); noTone(PIEZO); delay(200); } } void colorWipe(uint32_t color) { for(int i=0; i<strip.numPixels(); i++) { strip.setPixelColor(i, color); } strip.show(); } |