2

For this project I seek to create a counter of water glasses we drank throughout the day. I use for this an Arduino UNO card, a Piezo buzzer and a Neopixel rgb LEDs strip (5 LEDs for this test).

Aside from the last 2 LEDs that only light up to indicate to the user that his presence is considered by the product, I would like that when the HC-SR04 first detects our glass of water between 0 and 5cm (cheers!), the first LED lights up and stays on. When the first LED is on and the HC-SR04 detects a second time our water glass is between 0 and 5cm, then the second LED lights up and stays on as well. And so on. For each glass detected between 0 and 5cm, a slight sound sounds through the piezo buzzer.

By using the Adafruit Neopixel library (download here), I manage to make everything work as I see fit. However, I still have trouble programming if the LED n is on then the n+1 LED lights up at the next detection between 0 and 5cm, without all the LEDs lighting at the same time in a single detection.

I recently tried to use a boolean to identify when the LED n is true (ON/HIGH), but unsuccessfully. I leave you my code as is by thanking you for any help you can bring me.

See my layout and my code below: Arduino UNO + Components layout

#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);
}

Sorry for the code that is a bit rough, I am a beginner but I hope not to have lost you in my explanations (I lose myself in my code…).

Thanks,

Thibaut

5
  • You need to debounce the detection. Mar 28 '17 at 0:48
  • Thanks for answering @IgnacioVazquez-Abrams, can you be more explicit with a piece of code or something? As I said, I'm a beginner and I have trouble seeing what you mean.
    – Thib
    Mar 28 '17 at 11:12
  • Search for "debouncing" on Google. There are many ways to do it in hardware or software. I doubt you will find someone who has debounced a HC-SR04 in software for an Arduino, you will have to piece together bits from various sources. Debouncing switches is probably the most common. You might be a beginner, but if you have written that much you'll be able to write a debouncing function and it will be more 'fun' than copy and paste, because you will understand it. If you get stuck though, come back with what you have. Mar 28 '17 at 12:26
  • 1
    Yes you're totally right @Matt :) I'll see this as soon as possible and will come back here if I struggle to do what I want. Similarly, I will not fail to share the result if it's successful. Thanks guys.
    – Thib
    Mar 28 '17 at 12:30
  • 1
    @Thib Can you share your solution here as an answer (which is encouraged in cases like this)? I had a similar idea for combining range-detection with the WS2812 so I'm interested in how your project came out. Jan 23 '18 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.