Pages: [1]   Go Down
Author Topic: track counter  (Read 997 times)
0 Members and 1 Guest are viewing this topic.
Italy
Offline Offline
Full Member
***
Karma: 3
Posts: 153
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
}
       
      }
« Last Edit: September 23, 2012, 06:31:08 pm by onesky » Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 219
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void setup() {
  // put your setup code here, to run once:

}

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

Italy
Offline Offline
Full Member
***
Karma: 3
Posts: 153
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26495
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do I see a Serial.begin?
Also, indentation. I really like consistent indentation.
« Last Edit: September 23, 2012, 08:15:17 pm by AWOL » Logged

"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.

Italy
Offline Offline
Full Member
***
Karma: 3
Posts: 153
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: September 24, 2012, 11:40:10 am by onesky » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 640
Posts: 50331
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26495
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Italy
Offline Offline
Full Member
***
Karma: 3
Posts: 153
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: September 24, 2012, 04:57:49 pm by onesky » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26495
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
the code is very very long
So will this thread be unless you post the code.
Logged

"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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 640
Posts: 50331
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So will this thread be unless you post the code.
My thoughts exactly.  smiley
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley-razz

Just my 2 cents.

smiley
Logged

Pages: [1]   Go Up
Jump to: