Skip to content

Instantly share code, notes, and snippets.

@MLXXXp
Created June 7, 2016 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MLXXXp/21aee6e346c9ef17be19f43f4e9ed464 to your computer and use it in GitHub Desktop.
Save MLXXXp/21aee6e346c9ef17be19f43f4e9ed464 to your computer and use it in GitHub Desktop.
// A demonstration of the ArduboyPlaytune library used with the Arduboy
// A short score is played repeatedly in the background.
// The current settings are displayed and a circle moves across the
// bottom of the screen. If sound is muted the screen color is reversed.
// Buttons perform the folowing functions:
//
// A - Mute sound output by calling Arduboy2::audio.enabled(true).
// B - Unmute the sound by calling Arduboy2::audio.enabled(false).
// UP - Play a 1 kHz tone for 1.5 seconds.
// DOWN - Play a 2 kHz tone for 0.15 seconds.
// RIGHT - Toggle the toneMutesScore() function between true and false.
// LEFT - Reassign the two channels to speaker pins 1 and 2, 1 and 1 or
// 1 and no assignment of the second channel.
#include <Arduboy2.h>
#include <ArduboyPlaytune.h>
// Excerpt from Sinfonia No.12 in A major BWV.798 J.S.Bach
const byte score[] PROGMEM = {
3,232, 0x90,0x45, 0x91,0x39, 1,77, 0x80, 0x81, 0x90,0x44, 0,166, 0x80, 0x90,0x45, 0,166, 0x80, 0x90,0x47,
0x91,0x38, 1,77, 0x80, 0x81, 0x90,0x45, 0,166, 0x80, 0x90,0x44, 0,166, 0x80, 0x90,0x45, 0x91,0x36, 1,77,
0x81, 1,77, 0x91,0x31, 0,166, 0x80, 0x90,0x47, 0,166, 0x80, 0x81, 0x90,0x44, 0,166, 0x80, 0x90,0x45,
0,166, 0x80, 0x90,0x47, 0x91,0x32, 0,166, 0x80, 0x90,0x40, 0,166, 0x80, 0x81, 0x90,0x49, 0,166, 0x80,
0x90,0x40, 0,166, 0x80, 0x90,0x4A, 0x91,0x34, 0,166, 0x80, 0x90,0x40, 0,166, 0x80, 0x81, 0x90,0x4C, 1,77,
0x80, 0x90,0x49, 0x91,0x2D, 0,166, 0x80, 0x90,0x47, 0,166, 0x80, 0x81, 0x90,0x45, 0x91,0x39, 0,166, 0x80,
0x90,0x47, 0,166, 0x80, 0x81, 0x90,0x49, 0x91,0x38, 0,166, 0x80, 0x90,0x4B, 0,166, 0x80, 0x81, 0x90,0x4C,
0x91,0x36, 0,166, 0x80, 0x90,0x4E, 0,166, 0x80, 0x81, 0x90,0x50, 0x91,0x34, 0x92,0x40, 0,166, 0x80, 0x90,0x51,
0,166, 0x80, 0x81, 0x82, 0x90,0x53, 0x91,0x3F, 0,166, 0x81, 0x91,0x40, 0,166, 0x81, 0x91,0x3F, 0,166, 0x80,
0x90,0x51, 0,166, 0x80, 0x81, 0xf0
};
Arduboy2 arduboy;
ArduboyPlaytune tunes(arduboy.audio.enabled);
boolean toneMute = false;
byte channelMode = 0;
byte circlePos = 7;
void setup()
{
arduboy.begin();
if (!arduboy.audio.enabled()) {
arduboy.invert(true);
}
// audio setup
tunes.initChannel(PIN_SPEAKER_1);
tunes.initChannel(PIN_SPEAKER_2);
}
void loop ()
{
if (arduboy.pressed(UP_BUTTON)) {
tunes.tone(1000, 1500);
}
if (arduboy.pressed(DOWN_BUTTON)) {
tunes.tone(2000, 150);
}
if (arduboy.pressed(LEFT_BUTTON)) {
tunes.stopScore();
tunes.closeChannels();
switch (channelMode) {
case 0:
channelMode = 1;
tunes.initChannel(PIN_SPEAKER_1);
tunes.initChannel(PIN_SPEAKER_1);
break;
case 1:
channelMode = 2;
tunes.initChannel(PIN_SPEAKER_1);
break;
case 2:
channelMode = 0;
tunes.initChannel(PIN_SPEAKER_1);
tunes.initChannel(PIN_SPEAKER_2);
break;
}
}
if (arduboy.pressed(RIGHT_BUTTON)) {
tunes.toneMutesScore(toneMute = !toneMute);
}
if (arduboy.pressed(A_BUTTON)) {
arduboy.invert(true);
arduboy.audio.off();
}
if (arduboy.pressed(B_BUTTON)) {
arduboy.invert(false);
arduboy.audio.on();
}
// play the tune if we aren't already
if (!tunes.playing()) {
tunes.playScore(score);
}
update();
delay(100);
}
void update() {
arduboy.clear();
arduboy.setCursor(25, 3);
arduboy.print("Playtune Test");
arduboy.setCursor(6, 16);
arduboy.print("Audio enabled: ");
arduboy.print(arduboy.audio.enabled() ? "YES" : "NO");
arduboy.invert(!arduboy.audio.enabled());
arduboy.setCursor(6, 25);
arduboy.print("Channel pins: ");
switch (channelMode) {
case 0:
arduboy.print("1, 2");
break;
case 1:
arduboy.print("1, 1");
break;
case 2:
arduboy.print("1, X");
break;
}
arduboy.setCursor(6, 34);
arduboy.print("toneMutesScore: ");
arduboy.print(toneMute ? "YES" : "NO");
arduboy.fillCircle(circlePos, 54, 7, WHITE);
circlePos += 8;
if (circlePos > 119) {
circlePos = 7;
}
arduboy.display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment