Hey, I could use a little help. I have three touch sensors that when touched play three different sounds. My setup currently works well but I am trying to change it to make them have to be played in a specific order before a pin is set HIGH/LOW to make a relay trip. I have adjusted my code to what is below. The first "if" statement in my loop doesn't need to be a part of the "for", just the next three "if"s.
When compiling an error is returned saying 'playfile' was not declared in this scope. It doesn't give this error until then line commented below. Does anyone see the problem or an issue with my code otherwise? Not really sure if what I am trying will work but I am hoping that if Pin2 is read HIGH then a sound will play and "x" will increment, thus allowing pin3 to be read HIGH and x==1 playing that sound and in turn Pin4. Each sound should play whether it is in the right order or not but if the value of "x" isn't correct for that digitalRead of the specific pin then I want the "return" in the "else" statement to stop running the loop and restart it, thus resetting to x==0.
Thanks for any help.
#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
SdReader card;
FatVolume vol;
FatReader root;
FatReader f;
WaveHC wave;
short pin1=17;
short pin2=7;
short pin3=8;
short pin4=9;
int freeRam(void)
{
extern int __bss_end;
extern int *__brkval;
int free_memory;
if((int)__brkval == 0) {
free_memory = ((int)&free_memory) - ((int)&__bss_end);
}
else {
free_memory = ((int)&free_memory) - ((int)__brkval);
}
return free_memory;
}
void sdErrorCheck(void)
{
if (!card.errorCode()) return;
while(1);
}
void setup() {
byte i;
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(pin1,INPUT);
digitalWrite (pin1, HIGH);
pinMode(pin2,INPUT);
digitalWrite (pin2, HIGH);
pinMode(pin3,INPUT);
digitalWrite (pin3, HIGH);
pinMode(pin4,INPUT);
digitalWrite (pin4, HIGH);
pinMode(15, OUTPUT);
digitalWrite (15, HIGH);
if (!card.init()) {
sdErrorCheck();
while(1);
}
card.partialBlockRead(true);
uint8_t part;
for (part = 0; part < 5; part++) {
if (vol.init(card, part))
break;
}
if (part == 5) {
sdErrorCheck();
while(1);
}
if (!root.openRoot(vol)) {
while(1);
}
}
void loop() {
byte i;
;
if(digitalRead(pin1)==HIGH) {
playfile("SOUND1.WAV");
while (wave.isplaying) {}
wave.stop();
}
for (x = 0; x < 3; x ++)
{
if(digitalRead(pin2)==HIGH && x==0) {
playfile("SOUND2.WAV");
while (wave.isplaying) {}
wave.stop();
x++;
else {
playfile("SOUND2.WAV");
while (wave.isplaying) {}
wave.stop();
return;
}
}
if(digitalRead(pin3)==HIGH && x==1) {
playfile("SOUND3.WAV");
while (wave.isplaying) {}
wave.stop();
x++;
else {
playfile("SOUND3.WAV"); //this is the offending line of code
while (wave.isplaying) {}
wave.stop();
return;
}
}
if(digitalRead(pin4)==HIGH && x==2) {
playfile("SOUND4.WAV");
while (wave.isplaying) {}
wave.stop();
digitalWrite (15, LOW);
delay(100);
digitalWrite (15, HIGH);
delay(100);
else {
playfile("SOUND4.WAV");
while (wave.isplaying) {}
wave.stop();
return;
}
}
delay(500);
}
void playfile(char *name) {
if (wave.isplaying) {
wave.stop();
}
if (!f.open(root, name)) {
return;
}
if (!wave.create(f)) {
return;
}
wave.play();
}