How do I start/stop audio with push button??


hi, have been playing code awhile. press push button start wav audio , play on repeat until push same button again. right can press button , play audio repeatly can't stop it. can me? i'm beginner. here code:
code: [select]
#include <sd.h>
#include <tmrpcm.h>
tmrpcm tmrpcm;

int wasplaying = 0;
int finished = 0;
int start = 0;
int buttonpin = 2; //digital pin button is
int buttonstate = 0;


void setup() {
  serial.begin(9600);
  serial.print("\ninitializing sd card...");
  pinmode(10, output);
  if (!sd.begin(10)) {                                  
    serial.println("failed!");
    return;
  }
  serial.println("done.");
  tmrpcm.speakerpin = 9; //digital pin speaker is
}

void loop() {
  buttonstate = digitalread(buttonpin);
  serial.println(buttonstate);
 

  while(buttonstate == high)
  { //if button pressed, test.wav played repeatedly
  (start = 1);
  playnext();
  if(tmrpcm.isplaying())
    while(tmrpcm.isplaying());
  tmrpcm.stopplayback();
  }
}

void playnext() {
  if(wasplaying == 1) {
    serial.println("completed playback.");
    wasplaying = 0;
    finished = 1;
    start = 0;
  }
  tmrpcm.play("test.wav");          
  wasplaying = 1;  

}

code: [select]
while(buttonstate == high)
  { //if button pressed, test.wav played repeatedly
  (start = 1);
  playnext();
  if(tmrpcm.isplaying())
    while(tmrpcm.isplaying());
  tmrpcm.stopplayback();
  }


since never read button inside while loop, buttonstate never changes.  once you're in, it's infinite loop. 


Arduino Forum > Using Arduino > Programming Questions > How do I start/stop audio with push button??


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts