#include <SD.h> // need to include the SD library
#define SD_ChipSelectPin 10 //using digital pin 4 on arduino nano 328, can use other pins
#include <TMRpcm.h> // also need to include this library...
#include <SPI.h>
TMRpcm tmrpcm; // create an object for use in this sketch
unsigned long time = 0;
int numLoop = 0;
int throttle = 0;
int prevThrottle = 0;
int currThrottle = 0;
//RC scale
int rc_pin = 2;
unsigned long duration;
void setup(){
tmrpcm.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
Serial.begin(115200);
if (!SD.begin(SD_ChipSelectPin)) { // see if the card is present and can be initialized:
Serial.println("SD fail");
return; // don't do anything more if not
}
else{
Serial.println("SD ok");
}
//PLay init file to tell sound version
tmrpcm.play("init.wav");
while(tmrpcm.isPlaying()){};
}
void loop(){
//Read throttel value
++numLoop;
if (numLoop == 30000){
noInterrupts();
duration = pulseIn(rc_pin, HIGH);
interrupts();
numLoop = 0;
throttle = (duration - 700) / 16;
//Set new throttle value
if(throttle <= 25){
currThrottle = 1;
};
if((throttle >= 25) && (throttle <= 50)){
currThrottle = 2;
};
if((throttle >= 50) && (throttle <= 75)){
currThrottle = 3;
};
if(throttle >= 75){
currThrottle = 4;
};
//If currThrottle != prevThrottle set start playing new file
if(currThrottle != prevThrottle){
if(currThrottle == 1){
tmrpcm.play("idle.wav");
Serial.println("tmrpcm.play(idle.wav);");
prevThrottle = currThrottle;
}
if(currThrottle == 2){
tmrpcm.play("mid-low.wav");
Serial.println("tmrpcm.play(mid-low.wav);");
prevThrottle = currThrottle;
}
if(currThrottle == 3){
tmrpcm.play("mid-high.wav");
Serial.println("tmrpcm.play(mid-high.wav);");
prevThrottle = currThrottle;
}
if(currThrottle == 4){
tmrpcm.play("full.wav");
Serial.println("tmrpcm.play(full.wav);");
prevThrottle = currThrottle;
}
}
}
}