Trouble with my switch case

Hello. I have been having some problems using the Arduino Waveshield, and I've reduced them to something wrong with my switch case statement - I want it to play different sounds when I feed it numbers via the serial monitor in the IDE, but it's not even getting to the switch case - I know this because I included a default, which constantly spits out the line "dogs and cats", and doesn't stop when I send one of the number keys into the serial port.

I am aware I am probably doing something really simple worng - precisely what that thing might be is what I hope to ascertain with this post :slight_smile:

Here is my code:

#include <AF_Wave.h>
#include <avr/pgmspace.h>
#include "util.h"
#include "wave.h"

AF_Wave card;
File f;
Wavefile wave;
#define redled 9
void setup() {

// set up serial port
Serial.begin(9600);

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(redled, OUTPUT);

// open memory card
if (!card.init_card()) {
putstring_nl("Card init failed!"); return;
}
if (!card.open_partition()) {
putstring_nl("No partition!"); return;
}
if (!card.open_filesys()) {
putstring_nl("Couldn't open filesys"); return;
}
if (!card.open_rootdir()) {
putstring_nl("Couldn't open dir"); return;
}
}

void loop() {
int c = 0;
if (Serial.available()) {
c = Serial.read();
//Serial.println(c, BYTE);
}

switch (c) {
case 1:
playfile("1.WAV")
break;
case 2:
playfile("2.WAV");
break;
case 3:
playfile("3.WAV");
break;
case 4:
playfile("4.WAV");
break;
case 5:
playfile("5.WAV");
break;
case 6:
playfile("6.WAV");
default:
Serial.println("dogs and cats");
}
}

void playfile(char *name) {
Serial.print("bang");
// stop any file already playing
if (wave.isplaying) {
wave.stop();
card.close_file(f);
}
// close file if open

// play specified file
f = card.open_file(name);
if (f && wave.create(f)) {
wave.play();
}
}

For a start, I think: if(Serial.available()) should be: while(Serial.available()) because this will only operate the function when it checks if Serial.available is 'TRUE' and it is. Whereas my example will operate the function when Serial.available is 'TRUE'.

Ben aka /me

Try this:

 switch (c) {
   case '1':
     playfile("1.WAV")
     break;
   case '2':
     playfile("2.WAV");
     break;
   case '3':
     playfile("3.WAV");
     break;
   case '4':
     playfile("4.WAV");
     break;
   case '5':
     playfile("5.WAV");
     break;
   case '6':
     playfile("6.WAV");
   default:
   Serial.println("dogs and cats");
  }

It is important to remember that the data recieved on the serial is of the char datatype. Not int

Success! I switched the data type to char for the switch input. Thank you for mentioning that, I hadn't thought of it. I also replaced my switch structure with yours - am I correct that the '1' specifies is as an ascii equivalent? Also, I replaced my if(Serial.available) with a when(Serial.available). Also seems to be working well! Thank you both for your help.

Best Regards, Jeremy

... am I correct that the '1' specifies is as an ascii equivalent?

Yes.

'1' == 49