Hello everybody,
I am new to Arduino/C programming and I have a question concerning the possibility of updating the input string.
Here's the thing: I have programmed a morsecode translator.
You type in a text you want to morse and it translates the text to written morsecode and appropriate light signals.
Everything is working pretty fine,
but I want to optimize:
It bothers me that you cannot stop the Arduino from executing the program. Once you've typed in a text,
it translates and blinks, but you cannot make it stop, e.g. in case you've changed your mind about the message you wanted to send.
It would be cool if it would notice that there's new data available at all and then start again, translating the new text.
It would be even cooler if I could type in a certain command like 'Stop!" and make it stop executing and blinking so that you could
start typing a new text aferwards!
So what I've figured out is that I seem to have to make the Arduino update the information it reads
from the Serial port and then make it start the translating again, but with the new gained information.
I've read a bit about interrupts, but I don't know how to make an inerrupt through the Serial port (I tried something like
attachInterrupt(Serial, /random/translating_function, CHANGE)
and it didnt work.), and some people wrote it might not be a good idea to do that at all.
I don't know if that might be helpful since he code itself is working fine and my question is about
adding something, but here it is:
char letter;
int i;
int charposition;
int commaposition;
String string;
String input;
int x;
int length;
String code="";
String morsecode="A. -,B- . . .,C- . - .,D- . .,E.,F. . - .,G- - .,H. . . .,I. .,J. - - -,K- . -,L. - . .,M- -,N- .,O- - -,P. - - .,Q- - . -,R. - .,S. . .,T-,U. . -,V. . . -,W. - -,X- . . -,Y- . - -,Z- - . .,a. -,b- . . .,c- . - .,d- . .,e.,f. . - .,g- - .,h. . . .,i. .,j. - - -,k- . -,l - . .,m- -,n- .,o- - -,p. - - .,q- - . -,r. - .,s. . .,t-,u. . -,v. . . -,w. - -,x- . . -,y- . - -,z- - . .,0- - - - -,1. - - - -,2. . - - -,3. . . - -,4. . . . -,5. . . . .,6- . . . .,7- - . . .,8- - - . .,:- - - . . .,;- . - . - .,?. . - - . .,_. . - - . -,(- . - - .,)- . - - . -,\'. - - - - .,=- . . . -,+. - . - .,/- . . - .,@. - - . - .";
int LED=8;
int p;
String incoming;
String incomingcheck;
void setup()
{
Serial.begin(9600);
pinMode(LED,OUTPUT);
}
void loop()
{
if (Serial.available()>0)
{
chars_to_code();
Serial.print("Morsing: ");
Serial.println(input);
Serial.println(code);
use_code();
Serial.flush();
code="";
}
}
//for 1 letter
void extract_morsecode()
{
charposition=morsecode.indexOf(letter);
commaposition=morsecode.indexOf(",",charposition+1);
string=morsecode.substring(charposition+1,commaposition)+'!';
code+=string;
}
//for >1 letters
void chars_to_code()
{
if (Serial.available()>0)
{
input=Serial.readString();
length=input.length();
for(x=0;x<length;x++)
{
letter=input[x];
if (letter==' ')
{
string="S!";
code+=string;
// Serial.print(string);
}
else if (letter==',')
{
string="C!";
code+=string;
// Serial.print(string);
}
else
{
extract_morsecode();
}
}
}
}
void use_code()
{
for(i=0;i<code.length();i++)
{
if (code[i]=='.')
{
blip();
}
if (code[i]=='-')
{
doo();
}
if (code[i]=='!')
{
exclamation();
}
if (code[i]=='C')
{
c();
}
if(code[i]=='S')
{
s();
}
if(code[i]==' ')
{
pause();
}
}
}
void pause()
{
delay(200);
}
void blip()
{
digitalWrite(LED,HIGH);
delay(200);
digitalWrite(LED,LOW);
}
void doo()
{
digitalWrite(LED,HIGH);
delay(600);
digitalWrite(LED,LOW);
}
void exclamation()
{
delay(600);
}
void s()
{
delay(1400);
}
void c()
{
doo();
pause();
doo();
pause();
blip();
pause();
blip();
pause();
doo();
pause();
doo();
}
Sorry for my English ...
but I would be so glad if somebody could help me since I have been trying to solve this for two days now!
Thank you in Advance
Ariane