EasyManua.ls Logo

SunFounder 3in1 Kit - Page 157

Default Icon
351 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
SunFounder 3in1 Kit
Open the 5.7.tone_notone.ino file under the path of 3in1-kit\learning_project\5.7.tone_notone.
Or copy this code into Arduino IDE.
At the time when you finish uploading the codes to the R4 board, you can hear a melody containing seven notes.
How it works?
There are two points needing your attention:
1. tone() & noTone(): This function is used to control the sound of the passive buzzer directly and its prototype
is as follows:
Syntax
void tone(int pin, unsigned int frequency)
void tone(int pin, unsigned int frequency, unsigned long duration)
Parameters
pin: The Arduino pin on which to generate the tone.
frequency: The frequency of the tone in hertz.
duration: The duration of the tone in milliseconds (optional)
Generates a square wave of the specified frequency (and 50% duty cycle) on a pin (so as to make the passive buzzer
vibrate to make sound). A duration can be specified, otherwise the wave continues until a call to noTone(). The pin
can be connected to a piezo buzzer or other speaker to play tones.
Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone() will have
no effect. If the tone is playing on the same pin, the call will set its frequency.
Use of the tone() function will interfere with PWM output on pins 3 and 11.
It is not possible to generate tones lower than 31Hz.
Syntax
void noTone(int pin)
Parameters
pin: The Arduino pin on which to generate the tone.
Stops the generation of a square wave triggered by tone(). Has no effect if no tone is being generated.
Having known the two functions, you may grasp the codes—the installation of the array melody[] and the array
noteDurations[] is the preparation of the subsequently several times of calling of the function tone() and the
changing of tone and duration in the loop for better effect of music play.
2. pitches.h: The code uses an extra file, pitches.h. This file contains all the pitch values for typical notes. For
example, NOTE_C4 is middle C. NOTE_FS4 is F sharp, and so forth. This note table was originally written by
Brett Hagman, on whose work the tone() command was based. You may find it useful whenever you want to
make musical notes.
#include "pitches.h"
Note: There is already a pitches.h file in this sample program. If we put it together with the main code in one
folder, the successive steps of installing pitches.h can be omitted.
4.5. 5. More Syntax 153

Related product manuals