compilation problem with the switch

i have a problem with my case and i get compilation errors.

here is the code

/*
microphone sketch
 
 SparkFun breakout board for Electret Microphone is connected to analog pin 0
 */
 int i=0;
unsigned long currentTime  = 0; //this variable will be overwritten by millis() each iteration of loop
unsigned long pastTime     = 0; //no time has passed yet
unsigned long current  = 0; //this variable will be overwritten by millis() each iteration of loop
unsigned long past     = 0;
char letter[4];
char temp[4];
int           currentState = 0; //the default state
int           wait         = 100; //no need to wait, nothing has happened yet
int           flag         =0;
const int ledPin = 13;            //the code will flash the LED in pin 13
const int middleValue = 512;      //the middle of the range of analog values
const int numberOfSamples = 16;  //how many readings will be taken each time

int sample;                       //the value read from microphone each time
long signal;                      //the reading once you have removed DC offset
long averageReading;              //the average of that loop of readings

long runningAverage=0;          //the running average of calculated values
const int averagedOver= 4;     //how quickly new values affect running average
//bigger numbers mean slower

const int threshold=32000;        //at what level the light turns on

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  long sumOfSquares = 0;
  for (int i=0; i<numberOfSamples; i++) { //take many readings and average them
    sample = analogRead(0);               //take a reading
    signal = (sample - middleValue);      //work out its offset from the center
    signal *= signal;                     //square it to make all values positive
    sumOfSquares += signal;               //add to the total
  }
  averageReading = sumOfSquares/numberOfSamples;     //calculate running average
  runningAverage=(((averagedOver-1)*runningAverage)+averageReading)/averagedOver;

  if (runningAverage>threshold){         //is average more than the threshold ?
    digitalWrite(ledPin, HIGH);    //if it is turn on the LED
    past = current; //currentTime at this point is the current time from the previous iteration, this should now be pastTime
    current = millis();
    unsigned long timePassed2 = current - past;
    if(timePassed2 > 200){ 
      switch(letter){
      case '.':
        temp = 'E';
        return; 
      case '-':
        temp = 'T';
        return; 
      case '.-':
        temp ='A';
        return;
      case '---':
        temp ='O';
        return; 
      case '..':
        temp ='I';
        return;
      case '-.':
        temp='N';
        return;
      case '...':
        temp = 'S';
        return;
      case '....':
        temp = 'H'
          return;
      case '.-.':
        temp = 'R';
        return;
      case '-..':
        temp='D';
        return;
      case '.-..':
        temp='L';
        return;
      case '-.-.':
        temp ='C';
        return;
      case '..-':
        temp = 'U';
        return;
      case '--':
        temp = 'M';
        return;
      case '.--':
        temp = 'W';
        return;
      case '..-.':
        temp = 'F';
        return;
      case '--.':
        temp ='G';
        return;
      case '-.--':
        temp = 'Y';
        return;
      case '.--.':
        temp = 'P';
        return;
      case '-...':
        temp = 'B';
        return;
      case '...-':
        temp = 'V';
        return;
      case '-.-':
        temp = 'K';
        return;
      case '.---':
        temp = 'J';
        return;
      case '-..-':
        temp = 'X';
        return;
      case '--.-':
        temp = 'Q'
          return;
      case '--..':
        temp = 'Z';
        return;
      case ' ':
        delay(wgap);
        return;
      }
      Serial.print(temp);
      i=0;
    }
  }
  else{
    digitalWrite(ledPin, LOW);           //if it isn't turn the LED off

    pastTime = currentTime; //currentTime at this point is the current time from the previous iteration, this should now be pastTime
    currentTime = millis();    //currentTime is now the current time (again).
    unsigned long timePassed = currentTime - pastTime; //this is roll-over proof, if currentTime is small, and pastTime large, the result rolls over to a small positive value, the time that has passed
    if(timePassed > 30 && timePassed <200) //part1 of the state engine
    {
      if(timePassed >= wait) //part1 of the state engine
      {
        letter[i] = '-';
        i++;
      }
      else {
        letter[i] = '.';
        i++;
      }
    } 
  }
}

it receives audio from analog0 and it counts the millis between the the values over the threshold to determin whether it is a dot or a dash.

it worked fine and i had to my serial seperated groups of - and . ! the next step is where the swich comes to check whether the value that i have is equal to on of this and print to my serial the apropriate letter.

case label does not reduse to an integer constant

this the compilation error that i get.

any ideas here ?

where is letter declared is it and int (or similar) or is it an array of something which is how your using it later in the code. By the way its not return but break at the end of a case clause.

Mark

The value used in a case statement can only be an integer or something that can be represented by an integer. Values like .- and --- cannot be represented by an integer, hence the error.

Single quotes are for single characters. Which ONE key on your keyboard did you press to get the ONE character in single quotes here:

      case '.-..':

You've made that same mistake in many other places.

Also, letter is an array. You can't use an array in a switch statement.

ok i get the point.

so probably i need to create a value like this

String morseATable[] = { ".-00", "-...", "-.-.", "-..0", ".000", "..-.", "--.0",   // A-G
                        "....", "..00", ".---", "_._0", ".-..", "--00", "-.00",    // H-N
                        "---0", ".--.", "--.-", ".-.0", "...0", "-000", "..-0",    // O-U
                        "...-", ".--0", "-..-", "-.--", "--.."               // V-Z
                      };

when my mic detectes a word space there is a value named temp (i should better make it a string), that hase the dots and dashes up so far detected. and if the dots and dashes are less than 3 in total it goes with a for loop and fills the rest of the value with zeros.

if i can not use a switch, what would be more eficient comparator according to your opinion ? compare the temp value with the morseATable using a for loop ( and if i do, how would i break out from a for loop ) ant use the i in a switch after that.
but i have to break out from the for loop other wise i have to pass the hole table ( even if the letter is A for example that it is the first),
and with an
```

  • if (temp = morseATable[i]){
    flag=i
    }

switch (flag):{
.................(then i can use '1') // they are integers right ????? :smiley:
}*
```
what do you thing about that ? do you thing that there is a more eficient way of doing it ?

so probably i need to create a value like this

No. There is no reason to piss away resources using the String class to wrap each string. Use char *, instead.

compare the temp value with the morseATable using a for loop

Yes.

and if i do, how would i break out from a for loop

Gee, I don't know. Maybe

break;

Pay attention to = vs. ==.

Use strcmp(), not ==.

PAULS

i have responce from you even to my first post.

i would like to thank you man for the time you spend with noobs like me.

really i am lughing allone right here.

any way, your comments are sarcastic and unique ( in a way ) XD.

again thanks man.

i'll try to send the final code when i get the results i am hopping

kourpetis:
if i can not use a switch, what would be more eficient comparator according to your opinion ?

Once you have got the target strings in an array it would seem natural to implement a function which compares a candidate string against each array element and returns an indication of which element matched (if any). If the goal is to translate the morse sequence into an ascii character then it would be sensible to have the function return the corresponding character (if any). If you're doing something else then perhaps it would make more sense to return the matching array index.

I GIVE UP !

/*
microphone sketch
 
 SparkFun breakout board for Electret Microphone is connected to analog pin 0
 */
int i=0;
unsigned long currentTime  = 0; //this variable will be overwritten by millis() each iteration of loop
unsigned long pastTime     = 0; //no time has passed yet
unsigned long current  = 0; //this variable will be overwritten by millis() each iteration of loop
unsigned long past     = 0;
char* morseATable[] = { 
  ".-00", "-...", "-.-.", "-..0", ".000", "..-.", "--.0",   // A-G
  "....", "..00", ".---", "_._0", ".-..", "--00", "-.00",    // H-N
  "---0", ".--.", "--.-", ".-.0", "...0", "-000", "..-0",    // O-U
  "...-", ".--0", "-..-", "-.--", "--.."               // V-Z
};
char* temp="0000";
byte c;
int flag2=0;
boolean first=true;
int           currentState = 0; //the default state
int           wait         = 100; //no need to wait, nothing has happened yet
int           flag         =0;
const int ledPin = 13;            //the code will flash the LED in pin 13
const int middleValue = 512;      //the middle of the range of analog values
const int numberOfSamples = 16;  //how many readings will be taken each time

int sample;                       //the value read from microphone each time
long signal;                      //the reading once you have removed DC offset
long averageReading;              //the average of that loop of readings

long runningAverage=0;          //the running average of calculated values
const int averagedOver= 4;     //how quickly new values affect running average
//bigger numbers mean slower

const int threshold=32000;        //at what level the light turns on

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  long sumOfSquares = 0;
  for (int i=0; i<numberOfSamples; i++) { //take many readings and average them
    sample = analogRead(0);               //take a reading
    signal = (sample - middleValue);      //work out its offset from the center
    signal *= signal;                     //square it to make all values positive
    sumOfSquares += signal;               //add to the total
  }
  averageReading = sumOfSquares/numberOfSamples;     //calculate running average
  runningAverage=(((averagedOver-1)*runningAverage)+averageReading)/averagedOver;

  if (runningAverage>threshold){         //is average more than the threshold ?
    digitalWrite(ledPin, HIGH);    //if it is turn on the LED
    past = current; //currentTime at this point is the current time from the previous iteration, this should now be pastTime
    current = millis();
    unsigned long timePassed2 = current - past;
    if(timePassed2 > 200 && first==false){
      for (int b=0; b<26; b++){
        if (strcmp(temp, morseATable[b])==0){
          flag2=b;
          break;
        } 
      }
      flag2=flag2+1;
      switch(flag2){
      case '1':
        c = "A";
        break; 
      case '2':
        c = 'B';
        break; 
      case '3':
        c = 'C';
        break;
      case '4':
        c = 'D';
        break; 
      case '5':
        c = 'E';
        break;
      case '6':
        c = 'F';
        break;
      case '7':
        c = 'G';
        break;
      case '8':
        c = 'H';
        break;
      case '9':
        c = 'I';
        break;
      case '10':
        c = 'J';
        break;
      case '11':
        c ='K';
        break;
      case '12':
        c = 'L';
        break;
      case '13':
        c = 'M';
        break;
      case '14':
        c = 'N';
        break;
      case '15':
        c = 'O';
        break;
      case '16':
        c = 'P';
        break;
      case '17':
        c = 'Q';
        break;
      case '18':
        c = 'R';
        break;
      case '19':
        c = 'S';
        break;
      case '20':
        c = 'T';
        break;
      case '21':
        c = 'U';
        break;
      case '22':
        c = 'V';
        break;
      case '23':
        c = 'W';
        break;
      case '24':
        c = 'X';
        break;
      case '25':
        c = 'Y';
        break;
      case '26':
        c = 'Z';
        break;
      case ' ':
        c = ' ';
        break;
      }
      Serial.print("c :");
      Serial.println(c);
      Serial.print("flag2 :");
      Serial.println(flag2);
      Serial.print("temp :");
      Serial.println(temp);
      i=0;  
    }
    first=false;
  }
  else{
    digitalWrite(ledPin, LOW);           //if it isn't turn the LED off

    pastTime = currentTime; //currentTime at this point is the current time from the previous iteration, this should now be pastTime
    currentTime = millis();    //currentTime is now the current time (again).
    unsigned long timePassed = currentTime - pastTime; //this is roll-over proof, if currentTime is small, and pastTime large, the result rolls over to a small positive value, the time that has passed
    if(timePassed > 30 && timePassed <200) //part1 of the state engine
    {
      if(timePassed >= wait) //part1 of the state engine
      {
        Serial.print("timePassed :");
        Serial.println(timePassed);
        temp[i] = '-';
        i++;

      }
      else {
        Serial.print("timePassed :");
        Serial.println(timePassed);
        temp[i] = '.';
        i++;
      }
    }
    unsigned long timePassed2 = current - past;
    if (timePassed2 >200){
      first=false;
    } 
  }
}

i tryed c as a char and a byte, and i tryed with ' and " in switch ( offcouse with " i get compilation errors ).
flag 2 is correct and points me, to the correct letter.

why i can not get the Serial.print(c); to print me the character of the switch ?

flag2 is defined as an int. When a match is found in the for loop you assign the int loop variable c to flag2. So far so good, but when you use flag2 in the switch you try to match it with a char

      switch(flag2){
      case '1':
        c = "A";
        break; 
      case '2':
        c = 'B';
        break;

The code section above has another problem because for some cases you try to assign a pointer to a constant char ("A") to a byte. Put that mistake right and change the cases to ints rather than chars.

char* temp="0000";

The temp variable is a pointer to a string literal. That is NOT a place that you can write to. Get over it.

Declare the variables that you want to write to correctly.

char temp[5] = { "0000" };
        c = "A";

Pigs might wish for wings, too. You can NOT store a string in a byte. No matter how much you want to.

Pay some serious attention to the difference between single quotes and double quotes, The are NOT interchangeable.