Pages: [1]   Go Down
Author Topic: Trouble with my switch case  (Read 859 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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

Keynsham, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 224
I like for loops and walks on the beach,
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

"C++ : Where friends have access to your private members." - Gavin Russell Baker

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this:
Code:
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
« Last Edit: April 12, 2009, 04:46:08 pm by AlphaBeta » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Yes.

'1' == 49
Logged

Pages: [1]   Go Up
Jump to: