Piezo Speaker, RGB LED, & Button help needed please

Hi I was working on one of my Arduino projects and it is a pretty basic one but I am a beginner in Arduino. So my project states that I am supposed to increment the frequency of a piezo speaker from 20Hz to 20000Hz. Then the user will have to press the button after they cannot hear the sound anymore from the button and then the RGB led will display a color based on the frequency.

At this point, I know how to turn on and off the button and set the RGB led to whatever frequency it would read. The big problem I have is I don't understand what function to use and how to use it to increment the frequency and start and turn off the piezo speaker. My guess is that I have to use the HIGH and LOW to turn on and off the piezo speaker but I am completely lost in terms of incrementing the frequency. My teacher gave me a hint and said that we should try to use the tone() function but I looked that up and can't seem to find a way to link these two together.

Any help would be appreciated. Thank's guys!

uanon9526:
Hi I was working on one of my Arduino projects and it is a pretty basic one but I am a beginner in Arduino. So my project states that I am supposed to increment the frequency of a piezo speaker from 20Hz to 20000Hz.

My teacher gave me a hint and said that we should try to use the tone() function but I looked that up and can't seem to find a way to link these two together.

First up, using 'tone()' it's not possible to generate tones lower than 31Hz. (This is clearly stated in the "Tone" reference.) Tell your teacher that he/she should have known about that limitation.

..... after they cannot hear the sound anymore from the button

It'll have to be a very special button - most buttons don't make any sounds. :smiley:

At this point, I know how to turn on and off the button

You do this by pressing a button.

The big problem I have is I don't understand what function to use and how to use it to increment the frequency and start and turn off the piezo speaker.

It's not brain surgery - you were already pointed in the direction of 'tone()'.

My guess is that I have to use the HIGH and LOW to turn on and off the piezo speaker

Wrong guess. See above. (Use 'tone()' and then 'noTone()' if you need to turn it off.)

but I am completely lost in terms of incrementing the frequency.

Frequency in hertz is one of the parameters you pass in the call to 'tone()'.

You would do well to start by carefully reading the "Tone" reference:- Tone

Also check out the 'tone()' examples included with the IDE, under >File >Examples >Digital

A frequency is oscillations per second.
An oscillation is starting at one state, going to another, then back again.
So for example, you could turn a pin HIGH, then LOW again.
You simply need to add timing into this and you get a frequency.
Consider this timing example

unsigned long lastOccurence = 0;

void loop(){
  if(micros() - lastOccurence >= someDelayInMicroseconds){
    //code in here will be executed every 'someDelayInMicroseconds' microseconds
    //timing is crucial for frequency
    lastOccurence = micros(); //update the time this code was executed
  }
}

This is not the entire solution, but it should give you a push in the right direction...

Ps991:
A frequency is oscillations per second.
An oscillation is starting at one state, going to another, then back again.
So for example, you could turn a pin HIGH, then LOW again.
You simply need to add timing into this and you get a frequency.
Consider this timing example

unsigned long lastOccurence = 0;

void loop(){
 if(microseconds() - lastOccurence >= someDelayInMicroseconds){
   //code in here will be executed every 'someDelayInMicroseconds' microseconds
   //timing is crucial for frequency
   lastOccurence = microseconds(); //update the time this code was executed
 }
}




This is not the entire solution, but it should give you a push in the right direction...

His teacher hinted at using 'tone()'. There's no need to make it any harder than it needs to be.
Although 'tone()' only goes as low as 31Hz, that's fine since that's how the teacher wanted to see it done:-

My teacher gave me a hint and said that we should try to use the tone() function]

There's no such function as 'microseconds()', either. Your code could never compile, so you make it very hard for a beginner. 'micros()' maybe?

Edit: I don't intend writing any code to help - this is a school project and the OP says this isn't his first project so he should know how to increment the frequency at regular intervals if he thinks about it.

There's no such function as 'microseconds()', either. Your code could never compile, so you make it very hard for a beginner. 'micros()' maybe?

You are totally right, sorry, it has been a few months since I used Arduino and I forgot it was micros(). Good catch sir!