Pages: [1] 2   Go Down
Author Topic: error: 'setChar' was not declared in this scope  (Read 1101 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hy,

I am trying to write a program, that reads a short text from the serial connection, and sends it out as a morse code.
I wanted to test it out with the few characters I've added, but I get a compiler error: "Serial_to_morse:39: error: 'setChar' was not declared in this scope"

Here is the code:
Code:
/* This program reads a text up to 63 charakters through the Serial connection,
 puts it into an array of bytes
 and sends out the morse code. */

byte spk = 11;  //speaker pin

byte text[63];  //63 is max, because Serial.available() can't be bigger, Arduino can't buffer more than that.
boolean newText = false;
byte part[9];  //the parts of the morse code

byte wpm = 10; //words per minute, set speed
int Hz = 800; //morse code frequency (in herz)

void setup() {
  pinMode(spk, OUTPUT);
  Serial.begin(9600);
  Serial.println("Power On");
}

void loop()
{
  if(Serial.available()){  //read the text from serial
    delay(5);  //let arduino buffer some letters
    for(int i=0; i<63; i++){
      text[i] = Serial.read();
      delay(5);  //needed for stability
    }
    for(int i=0; i<63; i++){  //if there are no setChars to read, Serial.read reads 255
      if(text[i] == 255) text[i] = 0;
    }
    newText = true;
  }


  if(newText){
    for(int i=0; i<63; i++){
     
      switch(text[i]){  //convert ASCII numbers to morse code
      case 32:  //space
        setChar(4);
        break;
      case 33:  //!
        setChar(1,0,1,0,1,1);
        break;
      case 34: //"
        setChar(0,1,0,0,1,0);
        break;
      case 36:  //$
        setChar(0,0,0,1,0,0,1);
        break;
      case 38:  //&
        setChar(0,1,0,0,0);
        break;
      case 39:  //'
        setChar(0,1,1,1,1,0);
        break;
      case 40:  //(
        setChar(1,0,1,1,0);
        break;
      case 41:  //)
        setChar(1,0,1,1,0,1);
        break;
      case 43:  //+
        setChar(0,1,0,1,0);
        break;

//other charakters including letters and numbers to be added


      default:  //do nothing
        break;
      }
      charakter();  //output morse code
      text[i] = 0; //reset text
    }
  }
}

//couldn't find any other solutions to put the parts of the morse code in an array, default is 9
void setChar(byte p0=9, byte p1=9, byte p2=9, byte p3=9, byte p4=9, byte p5=9, byte p6=9, byte p7=9, byte p8=9){
  part[0] = p0;
  part[1] = p1;
  part[2] = p2;
  part[3] = p3;
  part[4] = p4;
  part[5] = p5;
  part[6] = p6;
  part[7] = p7;
  part[8] = p8;
}

void charakter(){
  for(byte i=0; i<9; i++){
    if(part[i] == 9) break;
    morse(part[i]);
  }
}

void morse(byte element){
  if(element == 0){  //dot
    tone(spk, Hz, 1000 / wpm);
    delay(1000 / wpm);
  }
  else if(element == 1){  //dash
    tone(spk, Hz, 3000 / wpm);
    delay(3000 / wpm);
  }
  else if(element == 2){  //inter-element gap
    delay(1000 / wpm);
  }
  else if(element == 3){  //short gap (between letters)
    delay(3000 / wpm);
  }
  else if(element == 4){  //medium gap (between words)
    delay(7000 / wpm);
  }
}

I appretiate any help.
Logged

Offline Offline
Edison Member
*
Karma: 18
Posts: 1170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can't normally have functions with variable numbers of arguments. You must specify all the args. You might make the last one -1 to signal to SetChar() that you don't need to go any farther.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if(Serial.available()){  //read the text from serial
    delay(5);  //let arduino buffer some letters
    for(int i=0; i<63; i++){
      text[i] = Serial.read();
      delay(5);  //needed for stability
    }
If there is one character available, read all 64 of them. Those delay()s are band-aids for not understanding how serial data is transmitted and received, and for the lack of the sender properly packaging the data.

Make the sender properly terminate the packet, and make the receiver receive whatever data exists, without any delay()s. Only act upon the data when the end of packet marker arrives.
Logged

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

You can't normally have functions with variable numbers of arguments. You must specify all the args. You might make the last one -1 to signal to SetChar() that you don't need to go any farther.

Well, "Serial.write" and "tone" also have some optional arguments. I don't understand, why I couldn't use them?
« Last Edit: November 30, 2012, 02:09:22 pm by Hasi321 » Logged

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

If there is one character available, read all 64 of them. Those delay()s are band-aids for not understanding how serial data is transmitted and received, and for the lack of the sender properly packaging the data.

Make the sender properly terminate the packet, and make the receiver receive whatever data exists, without any delay()s. Only act upon the data when the end of packet marker arrives.

That isn't my problem...
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
That isn't my problem...
Yet.
Logged

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

Yet.

I've tested it out before and it works just fine. I couldn't get it to work properly in an another way...

Code:
/* This program reads a text up to 63 charakters through the Serial connection,
puts it into an array of bytes
and prints it back out to the computer. */

byte text[63];  //63 is max, because Serial.available() can't be bigger, Arduino can't buffer more than that.
boolean newText = false;

void setup() {
  Serial.begin(9600);
  Serial.println("Power On");
}

void loop()
{
  if(Serial.available()){
    delay(5);  //let arduino buffer some letters
    for(int i=0; i<63; i++){
      text[i] = Serial.read();
      delay(5);  //needed for stability
    }
    for(int i=0; i<63; i++){  //if there are no charakters to read, Serial.read reads 255
      if(text[i] == 255) text[i] = 0;
    }
    newText = true;
  }

  if(newText){
    for(int i=0; i<63; i++){
      Serial.write(text[i]);
      text[i] = 0;  //reset
    }
    Serial.println("");
    newText = false;
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I've tested it out before and it works just fine. I couldn't get it to work properly in an another way...
What other ways have you tried? Like, maybe, using a while(Serial.availale()) loop?

Why did you decide on 5 milliseconds as a big enough waste of time? Why not 50000?
Logged

Offline Offline
Edison Member
*
Karma: 18
Posts: 1170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Because those are classes, not functions. Classes can have multiple methods with varying arguments.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 236
Posts: 24266
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Because those are classes, not functions
?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

What other ways have you tried? Like, maybe, using a while(Serial.availale()) loop?

Why did you decide on 5 milliseconds as a big enough waste of time? Why not 50000?

Yes, I have also tried while(Serial.availale()), but it mostly split my text and made only arrays with fragments of the text instead being together in one array.

Why did I decide 5 milliseconds? It's just testing. It still didn't work with 1, so I tried 5 and now it works. I might be able to reduce it, but it doesn't matters.
Logged

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

Because those are classes, not functions. Classes can have multiple methods with varying arguments.

Can I also make a class (whatever it is)?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 236
Posts: 24266
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can have a function have a fixed number of parameters, with unspecified ones being defaulted.
This probably won't work for you in this application.
You can Google va_args which probably will do what you want.

Neither approach has anything to do with classes.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.gammon.com.au/serial

Quote
Well, "Serial.write" and "tone" also have some optional arguments. I don't understand, why I couldn't use them?

...

Quote
Because those are classes, not functions. Classes can have multiple methods with varying arguments.

In C++ any function can have optional arguments. Example:

Code:
int foo (int a, int b, int c = 0, bool d = false)
  {
  int result = a + b;
  if (d)
    result -= c;
  else
    result += c;
  return result;
  }

void setup ()
  {
  foo (1, 2);
  foo (4, 5, 6);
  foo (8, 9, 42, true); 
  }  // end of setup
 
void loop () { }
 
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

... I get a compiler error: "Serial_to_morse:39: error: 'setChar' was not declared in this scope"

It looks like the function prototype-generator used by the IDE didn't recognize that prototype with the default arguments. Move setChar before loop, and it compiles OK.
Logged

Pages: [1] 2   Go Up
Jump to: