Hey,
I’m making a little project that is able to play simple songs. I have an Argon with a little speaker connected to it, and send it a ‘play’ command via the cloud. After playing my little song, the led stays breathing the normal light blue for a few seconds, beeps twice again and crashes. The led flashes red: SOS 1 SOS 1.
If I remove the speaker, and leave the pin (D5) unconnected, it crashes in the same way.
If I comment out the ‘tone()’ line in the code, everything works perfectly (but no sound, obviously).
What am I overlooking?
Thanks!
int songPin = 5;
bool songReceived = false; //flag for loop
unsigned long toneStart = 0; //milliseconds, to time tones
int songIndex = 0; //which note of the song we are going to play
const int notes[]={262,294,329,349,392,440,494,523}; //in Hz. do re mi fa sol la si do
const int song[]={0,1,2,3};
const int noteLength = 500; //milliseconds
int receiveSong(String msg){
songReceived = true;
return 1;
}
boolean playSong() {
//first note
if(songIndex == 0){
toneStart = millis()-(noteLength+1);
}
//playing note
if(millis() - toneStart > noteLength){
noTone(songPin);
tone(songPin,notes[song[songIndex]]);
toneStart = toneStart + noteLength;
songIndex ++;
}
//checking for end of song
if(songIndex < sizeof(song)){
return true;
}else{
songIndex = 0;
noTone(songPin);
return false;
}
}
void setup() {
Particle.function("song",receiveSong);
pinMode(songPin, OUTPUT);
}
void loop() {
if(songReceived){
songReceived = playSong();
}
}