Looking for input

Hello all!
I am new both to this forum and micro controllers (specifically Arduino, obviously :stuck_out_tongue: ).
After a week of incredibly proud Arduino ownership, I have learned quite a lot.
I have been trying to fumble my way through using a Ping))) sensor, with a 6 LED bar graph, and have also tied in a piezo buzzer, to fashion a functional backup sensor for a car, or any other type of thing that having a “too close” alarm would come in handy for.
I know there are several examples of this on the web, but I am trying to learn my way through the programming language with this project, so I have adapted several different codes to fit my design, and tried to do it without help.
until now…

I will post the code below, but first, everything works beautifully and exactly how I want it, Save for one tiny thing that really doesn’t bother me, but if there is a fix for it, I would like to learn it and why it works.

I can get the Ping sensor to light up my LEDs as an object gets closer, in the right order, and when I want them to come on. I am using 6 LEDs for my meter, and for the last two LEDs I want sound effects too.
a 1/2 second repeating beep for LED 5 to light up, and a solid tone for LED 6(the last one to light up, at the closest distance)

Right now, that is more or less what I get, but the tone kicks in just a bit before the LED actually lights up.
To finally get to my point, I would like the LED to light at the same time the pulse tone starts, and the last to light at the same time the solid tone starts.
Here is my code

#include <NewPing.h>
#define TRIGGER_PIN 8
#define ECHO_PIN 8
#define MAX_DISTANCE 400
#define ITERATIONS 10


int BUZZ = 10;

const int minDist = 0;
const int maxDist = 150;
int BUZZstate = LOW;
long prevBUZZ = 0;
long interval = 500;

const int ledCount = 6;

int ledPins =
2, 3, 4, 5, 6, 7

void setup()
pinMode(BUZZ, OUTPUT);

for (int thisLED = 0; thisLED < ledCount; thisLED++)
pinMode(ledPins[thisLED], OUTPUT);

void loop()
unsigned int uS = sonar.ping_median(ITERATIONS);
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM);

int ledLevel = map(uS / US_ROUNDTRIP_CM, 0, 161, ledCount, 0);

for (int thisLED = 0; thisLED < ledCount; thisLED++)
if (thisLED < ledLevel)
digitalWrite(ledPins[thisLED], HIGH);
} else {
digitalWrite(ledPins[thisLED], LOW);
int Distance = map(uS / US_ROUNDTRIP_CM, minDist, maxDist, 6, 1);
switch (Distance)
case 1:
digitalWrite(BUZZ, LOW);
case 2:
digitalWrite(BUZZ, LOW);
case 3:
digitalWrite(BUZZ, LOW);
case 4:
digitalWrite(BUZZ, LOW);
case 5:
unsigned long BUZZmillis = millis();
if(BUZZmillis - prevBUZZ > interval)
prevBUZZ = BUZZmillis;
if (BUZZstate == LOW)
BUZZstate = HIGH;
BUZZstate = LOW;
digitalWrite(BUZZ, BUZZstate);
case 6:
digitalWrite(BUZZ, HIGH);

sorry for the lack of comments in my code, this has been revised so many times, I was getting myself more confused with them in than out, so I took them out. If anyone has any questions on why I used a particular line or section, ask away, and I will do my best to clarify.

if anyone can see anywhere I can make it more efficient and also get the last two LEDs to light and the correct tones to sound simultaneously, I would greatly appreciate it!

Looking for any advice, really, though a solution to my specific problem would be wonderful.

Thanks in advance to anyone who can shed some light on my path :slight_smile:

You have two separate chunks of code reading the distance and working out which of the six bands it falls within. From your description, I would guess they are not producing the same result. I suggest you combine them so that you determine the ‘distance’ (0-5) once, and use the same value for the LEDs control and the buzzer control.

Also, when you turn the buzzer off, reinitialize BUZZstate so that the next buzz will always start in the ‘high’ state; otherwise you may have to wait for half a second before you hear anything.

Thank you so much for the reply and advice!

I was already tinkering with the sketch so I have been able to try out your suggestions.

I found the problem right away, and without having to change much at all.

I guess the problem was my own misunderstanding of the switch function. I had mapped it out from 0-5 originally, and wrote my switch starting from zero, which would turn on the buzz early, and wouldn't allow it to cycle properly. Of course at the time I was using a delay to pause the tone, which was screwing up everything...
I had changed my buzz map to start from one, which fixed most of my issues, but still had the buzz coming on a fraction of a second early, maybe 2cm before the LED came on.
after I changed the code to work around the delay, so I could take it out, I never changed the buzz map back to 0.
After reading your suggestion, I looked over my code, and noticed I have the two maps valued differently, so I set them to match, and now the beeping and the solid tone both start immediately when they are supposed to.

Now to reinitializing the BUZZstate to HIGH. I changed the int for BUZZstate to HIGH from LOW, and didn't really notice a difference.
Every so often, the first beep will start 1/2 second after the LED lights up, as you said it might. The tone starts perfectly though, so I am pleased about that.
is there another way to reinitialize the BUZZstate to HIGH that I am not thinking of?
perhaps somewhere at the end of the switch code?
sorry for my lack of experience here, it is very possible I haven't understood you correctly :stuck_out_tongue:

The key is "reinitialize". When the range changes so that case 5 is not executing any more, you don't know whether buzzstate was left high or low when it left case 5. You need a way to reset it to low. Simple but very dirty would be to set it low in every case but 5. Slightly better would be to check that the state is not 5 before entering the switch and set it low there.

Thank you for the clarification :slight_smile:

I have to head off to work, but when I get home, I will give it a shot. I have some idea of how to make both suggestions work, so I will see what I come up with.

I very much appreciate the advice!