Hey guys,
I tried to construct an Arduino program for my pressure sensor to detect a force of 100 or more, then wait for 0.8secs (using a CTC timer program) if the force is continuously above 100, and finally after, to play a music file from an SD card. If the force fell below 100 before 0.8secs, the process must end and return the beginning of pressure detection. But, apparently, after pressure detection, there is absolutely no music playing from audio output. So please help.
The unsuccessful code I've written is below:
#include <SPI.h>
#include <SD.h>
int pressurePin = A0;
int force;
const int chipSelect = 10;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop() {
force = analogRead(pressurePin);
Serial.println(force);
if(force > 350)
{
// initialize Timer1
cli(); // disable global interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
// set compare match register to desired timer count:
OCR1A = 15655; // time set to 0.8s
// turn on CTC mode:
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler:
TCCR1B |= (1 << CS10);
TCCR1B |= (1 << CS12);
// enable timer compare interrupt:
TIMSK1 |= (1 << OCIE1A);
// enable global interrupts:
sei();
}
else
{
return;
}
delay(100);
}
ISR(TIMER1_COMPA_vect)
{
File dataFile = SD.open("ADEFIR~1.AIF");
Serial.write(dataFile.read());
delay(5000);
}