If the only functionality you need is to beep the buzzer then just use a for loop and delay() calls to time the beep. Make your best attempt and we can help.
ToddL1962:
Please use a code block to post your code.
If the only functionality you need is to beep the buzzer then just use a for loop and delay() calls to time the beep. Make your best attempt and we can help.
i want to be able to input a number through the serial monitor, and it beep that many times. I used a for loop and it would beep constantly even with delays in place.
jacklythgoee:
i want to be able to input a number through the serial monitor, and it beep that many times. I used a for loop and it would beep constantly even with delays in place.
Then please post your attempt and we can find the issue.
To get the value of the Serial input, you can use something like this: (here's a page with more details)
In your code you are only starting to read if there is nothing there (Serial.available()==0)
This will take the number from the serial input when it is detected (more than 0) and pass it to the function called "buzzerBeep"
if (Serial.available() > 0) {
buzzerBeep(Serial.parseInt());
}
Here is the function:
void buzzerBeep(int beeps) {
for(int x = 0; x < beeps; x++){
digitalWrite(buzzPin, HIGH);
delay(1000);
digitalWrite(buzzPin, LOW);
delay(1000);
}
}
What that does is take the number of beeps from the serial input, and turn the buzzer on and off again that many times with a one second delay in between.
Here's the final code:
int number;
int buzzPin = 8;
String msg = "Please input your number: ";
void setup() {
Serial.begin(9600);
pinMode(buzzPin, OUTPUT);
Serial.println(msg);
}
void loop() {
if (Serial.available() > 0) {
buzzerBeep(Serial.parseInt());
}
}
void buzzerBeep(int beeps) {
for(int x = 0; x < beeps; x++){
digitalWrite(buzzPin, HIGH);
delay(1000);
digitalWrite(buzzPin, LOW);
delay(1000);
}
}
Do note this doesn't repeat the question for simplicity, it only asks it once in the setup.
I hope this helps