Grumpy_Mike:
I'm actually wanting the value of the rpm to CONSTANTLY change the volume of the sound.
So why would not any of the previous solutions do that?
because the volume is not meant to be constantly playing in a loop - its meant to be changing real time! not sure if im being very clear of what i'm trying to do!
So I made an optical tachometer to read the rpm and hooked that up to the speakers.
This is the code I am using - is there anything wrong with it?
int ledPin = 13; // IR LED connected to digital pin 13
int statusPin = 12; // LED connected to digital pin 12
volatile byte rpmcount;
volatile int status;
unsigned int rpm;
unsigned long timeold;
int bikePlaying = 0;
byte volume = 255;
void setup()
{
Serial.begin(9600);
attachInterrupt(0, rpm_fun, RISING);
Serial.write('v'); //Serial message for volume
Serial.write(volume); //Set volume at lowest value
rpmcount = 0;
rpm = 0;
timeold = 0;
}
void loop()
{
if (rpmcount >= 1) {
//Update RPM every 1 count, increase this for better RPM resolution,
//decrease for faster update
rpm = 30*1000/(millis() - timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
Serial.println(rpm,DEC);
if((rpm >= 80) && (bikePlaying == 0)){ //if a certain RPM is reached
Serial.write('t'); //Play Track 2 stored on MP3 Trigger
Serial.write(2);
bikePlaying = 1;
while (volume != 1) {
fadeIn(); //Fade volume in from min to max.
}
delay(3000);
}
if ((rpm <= 40) && (bikePlaying == 1)) { //If RPM falls below 40 and MP3 was playing
while (volume < 255) {
fadeOut(); //Fade out,
}
if (volume = 255) { //Then Stop.
Serial.write('O'); //Serial message for "stop"
bikePlaying = 0; //playing is off....
delay(3000);
}
}
}
}
void rpm_fun()
{
rpmcount++;
//Each rotation, this interrupt function is run twice
}
void fadeOut() {
volume += (240/100);
delay(40);
Serial.write('v');
Serial.write(volume);
}
void fadeIn() {
volume -= (240/100);
delay(40);
Serial.write('v');
Serial.write(volume);
}