/*
* Sound on ATTiny85 sandbox. Testing only.
*/
#include "pitches.h"
#define SPEAKER_PIN 4 // 0 = Pin 8, 4 = Pin 3
int gameTones[] = { NOTE_G3, NOTE_C4, NOTE_E4, NOTE_G5};
int melody[] = {
440, 440, 659, 587, 659, 659, 659, 659, 988, 988, 988, 740, 880, 0 , 0 , 392, 440, 440, 494, 440, 330, 330 };//, 0, 392, 440, 392, 440, 494, 494, 440, 330, 330, 0, 392, 440, 392, 440, 494, 587, 659, 587, 494, 587, 659, 440, 659, 659, 440, 659, 659, 740, 659, 587, 494, 440, 494, 494, 587, 659, 659, 440, 0, 494, 587, 494, 587, 659, 0, 659, 740, 740, 659, 659, 587, 587, 494, 587, 494, 587, 659, 0, 659, 587, 494, 587, 440, 440, 370, 330, 440, 330, 330, 330, 330, 0, 392, 440, 392, 440, 494, 587, 494, 440, 392, 440, 330, 440, 440, 0, 440, 440, 440, 330, 294, 330, 0, 392, 440, 392, 440, 494, 587, 659, 587, 494, 587, 659, 440, 659, 659, 440, 659, 659, 740, 659, 587, 494, 440, 494, 494, 587, 659, 659, 440, 0, 494, 587, 494, 587, 659, 0, 659, 740, 659, 587, 494, 587, 494, 587, 659, 0, 659, 587, 494, 587, 440, 370, 330, 440, 330, 330, 330, 330, 0, 392, 440, 392, 440, 494, 587, 494, 440, 392, 440, 330, 440, 440, 0, 0
//};
int noteDurations[] = {
248, 248, 248, 248, 496, 372, 124, 248, 124, 124, 248, 248, 496, 496, 248, 248, 496, 248, 248, 496, 496, 496 }; //, 248, 248, 496, 248, 248, 496, 496, 496, 496, 496, 248, 248, 496, 248, 248, 496, 496, 372, 124, 248, 248, 496, 248, 248, 496, 248, 248, 496, 496, 248, 248, 248, 248, 496, 248, 248, 496, 496, 496, 496, 248, 248, 248, 248, 496, 248, 248, 496, 496, 496, 496, 496, 496, 248, 248, 248, 248, 496, 496, 248, 248, 248, 248, 496, 248, 248, 496, 248, 248, 496, 496, 496, 248, 248, 496, 248, 248, 496, 496, 496, 248, 248, 496, 248, 248, 496, 248, 248, 496, 248, 248, 496, 496, 248, 248, 496, 248, 248, 496, 496, 248, 248, 248, 248, 496, 248, 248, 496, 248, 248, 496, 496, 248, 248, 248, 248, 496, 248, 248, 496, 496, 496, 496, 248, 248, 248, 248, 496, 248, 248, 992, 992, 992, 248, 248, 248, 248, 496, 496, 248, 248, 248, 248, 744, 248, 496, 248, 248, 496, 496, 496, 248, 248, 496, 248, 248, 496, 496, 496, 248, 248, 496, 248, 248, 496, 496, 0
//};
void setup() {
pinMode(SPEAKER_PIN, INPUT);
}
void loop() {
// Startup for Japanse lantern
//for(int i=0; i<sizeof(melody); i++) {
for(int i=0; i<15; i++) {
beep(SPEAKER_PIN, melody[i], noteDurations[i]);
//delay(100);
}
// Fanfare
/*
beep(262, 170);
delay(50);
beep(262, 170);
delay(30);
beep(294, 150);
delay(30);
beep(262, 200);
beep(294, 200);
beep(523, 600);
*/
//for(int i=0; i<3; i++) {
// beep(SPEAKER_PIN, gameTones[i], 150);
//delay(300);
// }
// NOTE_B0
//for(int i=0; i<10; i++) {
// beep(SPEAKER_PIN, NOTE_B0, 150);
// delay(100);
//}
/*
Startup sound:
C3 E3 G3
E3 G3 B3
C4
*/
// Jingle for Game85
/*
byte mainDelay = 120;
beep(SPEAKER_PIN, NOTE_C3, mainlen);
beep(SPEAKER_PIN, NOTE_E3, mainlen);
beep(SPEAKER_PIN, NOTE_G3, mainlen);
delay(200);
beep(SPEAKER_PIN, NOTE_E3, mainlen);
beep(SPEAKER_PIN, NOTE_G3, mainlen);
beep(SPEAKER_PIN, NOTE_B3, mainlen);
delay(500);
beep(SPEAKER_PIN, NOTE_C4, 100);
*/
// Game Over sound
/*
beep(SPEAKER_PIN, NOTE_C3, 300);
beep(SPEAKER_PIN, NOTE_E3, 300);
beep(SPEAKER_PIN, NOTE_D3, 600);
beep(SPEAKER_PIN, NOTE_C3, 200);
*/
/* intro 1
beep(SPEAKER_PIN, NOTE_C2, 800);
delay(100);
beep(SPEAKER_PIN, NOTE_C2, 300);
delay(50);
beep(SPEAKER_PIN, NOTE_E2, 300);
delay(50);
beep(SPEAKER_PIN, NOTE_F2, 400);
delay(50);
beep(SPEAKER_PIN, NOTE_G2, 200);
*/
/* intro 2
//C2 C3 D2 D3 E2 E3_
beep(SPEAKER_PIN, NOTE_C2, 200);
beep(SPEAKER_PIN, NOTE_C3, 250);
delay(100);
beep(SPEAKER_PIN, NOTE_D2, 200);
beep(SPEAKER_PIN, NOTE_D3, 250);
delay(100);
beep(SPEAKER_PIN, NOTE_E2, 200);
beep(SPEAKER_PIN, NOTE_E3, 400);
*/
// Intro 3
//C2 C2 D2 C2 D2 C3_
/*
beep(SPEAKER_PIN, NOTE_C4, 170);
delay(50);
beep(SPEAKER_PIN, NOTE_C4, 170);
delay(30);
beep(SPEAKER_PIN, NOTE_D4, 150);
delay(30);
beep(SPEAKER_PIN, NOTE_C4, 220);
beep(SPEAKER_PIN, NOTE_D4, 200);
beep(SPEAKER_PIN, NOTE_C5, 600);
*/
// Splash screen song (pretty lame tbh.)
/*
int len = 400;
playPart1(len);
beep(SPEAKER_PIN, NOTE_C5, len);
beep(SPEAKER_PIN, NOTE_E5, len);
beep(SPEAKER_PIN, NOTE_C5, len);
beep(SPEAKER_PIN, NOTE_E5, len);
beep(SPEAKER_PIN, NOTE_G5, len);
beep(SPEAKER_PIN, NOTE_F5, len);
beep(SPEAKER_PIN, NOTE_D5, 700);
//delay(50);
playPart1(len);
delay(50);
beep(SPEAKER_PIN, NOTE_B5, len);
beep(SPEAKER_PIN, NOTE_G5, len);
beep(SPEAKER_PIN, NOTE_A5, len);
beep(SPEAKER_PIN, NOTE_F5, len);
beep(SPEAKER_PIN, NOTE_G5, len);
beep(SPEAKER_PIN, NOTE_E5, len);
beep(SPEAKER_PIN, NOTE_C5, 600);
*/
// Landing sound, minor victory
/*
byte d = 150;
byte d2 = 30;
beep(SPEAKER_PIN, 523, 150);
beep(SPEAKER_PIN, 784, 150);
beep(SPEAKER_PIN, 1047, 150);
*/
delay(5000);
}
void playPart1(int len) {
beep(SPEAKER_PIN, NOTE_C5, len);
beep(SPEAKER_PIN, NOTE_E5, len);
beep(SPEAKER_PIN, NOTE_C5, len);
beep(SPEAKER_PIN, NOTE_E5, len);
beep(SPEAKER_PIN, NOTE_G5, len);
beep(SPEAKER_PIN, NOTE_F5, len);
beep(SPEAKER_PIN, NOTE_E5, len);
beep(SPEAKER_PIN, NOTE_D5, len);
}
void beep (int frequencyInHertz, long timeInMilliseconds) {
beep(SPEAKER_PIN, frequencyInHertz, timeInMilliseconds);
}
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)
{ // http://web.media.mit.edu/~leah/LilyPad/07_sound_code.html
int x;
long delayAmount = (long)(1000000 / frequencyInHertz);
long loopTime = (long)((timeInMilliseconds * 1000) / (delayAmount * 2));
pinMode(speakerPin, OUTPUT);
for (x = 0; x < loopTime; x++) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(delayAmount);
digitalWrite(speakerPin, LOW);
delayMicroseconds(delayAmount);
}
pinMode(speakerPin, INPUT);
}
/* ORIGINAL
https://wokwi.com/projects/285525640477671948
*/
// ????????????????????????????????????????????????????????????????????????????????????????????
// ????????????????????????????????????????????????????????????????????????????????????????????
// ????????????????????????????????????????????????????????????????????????????????????????????
/*
#include "pitches.h"
#define SPEAKER_PIN 0
const uint8_t buttonPins[] = { 12, 11, 10, 9, 7, 6, 5, 4 };
const int buttonTones[] = {
NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4,
NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};
const int numTones = sizeof(buttonPins) / sizeof(buttonPins[0]);
void setup() {
pinMode(SPEAKER_PIN, OUTPUT);
}
void loop() {
int pitch = 0;
for (uint8_t i = 0; i < numTones; i++) {
pitch = buttonTones[i];
if (pitch) {
tone(SPEAKER_PIN, pitch);
} else {
noTone(SPEAKER_PIN);
}
}
delay(1500);
}
*/
// ????????????????????????????????????????????????????????????????????????????????????????????
// ????????????????????????????????????????????????????????????????????????????????????????????
// ????????????????????????????????????????????????????????????????????????????????????????????
/*
// TinyTone for ATtiny85
// Notes
const int Note_C = 239;
const int Note_CS = 225;
const int Note_D = 213;
const int Note_DS = 201;
const int Note_E = 190;
const int Note_F = 179;
const int Note_FS = 169;
const int Note_G = 159;
const int Note_GS = 150;
const int Note_A = 142;
const int Note_AS = 134;
const int Note_B = 127;
int Speaker = 0; // A2/3
void setup()
{
pinMode(Speaker, OUTPUT);
}
void loop()
{
playTune();
delay(3000);
}
void TinyTone(unsigned char divisor, unsigned char octave, unsigned long duration)
{
int frequencyInHertz = 1;
//duration = (long)(1000000 / frequencyInHertz);
TCCR1 = 0x90 | (8-octave); // for 1MHz clock
//TCCR1 = 0x90 | (11-octave); // for 8MHz clock
OCR1C = divisor-1; // set the OCR
delay(duration);
TCCR1 = 0x90; // stop the counter
}
// Play a scale
void playTune(void)
{
TinyTone(Note_C, 4, 500);
TinyTone(Note_D, 4, 500);
TinyTone(Note_E, 4, 500);
TinyTone(Note_F, 4, 500);
TinyTone(Note_G, 4, 500);
TinyTone(Note_A, 4, 500);
TinyTone(Note_B, 4, 500);
TinyTone(Note_C, 5, 500);
}
*/