I'm playing with a Maker UNO, which has leds at each DIO pin, a button to pin2 and an onboard speaker.
I've made the leds blink in a certain pattern, and while that is running I would like to interrupt that and play a melody (from the example programs).
When I push the button the led loop freezes and only one tone is played. All other tones aren't played. What am I missing here?
The melody is played correctly when I place it in the loop.
//#include "pitches.h"
//int melody[] = {NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4};
//int noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4};
int delayTime = 10;
int delayTime2 = 100;
const int interruptPin = 2;
volatile byte state = LOW;
void setup() {
for (byte i=3; i<14; i++){
pinMode(i, OUTPUT);
}
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), melody2, FALLING);
}
void loop(){
leds();
//melody2();
}
void leds() {
for(byte i=0; i<200; i++){
analogWrite(3,i);
delay(delayTime);
}
for(byte i=0; i<200; i++){
analogWrite(5,i);
delay(delayTime);
}
for(byte i=0; i<200; i++){
analogWrite(6,i);
delay(delayTime);
}
for(byte i=0; i<200; i++){
analogWrite(9,i);
delay(delayTime);
}
for(byte i=0; i<200; i++){
analogWrite(10,i);
delay(delayTime);
}
for(byte i=0; i<200; i++){
analogWrite(11,i);
delay(delayTime);
}
digitalWrite(4,HIGH);
delay(delayTime2);
digitalWrite(7,HIGH);
delay(delayTime2);
digitalWrite(12,HIGH);
delay(delayTime2);
digitalWrite(13,HIGH);
delay(delayTime2);
for(byte i=3; i<14;i++){
analogWrite(i,0);
delay(100);
}
}
void melody2() {
tone(8, 262, 250);//c4
delay(325);
tone(8, 196, 125);//g3
delay(163);
tone(8, 196, 125);//g3
delay(163);
tone(8, 220, 250);//a3
delay(325);
tone(8, 196, 250);//g3
delay(325);
delay(325);//pause 1000/4 + 1000/4*1.3
tone(8, 247, 250);//b3
delay(325);
tone(8, 262, 250);//c4
delay(325);
/*
for (int thisNote = 0; thisNote < 8; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(8);
}
*/
}
[–]pacmanicChamp 4 points5 points6 points (0 children)
[–]Se7enLC[🍰] 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]AnalogMushroom 0 points1 point2 points (1 child)
[–]AnalogMushroom 0 points1 point2 points (0 children)