Adding a buzzer to bargraph example?

Hi,

I use the basic bargraph code which is included with Arduino software in Examples->Display->Bargraph (so I wont post it here).

I'd like to add a buzzer to only beep once every time a new LED lights up in the row of 10x LEDs.
So that means every time I turn the "potentiometer" higher it lights a new LED and makes a beep.
And no beeps when decreasing.

Been trying different solutions for 2 days now, and tested them by replacing the buzzer with a led, but not found a solution. I would be happy if someone has figured it out?

Thanks a lot! :slight_smile:

Please post your entire sketch in code tags. Yes, I have the examples but not your "different solutions". Post the one that you think came closest to working.

Also, have you succeeded in writing code that only buzzes the buzzer, to test it?

Hi! Ok. The code is below. The buzzer is on pin 13. In this not correcly working "solution" below, I have only added to the original bargraph code, these lines:

tone(13,50,20);
delay(20);

noTone(13);
delay(20);

The first 2 lines start the buzzer and the 2 below are the "noTone" command.
Here is the full sketch:

/*
  LED bar graph

  Turns on a series of LEDs based on the value of an analog sensor.
  This is a simple way to make a bar graph display. Though this graph uses 10
  LEDs, you can use any number by changing the LED count and the pins in the
  array.

  This method can be used to control any series of digital outputs that depends
  on an analog input.

  The circuit:
  - LEDs from pins 2 through 11 to ground

  created 4 Sep 2010
  by Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/BarGraph
*/

// these constants won't change:
const int analogPin = A0;   // the pin that the potentiometer is attached to
const int ledCount = 10;    // the number of LEDs in the bar graph

int ledPins[] = {
  2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};   // an array of pin numbers to which LEDs are attached


void setup() {
  // loop over the pin array and set them all to output:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT);
  }
}

void loop() {
  // read the potentiometer:
  int sensorReading = analogRead(analogPin);
  // map the result to a range from 0 to the number of LEDs:
  int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);

  // loop over the LED array:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    // if the array element's index is less than ledLevel,
    // turn the pin for this element on:
    if (thisLed < ledLevel) {
      digitalWrite(ledPins[thisLed], HIGH);

tone(13,50,20);
delay(20);

    }
    // turn off all pins higher than the ledLevel:
    else {
      digitalWrite(ledPins[thisLed], LOW);

noTone(13);
delay(20);
    }
  }
}

// —- add here —-

static int prevLevel:

if (prevLevel != ledLevel) {
    doTheBuzz();
    prevLevel = ledLevel;
}

Hello, I added as you instructed in the right place:

static int prevLevel;
if (prevLevel != ledLevel){
tone(13,50,20);
delay(20);
prevLevel = ledLevel;
}

But this seems to give a constrant rattle from the buzzeer...
Oh, well need to go to sleep and try again after work tomorrow :slight_smile:

Hi, also I must add that I'm not using a potentiometer as the input device on pin A0 but instead an antenna, so its actually an EMF detector. So the input changes all the time. Potentiometer would give a steadier input signal.

What’s the delay() for ?
check this link

And also consider the notone() function.

Perhaps there’s enough noise on your ’antenna’ input to cause ‘jitter’, and the tone is being retriggered.
You could put a margin perhaps 2 units or ‘anything’ to reduce edge values triggering the sound.

You might have an active buzzer. It has a fixed frequency, internal oscillator. For that you just apply voltage to make it beep.

Or, maybe:

static int prevLevel:

if (ledLevel > prevLevel) {
    doTheBuzz();
    }
prevLevel = ledLevel;

…maybe, but zero difference with memory usage as a static / global, and buzzer won’t trigger if ledValue is decreasing

As OP wishes.