track counter

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.

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
} 
        
      }

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.

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

}

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

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

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.

Do I see a Serial.begin?
Also, indentation. I really like consistent indentation.

AWOL:
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..

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

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

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.

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.

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

the code is very very long

So will this thread be unless you post the code.

So will this thread be unless you post the code.

My thoughts exactly. :slight_smile:

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. :stuck_out_tongue:

Just my 2 cents.

:slight_smile: