Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15265

MP3 player occassional malfunction?

$
0
0

I'm using Arduino Mega, using the YX5300 module to play audio files on an SD card, here'd be the simplified part of the code I'm running:

#include "MD_YX5300.h"
#define INSERT 6
#define PUSH 5
#define MOTOR 22
bool ACCESS = false;
bool RUN = false;
bool PRESS = false;
int TIME;
MD_YX5300 MP3(Serial3);

unsigned long HALT_BUTTON = 200;
unsigned long HALT_INPUT = 200;
unsigned long HALT_TIME = 1000;
unsigned long START_BUTTON = 0;
unsigned long START_INPUT = 0;
unsigned long START_TIME = 0;

void setup() {
  Serial.begin(9600);
  Serial3.begin(9600);
  pinMode(INSERT,INPUT_PULLUP);
  pinMode(PUSH,INPUT_PULLUP);
  pinMode(MOTOR,OUTPUT);
  digitalWrite(MOTOR,HIGH);
  MP3.begin();
}

void loop() {
  MP3.playTrackRepeat(1);
  TIME=0;
  while(RUN==false){
    Input();
    if(ACCESS==true){
      Start();
    }
  }
  while(RUN==true){
    while(PRESS==false){
      Count();
    }
    while(PRESS==true){     
      Spin();
    }
  }  
}

void Input(){
  unsigned long CURRENT=millis();
  if(CURRENT-START_INPUT>=HALT_INPUT){
    if(digitalRead(INPUT)==LOW){
      ACCESS=true;
      MP3.playTrack(2);
      START_INPUT=CURRENT;
      unsigned long HALT=CURRENT;
      while(CURRENT-HALT<1000){
        CURRENT=millis();
      }
      MP3.playTrackRepeat(1);
    }
  }
}

void Start(){
  unsigned long CURRENT = millis();
  if(CURRENT-START_BUTTON>=HALT_BUTTON){
    if(digitalRead(PUSH)==LOW){
      MP3.playTrack(3);
      START_BUTTON=CURRENT;
      unsigned long HALT=CURRENT;
      while(CURRENT-HALT<1000){
        CURRENT=millis();
      }
      RUN=true;
      MP3.playTrackRepeat(4);
    }
  }  
}

void Count(){
  unsigned long CURRENT = millis();
  TIME++;
  if(CURRENT-START_BUTTON>=HALT_BUTTON){
    if(digitalRead(PUSH)==LOW){
      MP3.playTrack(5);
      START_BUTTON=CURRENT;
      unsigned long HALT=CURRENT;
      while(CURRENT-HALT<1000){
        CURRENT=millis();
      }          
      PRESS=true;
      MP3.playTrackRepeat(6);
    }
  }
}

void Spin(){
  unsigned long CURRENT=millis();
  if(CURRENT-START_TIME>=HALT_TIME){
    digitalWrite(MOTOR,LOW);
    TIME--;
    START_TIME=CURRENT;
  }
  if(TIME==0){
    digitalWrite(MOTOR,HIGH);
    ACCESS=false;
    RUN=false;
    PRESS=false;
  }  
}

So the code above would be like spinning a DC motor (LOW = working, HIGH = standby) based on how much time it collects, but has to receive an input (e.g. IR sensor) to gain access to run the program first (by pressing a button). After pressing the button to run it, the variable TIME will increase until you press the button again, where you start spinning the motor for as long as the TIME variable.

I'm including sound here for each of the actions, like the "demo" sound (MP3.PlayTrackRepeat(1)) before it gains input, and a sound when an action is done like pressing a button or gaining the input. The sounds work well, but in the Start() part, occasionally the program will run the DC motor rather than doing MP3.playTrack(3) task it's intended too (but afterwards, it continues normally, like the counting and time depleting). What'd be the reason for this and what's the solution?

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 15265

Trending Articles