Arduino Tutorial 23: Changing Tone of an Active Buzzer

I need you guys to go ahead and get you a big mug of iced coffee. I need you to get out youre a Lego super starter kit. If you dont have one look down in the description. Click, the link 35 bucks – you get everything you need for this complete series of tutorials that way were working with the same hardware makes it a little bit easier. Ok, this is going to be a quick lesson and its a follow on from lesson number 22 and in lesson number 22. I was showing you that the active buzzer is very easy to incorporate sound in your project. The active bugs are in your a Lego. Kit is the one thats black on the bottom. The passive buzzer is green on the bottom skate the one thats black on the bottom and also make note. If you look, there is a plus and a minus on the top there. You can kind of see it theres, a plus that needs to go to the plus voltage and the negative needs to go to the negative voltage. And so I have the negative going to ground. And if I get the plus wire, I can just plug it into 5 volts and you can see that it turns on very easily well in the earlier lesson. I just showed you that you could kind of turn it on and off, and I want to show you in todays lesson that you actually can get a little bit of a tone out of this now.

If you really want to do tones, I would do the passive buzzer and we will do that in the next lesson, but I will just show you how to get a little bit of a tone out of here and so lets come over and lets start coding. I think this would be a good view, and so we are on pin 8, so Im going to call buzz, pin RSA int int is buzz, pin is equal to pin 8 and then I think that is all Im going to need. Look. I already did this. Okay, I need to do a pin mode for buzz, pin, pin mode, a buzz pin is going, the pinmode is going to be an output, and then I come here and what I can do is. I guess i need two other two other variables im going to have an INT and im going to have a delay time, one which im going to set to one millisecond and then im going to have an INT im going to have a delay time to and Im going to set that equal to two milliseconds and then what Im going to do here is Im going to create a for loop, and if I have a for loop, I am going to need a counter. So Im going to come up here and say int and create a counter J, okay, Im not going to put a value because the value will be set in the for loop.

So for J, equal one loop as long as J is less than or equal to 100 and then J is equal to J plus 1. All right open. The clause I hit return Arduino automatically puts in the curly to end the clause and then what I am going to do is Im going to say digital bright, but buzz, pin high Im going to delay by delay, time 1 and then Im going to digital right Buzz pin low okay, what you see is Im just going to turn it on and off and on I mean with the late Im, 1 being very short. This is gon na go through this a hundred times so Im gon na get the first pitch here and then similarly delay delay time, one ok now for the other. For the other tone, I need to come out of this for loop, because this for loop needs to be just that pitch now. Im gon na do the second pitch by coming up with a new for loop outside the other for loop. For oh, I made a mistake up here. I hope you guys were yelling at me up here. Use semicolons a lot of times. You use commas and its hard to figure out for J. Equal 1 to J is less than or equal to 100 and J. Equal J plus 1 open curly to open the clause, then Arduino puts the clock closed curly in for me, and I think the easiest thing is to just copy and paste this control C control B there.

It is, and this time Im going to do, delay time too. Alright, so lets listen to this and lets see if we get some sound out of this hold your breath. Oh, what did I do here? What is it complaining about for? Ah, I did it again. Ok, I told you people do that. Ok hold hold your breath. Yes, okay, do you hear that? Okay Cece, we got a little bit of an alarm sound, very annoying. Okay, thats, just showing you can get a little bit of a tone out of the active buzzer, but if you really want to do that, you probably want to go to the passive buzzer, which we will do in the next lesson. Okay, this has been palma quarter for top tech boy comm.

Share.
Exit mobile version