Italy
Offline
Full Member
Karma: 3
Posts: 136
|
 |
« on: September 23, 2012, 05:38:37 pm » |
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 } }
|
|
|
|
« Last Edit: September 23, 2012, 06:31:08 pm by onesky »
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 212
|
 |
« Reply #1 on: September 23, 2012, 06:24:04 pm » |
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: }
|
|
|
|
|
Logged
|
|
|
|
|
Italy
Offline
Full Member
Karma: 3
Posts: 136
|
 |
« Reply #2 on: September 23, 2012, 06:29:15 pm » |
ops sorry i didnt' paste it but it was already included in my code
|
|
|
|
|
Logged
|
|
|
|
|
Milton Keynes UK
Offline
Tesla Member
Karma: 88
Posts: 6286
-
|
 |
« Reply #3 on: September 23, 2012, 06:42:59 pm » |
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
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: September 23, 2012, 08:12:25 pm » |
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.
|
|
|
|
Italy
Offline
Full Member
Karma: 3
Posts: 136
|
 |
« Reply #5 on: September 24, 2012, 11:33:18 am » |
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 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
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #6 on: September 24, 2012, 11:59:30 am » |
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
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: September 24, 2012, 12:26:54 pm » |
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.
|
|
|
|
Italy
Offline
Full Member
Karma: 3
Posts: 136
|
 |
« Reply #8 on: September 24, 2012, 04:32:22 pm » |
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
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #9 on: September 24, 2012, 05:07:50 pm » |
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.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #10 on: September 25, 2012, 05:46:06 am » |
So will this thread be unless you post the code. My thoughts exactly. 
|
|
|
|
|
Logged
|
|
|
|
|
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)
|
 |
« Reply #11 on: September 25, 2012, 07:08:32 am » |
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.  Just my 2 cents. 
|
|
|
|
|
Logged
|
|
|
|
|
|