MP3Trigger serial commands & volume fade.

Hi chaps,

So I'm using an Arduino nano to push serial commands to the robertsonics MP3 Trigger. With my very limited knowledge of coding and a lot of reading I've got it doing what I want. Almost. I can make it play specific tracks, and set the volume to whatever I want but I don't understand how to make it fade the sound in when the track is triggered, and fade-out when it's deactivated.

So this is the small piece of code i'm using to test the one track:

void setup() {

Serial.begin(38400);
}

void loop() {

Serial.write('v');
Serial.write(100); 
 
Serial.write('t');
Serial.write(1);
delay(1500);
 }

Apparently you can get nice smooth sound transitions, becuase you can write to the board quite quickly.. I just don't know how to. I guess I basically need to make it push decreasing numbers to the serial port (0 is the loudest volume setting, 240 is mute) and rising numbers when the switch is deactivated...

I did try this (as a test) but with some very odd results:

void setup() {

Serial.begin(38400);
}

void loop() {
 
Serial.write('t');
Serial.write(1);
delay(1500);

Serial.write('v');
Serial.write(100); 

Serial.write('v');
Serial.write(90); 

Serial.write('v');
Serial.write(80); 

Serial.write('v');
Serial.write(70); 

Serial.write('v');
Serial.write(60); 

Serial.write('v');
Serial.write(50); 

Serial.write('v');
Serial.write(40); 

Serial.write('v');
Serial.write(30); 

Serial.write('v');
Serial.write(20); 

Serial.write('v');
Serial.write(10); 
}

anyone help a noob out? :slight_smile:

So i've managed to get this far (see code below) (should this be in the 'syntax' section of the forums??)

Where it is decreasing the volume incrementally, It's tracing out the correct stuff form serial monitor : "t
v24v48v72v96v120v144v168v192v216v240"

it's playing the track, but doing nothing to the volume. I think the issue is with turning the 'volume' variable into a binary number.

int duration = 3000;
int increment = duration/10;
int 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/10);

Serial.write('v');
Serial.print(volume);

}

should your serial.print(volume) be serial.write(volume) ?

what about nesting a for loop to decrement 240 to 0 (fading the volume up)?

for (i= 240; i > -1; i-=10){
volume = i;
serial.write('v');
serial.write(volume);
. . . .
}