Go Down

Topic: track counter (Read 1 time) previous topic - next topic

onesky

Sep 24, 2012, 12:38 am Last Edit: Sep 24, 2012, 01:31 am by onesky Reason: 1
i wish to use the mp3 board with arduino as an mp3 player: when the button is pressed it should start to play music of the 1st track and when the button is pressed again it should skip to the 2nd and so on until the track #8. After that it should start again from the first track.

I tried this code but wont work! if i use the Random function it works. I dont' understand why.

Code: [Select]
const int buttonPinFire = 7;   
int TrackCounter = 0;
int val = 0;

void setup() {

  pinMode(buttonPinFire, INPUT);
  }
void loop()
{
        val = digitalRead(buttonPinFire);  // read the pin value and save it
      // control that input is HIGH (button pressed) 
             if (val == HIGH) { 
   
                        // play files sequence in folder advert09
    Serial.write(0x7E);
    Serial.write(0x07);
    Serial.write(0xA0);
    Serial.write(0x30); // folder tens
    Serial.write(0x39); // folder unit
    Serial.write(0x30); // file hundreds
    Serial.write(0x30); // file tens
    Serial.write(0x30+TrackCounter); // file units
    Serial.write(0x7E);
  }
      TrackCounter++;
if        (TrackCounter == 8)  {
TrackCounter = 0; //reset track counter
}
       
      }

PGT

there is no main void loop in your prog, you did good with setup but main a continous loop is required also.
its a loop that always goes on keeps running.

Code: [Select]

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

onesky

ops sorry i didnt' paste it but it was already included in my code

PeterH

I don't get what those serial writes do, but you're doing that whenever you find the button is pressed. I think it would make more sense to do it when the button changes from unpressed to pressed. To achieve that you would need to remember the previous button state and compare it with the new state; if they differ, the button has been pressed or released. It would also be sensible to have a short delay to allow for any hardware bounce.
I only provide help via the forum - please do not contact me for private consultancy.

AWOL

#4
Sep 24, 2012, 03:12 am Last Edit: Sep 24, 2012, 03:15 am by AWOL Reason: 1
Do I see a Serial.begin?
Also, indentation. I really like consistent indentation.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

onesky

#5
Sep 24, 2012, 06:33 pm Last Edit: Sep 24, 2012, 06:40 pm by onesky Reason: 1

Do I see a Serial.begin?
Also, indentation. I really like consistent indentation.
in the original code is included, i just pasted the part on topic..

what is not working is this:

   Serial.write(0x30+TrackCounter); // file units

if i use the random option, it works..

Quote
val = digitalRead(buttonPinFire);  // read the pin value and save it
      // control that input is HIGH (button pressed) 
      if (val == HIGH) { 
                // play files random in folder advert08
                 trackNumber = random (0,6);
     Serial.write(0x7E);
    Serial.write(0x07);
    Serial.write(0xA0);
    Serial.write(0x30); // folder tens
    Serial.write(0x38); // folder unit
    Serial.write(0x30); // file hundreds
    Serial.write(0x30); // file tens
    Serial.write(0x30+trackNumber); // file units
    Serial.write(0x7E);
  }
      }

     

this is the mp3 board manual
http://www.elechouse.com/elechouse/images/product/MP3%20WTM-SD%20module/WTM-SD%20moduleV1.08.pdf

PaulS

In one case, you use trackCounter. In the other, you use trackNumber. We can see how trackCounter is defined, but not how trackNumber is.

AWOL

Quote
in the original code is included, i just pasted the part on topic..
which is precisely why we ask you to post all of your code.

0x30 is better written as '0', IMO.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

onesky

#8
Sep 24, 2012, 11:32 pm Last Edit: Sep 24, 2012, 11:57 pm by onesky Reason: 1

In one case, you use trackCounter. In the other, you use trackNumber. We can see how trackCounter is defined, but not how trackNumber is.


they are same

// Variables will change:
int trackNumber = 0;
int TrackCounter = 0;


the code is very very long
if i remove   "TrackCounter++" the first track plays fine but only that one

AWOL

Quote
the code is very very long

So will this thread be unless you post the code.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Quote
So will this thread be unless you post the code.

My thoughts exactly.  :)

tuxduino

Quote
they are same

// Variables will change:
int trackNumber = 0;
int TrackCounter = 0;


What do you mean by "they are same" ? From those two lines we can only see they've got the same initialization value. Do you expect that changing one will automatically change the other ?

Also, why do you use first capital letter in one case and not in the other ? Don't overlook these small details, or you'll be facing hard-to-solve bugs, especially in long code.
What's worse, you have to semantically interchangeable variables, that are easily confused. If you really need them both, give them a more descriptive name (*).
One last thing... comments like "variables will change" are of little help. Perhaps you could write a description of each variable role in the code, instead. :P

Just my 2 cents.

:)

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview