I am working on a program that is supposed to be able to randomly play 1 out of 5 songs when a button is pressed, however, I want to implement a way to be able to push the same button to turn that song off, and after another press change to another random song. Currently, my program can start a random song by pushing the button, but it won't let me stop it early. Once it does end though, I can press again to change the song. Here is the program so far:
#include "pitches.h"
long randNumber;
const int tempo=72;
const int buzzer=7;
const int button2=11;
const int noteSpace=10;
int eighth=tempo*6;
int quarter=tempo*12;
int half=tempo*24;
int dotHalf=tempo*36;
int whole=tempo*48;
int button2State=0;
int lastButton2State=0;
void setup() {
pinMode(buzzer, OUTPUT);
Serial.begin(9600);
}
void loop() {
int button2State = digitalRead(button2);
if(button2State==HIGH){
randomSeed(analogRead(0));
randNumber = random(1,6);
Serial.println(randNumber);
if(randNumber==1){
journey();
}
if(randNumber==2){
innerLight();
}
if(randNumber==3){
lostLight();
}
if(randNumber==4){
theFarm();
}
if(randNumber==5){
theFarm();
}
}
else {
noTone(buzzer);
}
delay(200);
}
void journey() {
Serial.println("playing Journey");
tone(buzzer, NOTE_G4, eighth);
delay(eighth-noteSpace);
tone(buzzer, NOTE_A4, eighth);
delay(eighth-noteSpace);
tone(buzzer, NOTE_AS4, half);
delay(half-noteSpace);
}
void innerLight() {
Serial.println("playing Inner Light");
}
void lostLight() {
Serial.println("playing Lost Light");
}
void theFarm() {
Serial.println("playing The Farm");
}
void forgeAhead() {
Serial.println("playing Forge Ahead");
}