I'm doing a project that includes an ultrasonic sensor and a buzzer, where when the sensor detects an object the buzzer beeps. And I have successfully done that but wanted to ask if there's another code where the buzzer does not continuously beep, but rather intermittently. So as the object gets closer to the sensor, the buzzer becomes more continuous instead of intermittent and vice versa. Kindly advice on this and thank you in advance! Also, I'm using an Arduino Mega 2560.
You need to look at the blink without delay example in the IDE
The generalised way how writing code is done is:
analysing the functionality and describe it in normal words
identifiying senseful parts of the overall functionality and divide the overall functionality into parted funcionalities.
then start to write code for the part-functionalities.
In your case this is:
measuring distance (this is a part you have working)
creating an intermitant buzz with a constant interval
making the buzzer on/off-interval dependant on the measured distance
In the end you want to do two things in parallel:
- measuring distance
- buzzing in intervalls
doing things in parallel requires non-blocking timing based on function millis().
Here is a demo-code that shows how this can be done
This demo-code is not close to your thing combining distance-measuring with variable intervall-buzzing but shows the basic principle how non-blocking timing works
It is a continuosly checking if a certain time-intervall has passed by
unsigned long DemoTimerA = 0; // variables that are used to store timeInformation
unsigned long DemoTimerB = 0;
unsigned long DemoTimerC = 0;
unsigned long DoDelayTimer = 0;
boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - expireTime >= TimePeriod ){
expireTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
void setup() {
Serial.begin(115200);
Serial.println("Program started activate Show timestamp in serial monitor");
}
void loop() {
if ( TimePeriodIsOver(DemoTimerA,1000) ){
Serial.println(" TimerA overDue");
}
if ( TimePeriodIsOver(DemoTimerB,2000) ){
Serial.println(" TimerB overDue");
}
if ( TimePeriodIsOver(DemoTimerC,3000) ) {
Serial.println(" TimerC overDue");
}
if ( TimePeriodIsOver(DoDelayTimer,20000) ){
Serial.println("every 20 seconds execute delay(3500)... to make all other timers overdue");
delay(3500);
}
}
best regards Stefan
Hello Stefan,
Thank you for your reply, your method seems to be the most efficient way to execute this. If I wanted it to beep intermittently at a distance of 40 cm, and continuously at a distance of 15 cm, how do I modify your code such that it fulfills those measurements and/or conditions. I'm fairly new to this so kindly assist me and my apologies if you have already mentioned this in the code you provided above.
If this is your "second" program this is pretty ambitious.
I will not post ready to use code.
imagine you would like to learn to carve. To carve such a rabbit
Would it be helpful If I would carve it for you? If you want to learn carving?
Just a little if you would be able to watch me carving. This is just an analogon
I can't carve in such an artistic style.
You should run the demo-code on your Arduino Mega 2560 looking at what is shown in the serial monitor.
Then you should post your code that you have so far.
The code that does measuring the distance and switches on the buzzer permamently.
Then next step is to add switching on / off your buzzer to the demo-code with the non-blocking timing.
best regards Stefan
I have found the solution to this issue, I used else if commands. Thanks for your help!
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.