User Manual

2. OrangutanBuzzerExample2
Demonstrates how you can use this library’s play() method to start a melody playing. Once started,
the melody will play all the way to the end with no further action required from your code, and the rest
of your program will execute as normal while the melody plays in the background. The play() method
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
250, 125, 125, 125, 250, 125, 125, 125, 125, 125, 125, 125, 125, 125,
250, 125, 125, 125, 250, 125, 125, 200, 50, 100, 25, 500,
250, 125, 125, 125, 250, 125, 125, 125, 125, 125, 125, 125, 125, 125,
250, 250, 125, 375, 500
};
OrangutanLCD lcd;
OrangutanPushbuttons buttons;
OrangutanBuzzer buzzer;
unsigned char currentIdx;
void setup() // run once, when the sketch starts
{
currentIdx = 0;
lcd.print("Music!");
}
void loop() // run over and over again
{
// if we haven't finished playing the song and
// the buzzer is ready for the next note, play the next note
if (currentIdx < MELODY_LENGTH && !buzzer.isPlaying())
{
// play note at max volume
buzzer.playNote(note[currentIdx], duration[currentIdx], 15);
// optional LCD feedback (for fun)
lcd.gotoXY(0, 1); // go to start of the second LCD line
lcd.print((unsigned int)note[currentIdx]); // print integer value of current note
lcd.print(" "); // overwrite any left over characters
currentIdx++;
}
// Insert some other useful code here...
// the melody will play normally while the rest of your code executes
// as long as it executes quickly enough to keep from inserting delays
// between the notes.
// For example, let the top user pushbutton function as a stop/reset melody button
if (buttons.isPressed(TOP_BUTTON))
{
buzzer.stopPlaying(); // silence the buzzer
if (currentIdx < MELODY_LENGTH)
currentIdx = MELODY_LENGTH; // terminate the melody
else
currentIdx = 0; // restart the melody
buttons.waitForRelease(TOP_BUTTON); // wait here for the button to be released
}
}
Programming Orangutans and the 3pi Robot from the Arduino
Environment
© 2001–2019 Pololu
Corporation
5. Arduino Libraries for the Orangutan and 3pi Robot Page 40 of 66