How can I start and stop a song with a button?


i working on program supposed able randomly play 1 out of 5 songs when button pressed, however, want implement way able push same button turn song off, , after press change random song. currently, program can start random song pushing button, won't let me stop early. once end though, can press again change song. here program far:

code: [select]

#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 farm");
}
void forgeahead() {
  serial.println("playing forge ahead");
}

once call function play song, don't @ switch again until function ends. if want switch (not button. buttons shirts) stop song, must read switch state while song playing.


Arduino Forum > Using Arduino > Programming Questions > How can I start and stop a song with a button?


arduino

Comments

Popular posts from this blog

Error compiling for board Arduino/Genuino Uno.

Installation database is corrupt

esp8266 (nodemcu 0.9) client.write très lent ???