vendredi 19 février 2021

Morse Code Arduino problems. Cannot type a char without "not a char" showing up on the monitor before converted morse code

Just a quick question for my design class. I am trying to interface with the Serial monitor to translate a char into morse code. To do this, a few if else statements were used in the first part of my loop. Along with the index for the alphabet, there are certain special chars within if/else statements that I would like to use, mainly space between words, and line feed to make a new line. I used the ascii value of 32 for space and '\n' for line feed. If none of these statements are valid, I instructed the Serial monitor to write "not a char"

The first two if statements determine A/a, and put them into the index1 of my array. char patter[index1][index2] Index one has 26 elements, as it is all the letters in the alphabet (entries 0-25). I manipulated ascii values to set both lowercase and uppercase equal to each other.

This is later passed into a for loop, which uses a second index of 7 max */_'s This is coupled with if/else statements to assign the correct value, dit or dah. Dit and Dah are defined in void statements after the loop.

My problem with the code is that the monitor keeps on displaying the "not a char" string before every converted morse code.

Any help is greatly appreciated thank you -Ryan

char pattern[26][7]={
"*_", //A,a
"_***",//B,b
"_*_*",//C,c
"_**", //D,d
"*", //E,e
"**_*",//F,f
"__*", //G,g
"****",//H,h
"**", //I,i
"*___",//J,j
"_*_", //K,k
"*_**",//L,l
"__", //M,m
"_*", //N,n
"___", //O,o
"*__*",//P,p
"__*_",//Q,q
"*_*", //R,r
"***", //S,s
"_", //T,t
"**_", //U,u
"***_",//V,v
"*__", //W,w
"_**_",//X,x
"_*__",//Y,y
"__**" //Z,z
};

const int piezo = 9;
char c;
int index1=0,index2=0;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
delay(100);
Serial.println("Ready!");
pinMode(piezo,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
if (Serial.available())
{
 c=Serial.read();
 index1=-1;
 //between A and Z
 if(c>='A'&&c<='Z')
 {
 index1=c-65;
 }
 if(c>='a'&&c<='z')
 {
 index1=c-97; 
 }
 if(c == 32)
 {
 Serial.print(' ');
 delay(700); 
 }
 if(c == '\n')
 {
 Serial.println(); 
 } 
 else
 {
 Serial.print("not a char");
 }
}


if(index1>=0&&index1<=25)//if it’s a valid character
{
 for(index2=0;index2<=6; index2 ++)
 {
 //dit
   if(pattern[index1][index2]=='*')
   {
   dit();
   }
//dah  
   if(pattern[index1][index2]=='_')
   {
   dah();
   }
  if(pattern[index1][index2]==NULL)
  {
  break;
  }
 delay(300);//space between Dit and Dah
 }
 delay(100);//space between Letters
}
}
void dit()
{
 Serial.print('*');
 delay(40);

}
void dah()
{
 Serial.print('_');
 delay(200);

}

Aucun commentaire:

Enregistrer un commentaire