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:
/* 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.