Notifications
Clear all

Piezo Buzzer for loop not working

24 Posts
6 Users
0 Likes
8,487 Views
(@rms84)
Member
Joined: 4 years ago
Posts: 7
Topic starter  

Thank you everyone that contributed to this thread, I have managed to solve the problem. ? ? ? 

A big thank you for educating me that i was being re-set to 0 every time the code looped. I was completely unaware of this and was tearing my hair out wondering what was wrong with my code!

For some reason when I declared i in the global setup code it wouldn't work (not sure why, probably something a newbie like me overlooked). So I did some research into how to stop i being reset to 0 and came across a way to stop that is to declare i as a "static int". I also changed the code to an if / else statement and it finally beeped the correct number of times then stopped. 

I will post the code below in case it can help anybody reading this with a similar problem, or if anyone has any further advice on how to improve. I've kept my delay code in there, so wondering why that's bad? Some feedback as to why its ugly would be really appreciated and I will see if I can improve on that next. As I wanted it to beep for 2 mins it seemed this was the best way to do it (with my limited knowledge).

 

Here is my new code. I've kept the rest of my project code out to avoid confusion.....

void setup() {
// put your setup code here, to run once:

}
void loop() {

static int i = 0;
int pin = 8;
int frequency = 2000;
int duration = 500;

// beeps for 2 minutes (120 seconds)
if (i < 120){
i++;
tone(pin, frequency, duration);
delay(1000);
}

else
noTone(pin);
}

 


   
ReplyQuote
(@rms84)
Member
Joined: 4 years ago
Posts: 7
Topic starter  

@pugwash

I've just learned why delay is a problem as it was starting to affect something else in the project. Thanks for educating me, I've now changed the script to millis as I've done some research into it and now understans why it's better to use millis rather than delay ? 


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 
Posted by: @rms84

@pugwash

I've just learned why delay is a problem as it was starting to affect something else in the project. Thanks for educating me, I've now changed the script to millis as I've done some research into it and now understans why it's better to use millis rather than delay ? 

My pleasure!! May you have years of unproblematic interrupt requests! ? 


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1112
 

rms84

I just perused your code snippet  a couple of posts back whilst as I was finishing my bed time Horlicks.  I did not look at the whole thread but my quick comments on your code as as follows.

I see you now appreciate millis instead of delay and see that you now see that your code was looping back to the start of the loop and so the i variable was being reset to zero.  As its now changed to a static variable the loop function remembers the value and as it's already declared it now ignores the declaration that initially assigns a 0 value.  

However whilst I presume this was just a little test script, normally a lot more goes on in the main loop so it would be best to get into the habit of making this beeping as a function that is then called from the main loop as required.   Also rather than use variable that is checked with an if statement and then incremented with a delay  consider the following (just a suggestion)

int beenBeeped = 0; // this is just so the function is only called once in the loop()

const int frequency = 2000; 

const int duration = 500;

void setup() {

}

void loop() {

if (beenBeeped == 0) { 

   beenBeeped = doBeeb(120000); // 2 minutes I hope!!

  }

}

int doBeeb(long beep_time) {

tone(8,frequency,duration);

while (millis() < beep_time) { }

noTone(8);

return 1;

}

 

If the frequency and duration variables do not change (vary) then consider using a constant for these values.  As the example above place them  at the top of the program so their values can be easily modified should the tone not be to your taste

As I'm very prone to typos I thought I would put the code into Arduino IDE and check.  Well bugger me the Arduino no longer works on my my Mac and I find its because I updated to the latest os Catalina.   I cant get to bed until this is fixed so I hope its not going to be a long night.  Oh bother.


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@byron

"Horlicks" my Dad used to drink that stuff, but I found it revolting, then again the reverse was true about Marmite. It is an associative thing, as I have always linked Horlicks to old people, and I am trying to convince myself that my own 64 years is not OLD.

Back to the topic, I disagree about using an extra function. tone() is already a function and a one-liner at that, therefore it would unnecessarily bloat the code. And I think that there is confusion about tone() and noTone().

noTone() is only called if the tone duration is not specified. Therefore

tone(8,frequency,duration);
while (millis() < beep_time) { }
noTone(8);

makes no sense. tone() will shut off automatically after the duration has been achieved.

Also putting millis() in an external function is not a good idea, all timing and flow control should be in the main loop() function. Keep external functions for procedures that are called at different points in the program and/or passed different variables on each loop.


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1112
 

@rms84 @Pugwash

Well I'm not too bleary eyed after staying up to fix my Arduino / mac Catalina issue (which is now resolved after a bit of googling) and I see the errors in my suggestions that has proved to be a not too helpful I fear.  I quite agree on your point about not using millis in the way I put it as its utterly superfluous as tone has a duration argument.  And, as millis() starts when the Ardunino is fired up my code would not work if some other task meant that the funtion was not called after 2 minutes was up.  All in all a right balls up. ? 

So I now distill my suggestions as follows

1. consider using a constance if the variable value does not change in the program.

2. Use functions as much as possible is good practice.  However do not use a function just to call another function, (silly me). 

3. If you are needing to use time delays then don't loop with if statements and incrementing a variable, millis() is incrementing away quite nicely as it is.

4. if the function has a duration argument such as tone() then use it, it will do just that.  

And I see from rms84 lastest posted code your programming now looks much better and you are learning very fast indeed. ?   (woops I edit this post as see it was Pugwash that posted the code not rms84 - I must keep taking the tablest!)

Fixing up my Arduino IDE and the googling around somewhat reminded my of the antiquity of good old Arduino uno and the programming environment.   For Python I started to use Visual Studio Code and really like it so I will load the platformIO into it and have a go at that for Arduino code to see how I like it.    That also brings to mind my recent forays into using micropython for boards that can use it in place of the Arduino C.  Whilst C runs a lot faster then python, in my use cases of home control, weather station sensor recording, and robot control, has proved fast enough and much better to use.  I should not bang on about this in this post and sometime I will start a new thread on this when I get the time to see if it sparks any interest.

Coincidentally yesterday my wife was mooting what to put in her Christmas groceries list and found some marmite flavoured crisps and biscuits in her online shopping site. In place of the flavoured biscuits I suggested a jar of marmite and plain old biscuits so I could then spread it on nice and thick.  I wonder if a bit of marmite in my Horlicks would be a good idea. ? 


   
ReplyQuote
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
 

@byron

However, do not use a function just to call another function.

There is absolutely nothing wrong with calling functions from other functions, in fact, recursive functions are a science in their own right.

"Horlicks and Marmite" entirely redefines the term "an acquired" taste"! ? ? 


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1112
 

@Pugwash - I was not meaning to say functions calling functions is wrong, just was meaning to say that creating a function just to call another function, and thats all it does, is a waste and I was concerned my previous post could be construed to indicate I was advocating this.   But you are right to point this out and I think I will refrain from making more suggestion as I only seem to potentially mislead which is not good.


   
ReplyQuote
Page 2 / 2