Button beep

Hello guys, I need your help.

I’m working on a project where I use a lot of buttons. I want to add a feature with these rules:

  • When I press a button, a piezo beeps.

  • When I press the button, the piezo beeps. Then I immediately release the button and nothing happens. I press the button quickly again and the piezo beeps again.

  • When I press the button, the piezo beeps. Then I hold the button and nothing happens.

And it is also worth mentioning that all buttons are connected via analog pin.

All I was able to come up with this:

const int piezo = 10;
const int buttons = A0;

const float button_1 = 1000.0;
const float button_2 = 800.0;
const float button_3 = 600.0;
const float button_4 = 400.0;
int button_value;
int pressed_first_time_1 = 0;
int pressed_first_time_2 = 0;
int pressed_first_time_3 = 0;
int pressed_first_time_4 = 0;

int last_played = 100;
int last_played_limit = 100;

int_button_frequency = 800;
int button_sound_time = 20;

void setup() {

pinMode(buttons, INPUT);

}

void loop() {

last_played = last_played + 1;

button_value = analogRead(buttons);

if (button_value < (button_1 + 10) && button_value > (button_1 - 10)) {

  if (pressed_first_time_1 != 1) {
    tone(piezo, button_frequency, button_sound_time);
    last_played = 0;
  }

  if (last_played >= last_played_limit) {
        tone(piezo, button_frequency, button_sound_time);
        last_played = 0;
  }

  pressed_first_time_1 = 1;
  pressed_first_time_2 = 0;
  pressed_first_time_3 = 0;
  pressed_first_time_4 = 0;

}

else if (button_value < (button_2 + 10) && button_value > (button_2 - 10)) {

  if (pressed_first_time_2 != 1) {
    tone(piezo, button_frequency, button_sound_time);
    last_played = 0;
  }

  if (last_played >= last_played_limit) {
        tone(piezo, button_frequency, button_sound_time);
        last_played = 0;
  }

  pressed_first_time_1 = 0;
  pressed_first_time_2 = 1;
  pressed_first_time_3 = 0;
  pressed_first_time_4 = 0;

}

else if (button_value < (button_3 + 10) && button_value > (button_3 - 10)) {

  if (pressed_first_time_3 != 1) {
    tone(piezo, button_frequency, button_sound_time);
    last_played = 0;
  }

  if (last_played >= last_played_limit) {
        tone(piezo, button_frequency, button_sound_time);
        last_played = 0;
  }

  pressed_first_time_1 = 0;
  pressed_first_time_2 = 0;
  pressed_first_time_3 = 1;
  pressed_first_time_4 = 0;

}

else if (button_value < (button_4 + 10) && button_value > (button_4 - 10)) {

  if (pressed_first_time_4 != 1) {
    tone(piezo, button_frequency, button_sound_time);
    last_played = 0;
  }

  if (last_played >= last_played_limit) {
        tone(piezo, button_frequency, button_sound_time);
        last_played = 0;
  }

  pressed_first_time_1 = 0;
  pressed_first_time_2 = 0;
  pressed_first_time_3 = 0;
  pressed_first_time_4 = 1;

}

But this doesn’t work as I wanted to. It beeps when pressed first time. When I press another button, it beeps again. When I press a button, then I wait few seconds and then I press the button again, it beeps again. When I hold a button a little longer, then it does nothing.

But what it does wrong: When I hold a button too long, it beeps again… And when I press a button several times in a row (quickly enough), it beeps only the first time and then it beeps only once per few seconds.

Can anyone help me with the code? I will be grateful for any help :slight_smile:

Your 'last_played' variable is a 16-bit integer which can only hold numbers up to 32767. Since you are running through loop() hundreds or thousands of times per second your variable will overflow fairly quickly.

For timing, use the millis() function and use unsigned long variables. This will give you millisecond timing for intervals up to 47 days.

My reading of your design document leads me to believe that it doesn't matter how long you hold or release a button, you want one beep each time any button goes from unpushed to pushed. To do that you just need to keep track of the previous state and when the current state is pushed and the previous state is unpushed, start a beep.

Thanks, I thought that it would be difficult to track previous state of an analog input.. But after a while I figured out the conditions and then it started working :slight_smile: