Hi there, I've attempted to make a speaker work from a force resistor, reading off an SD card. Problem is, everything in my sketch works except for the sound.
Could anyone give me a heads up as to why this isn't playing sound files?
Thank you, the sketch is below.
#include <SD.h> // need to include the SD library
//#define SD_ChipSelectPin 53 //example uses hardware SS pin 53 on Mega2560
#define SD_ChipSelectPin 10 //using digital pin 4 on arduino nano 328
#include <TMRpcm.h>
#include <SPI.h>
TMRpcm tmrpcm; // create an object for use in this sketch
char mychar;
/*This code allows a force resistor to dictate which Led will turn on.
Depending on the threshold, 3 alternating coloured Leds will turn on to signify
the power of a force. A 3 way switch turns the program to level 1, 2 or off.
*/
const int positionA = 2; // Switch position A
const int positionB = 3; // Switch position B
int ledPinA = 8; // LED is connected to digital pin 7
int ledPinB = 7; // LED is connected to digital pin 8
int ledPinC = 6; // LED is connected to digital pin 9
int forceSensor = A0; // Force sensor is connected to analog pin 0
int sensorValue; // variable to store the value coming from the sensor
long previousMillis = 0; // will store last time LED was updated
const int chipSelectPin = 10;
// the follow variables is a long because the time, measured in milliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 100; // interval at which to blink (milliseconds)
void setup()
{
pinMode(ledPinA, OUTPUT); // sets the ledPin to be an output
pinMode(ledPinB, OUTPUT); // sets the ledPin to be an output
pinMode(ledPinC, OUTPUT); // sets the ledPin to be an output
pinMode(positionA, INPUT); // sets the switch positon A as an input
pinMode(positionB, INPUT); // sets the switch position B as an input
pinMode (10, OUTPUT); // sets the Sd card slot as an output
tmrpcm.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc
Serial.begin(9600); //initialize the serial port
if (!SD.begin(SD_ChipSelectPin)) { // see if the card is present and can be initialized:
return; // don't do anything more if not
}
}
void loop() // run over and over again
{
int stateA = digitalRead(2);
int stateB = digitalRead(3);
{
sensorValue = analogRead(forceSensor); // read the value from the sensor
Serial.print(" Sensor Value: ");
Serial.println(sensorValue);
delay(100); // delay for 1/10 of a second
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
{if(stateA == HIGH && sensorValue>975){ //You can change the number here to adjust the threshold.
Serial.print("HIGH");
digitalWrite(ledPinC, HIGH); //Turn LED on
tmrpcm.play ("Hit5c.wav");
delay (4000); //Keep LED on for 4 seconds
digitalWrite (ledPinC, LOW); //Turn LED off
}
else if( stateA == HIGH && sensorValue>700){ //You can change the number here to adjust the threshold.
Serial.print("Medium");
digitalWrite(ledPinB, HIGH); //Turn LED on
tmrpcm.play ("Hit3c.wav");
delay (4000); //Keep LED on for 4 seconds
digitalWrite (ledPinB, LOW); //Turn LED off
}
else if( stateA == HIGH && sensorValue>300){ //You can change the number here to adjust the threshold.
Serial.print("Low");
digitalWrite(ledPinA, HIGH); //Turn LED 7 on
tmrpcm.play ("Hit2c.wav");
delay (4000); //Keep LED on for 4 seconds
digitalWrite (ledPinA, LOW); //Turn LED off
}
{if( stateB == HIGH && sensorValue>700){ //You can change the number here to adjust the threshold.
Serial.print("HIGH");
digitalWrite(ledPinC, HIGH); //Turn LED on
tmrpcm.play ("Hit6c.wav");
delay (4000); //Keep LED on for 4 seconds
digitalWrite (ledPinC, LOW); //Turn LED off
}
else if( stateB == HIGH && sensorValue>350){ //You can change the number here to adjust the threshold.
Serial.print("Medium");
digitalWrite(ledPinB, HIGH); //Turn LED on
tmrpcm.play ("Hit1c.wav");
delay (4000); //Keep LED on for 4 seconds
digitalWrite (ledPinB, LOW); //Turn LED off
}
else if( stateB == HIGH && sensorValue>1){ //You can change the number here to adjust the threshold.
Serial.print("Low");
digitalWrite(ledPinA, HIGH); //Turn LED 7 on
tmrpcm.play ("Hit4c.wav");
delay (4000); //Keep LED on for 4 seconds
digitalWrite (ledPinA, LOW); //Turn LED off
}
else {
Serial.println("none");
digitalWrite(ledPinA, LOW); //Turn LED off
digitalWrite(ledPinB, LOW); //Turn LED off
digitalWrite(ledPinC, LOW); //Turn LED off
}
}
}
}
}
}