How to stop buzzer sensor?

I have a code that I'm trying to make something like a selection menu with a potentiometer and LED's and when the potetiometer goes from one another the Buzzer makes a short sound and stops. At the moment with my code it is making a short sound but then goes on to make another sound and it goes on in a loop. Any help?? ( I've only done the first LED/ selection with the sound)

const int POT_PIN = 1;
const int LED_1 = 2;
const int LED_2 = 3;
const int LED_3 = 4;
const int LED_4 = 5;
const int SWITCH_PIN = 6;
const int BUZZER_PIN = 7;
int s;
int sound;
void setup() {
  Serial.begin(9600);
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(LED_3, OUTPUT);
  pinMode(LED_4, OUTPUT);
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(BUZZER_PIN, OUTPUT);

}

int pot;

void loop() {
  pot = analogRead(POT_PIN);
  sound = analogRead(BUZZER_PIN);
  s = digitalRead(SWITCH_PIN);
  Serial.println(pot);
  Serial.println(s);
  if (pot <= 700 && pot >= 600 && s == 1) {
    digitalWrite(LED_1, HIGH);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, LOW);
    digitalWrite(LED_4, LOW);
    tone(BUZZER_PIN, 1000);
    delay(500);
    noTone(BUZZER_PIN);
    delay(500);
  } else if (pot <= 700 && pot >= 600 && s == 0) {
    for (int i = 1; i <= 10; i++) {
      digitalWrite(LED_1, HIGH);
      delay(500);
      digitalWrite(LED_1, LOW);
      delay(500);
    }
  } else if (pot <= 599 && pot >= 450 && s == 1) {
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, HIGH);
    digitalWrite(LED_3, LOW);
    digitalWrite(LED_4, LOW);
  } else if (pot <= 599 && pot >= 450 && s == 0) {
    for (int i = 1; i <= 6; i++) {
      digitalWrite(LED_2, HIGH);
      delay(500);
      digitalWrite(LED_2, LOW);
      delay(500);
    }
  } else if (pot <= 449 && pot >= 300 && s == 1) {
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, HIGH);
    digitalWrite(LED_4, LOW);
  } else if (pot <= 449 && pot >= 300 && s == 0) {
    for (int i = 1; i <= 4; i++) {
      digitalWrite(LED_3, HIGH);
      delay(500);
      digitalWrite(LED_3, LOW);
      delay(500);
    }
  } else {
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, LOW);
    digitalWrite(LED_4, HIGH);
  }

}

// Led 1 = Chicken
// Led 2 = Vegetables
// Led 3 = Coffee/Soup
// Led 4 = No input

redo your topic ant put code between tags.
click " < code > " tools bar.

Read : How to get the best out of this forum

1 Like

What? I don't get where to redo my topic and put "code" between tags. And where do I click "" tools bar?

Gave me an error saying "a function-definition is not allowed here before'{' token"
It's marking red where there is the " void loop () { "

Edit your first post and you'll find the format buttons in the tool bar:

immagine

1 Like

OK
I'm going to teach you.

  1. Edit your topic;
  2. Remove the code you pasted in the topic text area;
  3. Using the mouse, click on " < code > " in the tool bar;

4 . Copy your original code;
5. Paste between the
....
type or paste code here
....
that appear on the screen;
6. Save your changes.

Understood now?

1 Like

@fxtalz

Try this code:

const int POT_PIN = 1;
const int LED_1 = 2;
const int LED_2 = 3;
const int LED_3 = 4;
const int LED_4 = 5;
const int SWITCH_PIN = 6;
const int BUZZER_PIN = 7;
int s;
int sound;
void setup() {
  Serial.begin(9600);
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(LED_3, OUTPUT);
  pinMode(LED_4, OUTPUT);
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(BUZZER_PIN, OUTPUT);

}

int pot;
int count = 0;
void loop() {
  pot = analogRead(POT_PIN);
  if (pot < 600) count = 0;
  sound = analogRead(BUZZER_PIN);
  s = digitalRead(SWITCH_PIN);
  Serial.println(pot);
  Serial.println(s);
  if (pot <= 700 && pot >= 600 && s == 1) {
    digitalWrite(LED_1, HIGH);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, LOW);
    digitalWrite(LED_4, LOW);

    if (count < 2) {
      tone(BUZZER_PIN, 1000);
      delay(500);
      noTone(BUZZER_PIN);
      delay(500);
      count++;
    }
  } else if (pot <= 700 && pot >= 600 && s == 0) {
    for (int i = 1; i <= 10; i++) {
      digitalWrite(LED_1, HIGH);
      delay(500);
      digitalWrite(LED_1, LOW);
      delay(500);
    }
  } else if (pot <= 599 && pot >= 450 && s == 1) {
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, HIGH);
    digitalWrite(LED_3, LOW);
    digitalWrite(LED_4, LOW);
  } else if (pot <= 599 && pot >= 450 && s == 0) {
    for (int i = 1; i <= 6; i++) {
      digitalWrite(LED_2, HIGH);
      delay(500);
      digitalWrite(LED_2, LOW);
      delay(500);
    }
  } else if (pot <= 449 && pot >= 300 && s == 1) {
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, HIGH);
    digitalWrite(LED_4, LOW);
  } else if (pot <= 449 && pot >= 300 && s == 0) {
    for (int i = 1; i <= 4; i++) {
      digitalWrite(LED_3, HIGH);
      delay(500);
      digitalWrite(LED_3, LOW);
      delay(500);
    }
  } else {
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, LOW);
    digitalWrite(LED_4, HIGH);
  }

}

// Led 1 = Chicken
// Led 2 = Vegetables
// Led 3 = Coffee/Soup
// Led 4 = No input

Yes thank you and done

1 Like

works but it beeps twice not once. How do I fix that? And when it beeps I can't just use the potentiometer to go from one another because it pauses then beeps and then I can switch again

Change here for how many time do you want to beep.
(count < 2) { //Two times
(count < 1) { // One time
(count < 7) { // Seven times and so ......

Ok. And so with this code can I just copy and paste into all the others to make the same sound into the others? Even in the for loops but I can maybe change the frequency? To make a different sound when selected?

Try it and see if the result pleases you.
I allow :joy: :joy: :joy:

alright thanks

const int POT_PIN = 1;
const int LED_1 = 2;
const int LED_2 = 3;
const int LED_3 = 4;
const int LED_4 = 5;
const int SWITCH_PIN = 6;
const int BUZZER_PIN = 7;
int s;
int sound;
void setup() {
  Serial.begin(9600);
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(LED_3, OUTPUT);
  pinMode(LED_4, OUTPUT);
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(BUZZER_PIN, OUTPUT);

}

int pot;
int count = 0;
void loop() {
  pot = analogRead(POT_PIN);
  if (pot < 600) count = 0;
  sound = analogRead(BUZZER_PIN);
  s = digitalRead(SWITCH_PIN);
  Serial.println(pot);
  Serial.println(s);
  if (pot <= 700 && pot >= 600 && s == 1) {
    digitalWrite(LED_1, HIGH);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, LOW);
    digitalWrite(LED_4, LOW);
      if (count < 1) {
      tone(BUZZER_PIN, 1000);
      delay(500);
      noTone(BUZZER_PIN);
      delay(500);
      count++;
    }
  } else if (pot <= 700 && pot >= 600 && s == 0) {
    
      for (int i = 1; i <= 10; i++) {
      digitalWrite(LED_1, HIGH);
      delay(500);
      digitalWrite(LED_1, LOW);
      delay(500);
    } if (count < 1) {
          tone(BUZZER_PIN,2000);
          delay(500);
          noTone(BUZZER_PIN);
          delay(500);
          count++;
         }
  } else if (pot <= 599 && pot >= 450 && s == 1) {
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, HIGH);
    digitalWrite(LED_3, LOW);
    digitalWrite(LED_4, LOW);
  } else if (pot <= 599 && pot >= 450 && s == 0) {
    for (int i = 1; i <= 6; i++) {
      digitalWrite(LED_2, HIGH);
      delay(500);
      digitalWrite(LED_2, LOW);
      delay(500);
    }
  } else if (pot <= 449 && pot >= 300 && s == 1) {
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, HIGH);
    digitalWrite(LED_4, LOW);
  } else if (pot <= 449 && pot >= 300 && s == 0) {
    for (int i = 1; i <= 4; i++) {
      digitalWrite(LED_3, HIGH);
      delay(500);
      digitalWrite(LED_3, LOW);
      delay(500);
    }
  } else {
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, LOW);
    digitalWrite(LED_4, HIGH);
  }

}

// Led 1 = Chicken
// Led 2 = Vegetables
// Led 3 = Coffee/Soup
// Led 4 = No input

I tried adding the same if (count < 1) etc etc code into the for loop but when the pullup switch is pressed it doesn't give out the single beep. Why?

Your code has a lot of delay(), and it may be that at the moment you press the switch the processing is in some delay() and you don't feel that the switch has been pressed.
Perhaps using interrupt, you can resolve the error.
But the best thing would be, instead of using delay(), to use milliils().
I know that using millis() is complicated for some people, but it is a much "healthier" solution than using delay().

I have a code that I'm trying to make something like a selection menu with a potentiometer and LED's and when the potentiometer goes from one another the Buzzer makes a short sound and stops. At the moment with my code it is making a short sound which is how I want it but then with the same values on the potentiometer when the pull up switch is clicked I don't get any sound which I need. (I've only done the first LED/ selection with the sound)

const int POT_PIN = 1;
const int LED_1 = 2;
const int LED_2 = 3;
const int LED_3 = 4;
const int LED_4 = 5;
const int SWITCH_PIN = 6;
const int BUZZER_PIN = 7;
int s;
int sound;
void setup() {
  Serial.begin(9600);
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(LED_3, OUTPUT);
  pinMode(LED_4, OUTPUT);
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(BUZZER_PIN, OUTPUT);

}

int pot;
int count = 0;
void loop() {
  pot = analogRead(POT_PIN);
  sound = analogRead(BUZZER_PIN);
  s = digitalRead(SWITCH_PIN);
  Serial.println(pot);
  Serial.println(s);
  if (pot < 1000) count = 0;
  if (pot <= 700 && pot >= 600 && s == 1) {
    digitalWrite(LED_1, HIGH);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, LOW);
    digitalWrite(LED_4, LOW);

    if (count < 1) {
      tone(BUZZER_PIN, 1000);
      delay(500);
      noTone(BUZZER_PIN);
      delay(500);
      count++;
    }
  } else if (pot <= 700 && pot >= 600 && s == 0) {
    if (count < 1) {
        tone(BUZZER_PIN,2000);
        delay(500);
        noTone(BUZZER_PIN);
        delay(500);
        count++;
      }
    for (int i = 1; i <= 10; i++) {
      digitalWrite(LED_1, HIGH);
      delay(500);
      digitalWrite(LED_1, LOW);
      delay(500);
    }
  } else if (pot <= 599 && pot >= 450 && s == 1) {
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, HIGH);
    digitalWrite(LED_3, LOW);
    digitalWrite(LED_4, LOW);
  } else if (pot <= 599 && pot >= 450 && s == 0) {
    for (int i = 1; i <= 6; i++) {
      digitalWrite(LED_2, HIGH);
      delay(500);
      digitalWrite(LED_2, LOW);
      delay(500);
    }
  } else if (pot <= 449 && pot >= 300 && s == 1) {
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, HIGH);
    digitalWrite(LED_4, LOW);
  } else if (pot <= 449 && pot >= 300 && s == 0) {
    for (int i = 1; i <= 4; i++) {
      digitalWrite(LED_3, HIGH);
      delay(500);
      digitalWrite(LED_3, LOW);
      delay(500);
    }
  } else {
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, LOW);
    digitalWrite(LED_4, HIGH);
  }

}

// Led 1 = Chicken
// Led 2 = Vegetables
// Led 3 = Coffee/Soup
// Led 4 = No input

Right now with this code it's working up to a point that when I use the potentiometer to go to the ranges of LED_1 it makes a sound but it keeps on repeating till I either change the potentiometer range or I press the button. But after I press the button on the pullup switch it goes back to making the sound repetitively. I only need it to make the repetitive sound once when from another potentiometer range goes to it to make a sound that indicates that you switched from on another. Any help???

consider something like this to map the analog value of the slider to a menu entry

button is implemented, with a button library. I picked Toggle from @dlloyd.

click to see the code
#include <Toggle.h>

const byte POT_PIN = A1;
const byte ledPins[] = {2, 3, 4, 5};
const byte ledCnt = sizeof ledPins / sizeof * ledPins;
const char * ledText[ledCnt] = {"Chicken", "Vegetables", "Coffee/soup", "No input"};
const byte switchPin = 6;
const byte buzzerPin = 7;

Toggle button;

int oldMenuIndex = -1;

void setup() {
  button.begin(switchPin);
  for (int pinIndex = 0; pinIndex < ledCnt; pinIndex++)  pinMode(ledPins[pinIndex], OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  int newMenuIndex = map(analogRead(POT_PIN), 0, 1024, 0, ledCnt); // generate a number between 0 and ledCnt-1 (ledCnt is not possible as 1024 is not possible)
  bool s = (digitalRead(switchPin) == LOW); // true if pressed

  if (newMenuIndex != oldMenuIndex) {
    tone(buzzerPin, 200u + 300u * newMenuIndex, 100);
    if (oldMenuIndex >= 0) digitalWrite(ledPins[oldMenuIndex], LOW);
    digitalWrite(ledPins[newMenuIndex], HIGH);
    Serial.println(ledText[newMenuIndex]);
    oldMenuIndex = newMenuIndex;
  }

  button.poll();
  if (button.onPress()) {
    Serial.print("New order in : ");
    Serial.println(ledText[oldMenuIndex]);
  }

}

Note that I edited the code and the wokwi whilst you clicked on it to add the button feature.

Yes I realised. But I can't make it like that. Take into for instance you have a knob on a microwave and you turn the potentiometer (knob) onto the selected cooking option (The 3 LEDs) and you click start. Depending on what cooking selection you picked it blinks an amount of times to show that it's cooking. My problem is that I have another feature of the sound where when you switch from one cooking option to another it gives out a sound and when you press the button on a selected cooking option it gives out another type of sound. This is where I'm not able to do it. I have been working on this for quite some time now and I'm getting kind of agitated with it. If you know how to help please do so.

Hi @fxtalz,

which microcontroller do you use?

If it is an Arduino board pin 1 refers to D1 (digital 1) that is used for Hardware Serial. To address the analog inputs you had to use A0, A1, etc.

(As you may see @J-M-L corrected this already in the Wokwi sketch.)