Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_NeoPixel.h>
- const int trigPin = 9;
- const int echoPin = 10;
- const int buzzer = 11;
- #define dinPin 4
- #define numLEDs 5
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(numLEDs, dinPin, NEO_GRB + NEO_KHZ800);
- long duration;
- int distance;
- int blue;
- int green;
- int off;
- boolean lightLED = false;
- void setup() {
- strip.begin();
- strip.setBrightness(80);
- strip.show();
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- pinMode(buzzer, OUTPUT);
- Serial.begin(9600);
- }
- void loop() {
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distance = duration * 0.034 / 2;
- uint32_t blue = strip.Color(0, 100, 255);
- uint32_t green = strip.Color(0, 255, 20);
- uint32_t off = strip.Color(0, 0, 0);
- ///////////////////////////////////////////////////////////
- // LED 1 + Buzzer
- ///////////////////////////////////////////////////////////
- if (distance <= 5) {
- if (! lightLED) {
- strip.setPixelColor(0, blue);
- strip.show();
- delay(0);
- }
- else {
- lightLED = false;
- }
- digitalWrite(buzzer, HIGH);
- tone(buzzer, 400, 100);
- delay(75);
- tone(buzzer, 600, 100);
- delay(75);
- tone(buzzer, 800, 100);
- delay(75);
- noTone(buzzer);
- }
- else {
- digitalWrite(buzzer, LOW);
- }
- ///////////////////////////////////////////////////////////
- // LED 2
- ///////////////////////////////////////////////////////////
- if (lightLED && distance <= 5) {
- lightLED = true;
- if (! lightLED) {
- strip.setPixelColor(1, blue);
- strip.show();
- delay(0);
- }
- }
- ///////////////////////////////////////////////////////////
- // DETECT USER PRESENCE
- ///////////////////////////////////////////////////////////
- if (distance >= 6 && distance <= 20) {
- strip.setPixelColor(3, green);
- strip.setPixelColor(4, green);
- strip.show();
- delay(0);
- }
- else {
- strip.setPixelColor(3, off);
- strip.setPixelColor(4, off);
- strip.show();
- delay(0);
- }
- // Voir la distance dans le moniteur
- Serial.print("Distance: ");
- Serial.println(distance);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement