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 LED n+1 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:
#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);
}
Some people also told me to debounce the detection but I found nothing that could help me in this case by searching for “debouncing” on Google.
I will be more grateful if you have a piece of code or something to help me.
Thanks,
Thibaut