If/else and return question

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();
}

Hi,

I can't find from which library playfile() comes from.

Jacques

Honestly I'm not really sure either. I assume the wav shield library. I have this code working right now without the "For" and "Else" and "Return" coding. Playfile does not give me any issue then but when I added these code items in to try to require a certain order of sounds being played to activate the final switch it started giving me the "playfile not declared" message. It definitely works in my current rendition of this code that I am running on my arduino right now. I just really wanted it to be able to do a loop reset each time a sound was played out of order.

If you properly indent the code (use tools -> auto format in the IDE), you will probably immediately see what is wrong.

Hint:
After autoformat, every function should start at the beginning of a line; void playfile(char *name) does not start at the beginning of a line which indicates that you have a mismatch between { and }

Ok thanks. I went through after auto formatting and found the errors. You're right, much easier to see them. I got the code to compile but I'm not where I can test it yet.

My question right now is how exactly the "return" command works?

will it restart my void loop() which is what I want it to do?

or

will it restart that particular "if" statement? which is not my intent.

Return exits from the function it's called in, in this case loop. So it'll do what you want.