system
March 26, 2010, 5:05pm
1
So i'm trying to use the Arduino to push binary commands to an MP3Trigger via serial, I have simple stuff working but can't seem to get the volume changes I need. I think it's just an issue with my code, and the fact that I can't seem to get the volume data out as an unsigned single byte.
I've been discussing it on the makerjam forum here too:
http://makerjam.com/forum/mp3-trigger/volume-fade-on-mp3trigger/
this is where i'm at with the code.
int duration = 10000;
int increment = duration/100;
int volume = 0;
void setup(){
Serial.begin(38400);
Serial.write('t');
Serial.write(1);
}
void loop()
{
if (volume < 40) {
turnDownVolume();
delay(increment);
}
}
void turnDownVolume() {
volume += (40/40);
char vol[10];
itoa(volume, vol, 10);
Serial.write('v');
Serial.write(volume);
}
system
March 26, 2010, 5:10pm
2
I can't seem to get the volume data out as an unsigned single byte
int volume = 0;
Maybe if it were an unsigned byte to begin with?
can't seem to get the volume changes I need.
This doesn't really tell us what changes you do need, and how the ones you obtain differ from this ideal.
system
March 26, 2010, 5:30pm
3
void turnDownVolume() {
volume += (40/40);
Why does a function named turnDownVolume increment volume?
Why is there an unnecessary divide operation?
Serial.write is for sending numbers, not strings. Serial.print is for sending strings.
If you really wanted to send volume as a byte, why are you converting it to a string?
system
March 26, 2010, 5:48pm
4
Sorry (That was a version of the code I'd been messing with)
This is what i'm currently using.
When I say i'm not getting the volume changes, I mean I'm not getting any volume changes.
int duration = 10000;
int increment = duration/100;
int volume = 1;
void setup(){
Serial.begin(38400);
Serial.write('t');
Serial.write(1);
}
void loop()
{
if (volume < 40) {
turnDownVolume();
delay(increment);
}
}
void turnDownVolume() {
volume += 1;
Serial.write('v');
Serial.write(volume);
}
I've tried pushing volume commands to the board without the maths, which works fine (it alters the volume of track 1 every 5 seconds)
void setup() {
Serial.begin(38400);
Serial.write('t');
Serial.write(1);
}
void loop() {
Serial.write('v');
Serial.write(1);
delay(5000);
Serial.write('v');
Serial.write(20);
delay(5000);
Serial.write('v');
Serial.write(40);
delay(5000);
Serial.write('v');
Serial.write(60);
delay(5000);
Serial.write('v');
Serial.write(100);
delay(5000);
}
I think what I need to do is convert volume to an unsigned single byte before I send it to the trigger... I just don't know how to do that. :-X
system
March 26, 2010, 6:34pm
5
Just declare volume to be a byte, instead of an int.
The serial.write(volume) is writing the high order byte first, then the low order byte. Since the high order byte is 0, no volume change occurs.
Rename the function, though, to reflect what is is actually doing, which is increasing the volume.
system
March 26, 2010, 6:42pm
6
Fantastic, that works thanks Paul, It is actually decreasing the volume at the moment (0 is the loudest, 240 being absolute dead mute on the MP3Trigger)
So the updated code is (oh and I increased the duration to 10 seconds to make sure it was working):
int duration = 10000;
int increment = duration/100;
byte volume = 0;
void setup(){
Serial.begin(38400);
Serial.write('t');
Serial.write(1);
}
void loop() {
if (volume < 240) {
turnDownVolume();
delay(increment);
}
}
void turnDownVolume() {
volume += (240/100);
Serial.write('v');
Serial.write(volume);
}
thanks again.
system
March 26, 2010, 7:01pm
7
Just keep in mind that 240/100 = 2.