
SunFounder Thales Kit for Raspberry Pi Pico, Release 1.0
Code
After uploading the code, when you press the button, you can hear a 7-note melody.
How it works?
#include
"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.
Note:
There is already a pitches.h file in this sample program. If you cannot find pitches.h after downloading or
opening the code, you can just install one manually.
Click the button below the serial monitor icon and select “New Tab”, or use Ctrl+Shift+N.
Then paste from
and save it as
pitches.h
:
int
melody[]
=
{NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3,
0
, NOTE_B3, NOTE_C4};
int
buttonPin
=
14
;
//note durations. 4=quarter note / 8=eighth note
int
noteDurations[]
=
{
4
,
8
,
8
,
4
,
4
,
4
,
4
,
4
};
The array
melody[]
stores 7 notes, and
noteDurations[]
is the duration corresponding to these notes, 4=quarter
note, 8=eighth note.
•
•
int
buttonState
=
digitalRead
(buttonPin);
//read the input pin
//if the button is pressed
if
(buttonState
==
1
) {
//iterate over the notes of the melody
for
(
int
i
=
0
; i
<
8
; i
++
) {
(continues on next page)
158
Chapter 4. For Arduino User
Summary of Contents for Thales Kit
Page 1: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 Jimmy SunFounder Jun 04 2021 ...
Page 2: ......
Page 4: ...ii ...
Page 6: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 2 CONTENTS ...
Page 140: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 136 Chapter 3 For MicroPython User ...
Page 164: ...SunFounder Thales Kit for Raspberry Pi Pico Release 1 0 160 Chapter 4 For Arduino User ...