I'm working on a project in which I take a given message and convert it into morse code, which in turn the Arduino spits out as beeps. First, I tried to convert the given message (a string) into a char array using toCharArray(). I think I did that wrong; most of the documentation lingo went over my head.
However, I still need to know how take take each successive character in the array and compare it to each letter of the alphabet, so that I can convert the proper letter to the proper sequence of beeps. Here's my code:
String message = "ad e dac";Â //this message could be anything
const int optoPin = 2;
void setup() {
 pinMode(optoPin, OUTPUT);
}
void loop() {
 morseCode(message.toCharArray(char[], 20));
Â
 delay(5000);
}
void pressButton(String ditDaw) {Â //this part is irrelevant for now
 if(ditDaw == "dit") {
  digitalWrite(optoPin, HIGH);
  delay(100);
  digitalWrite(optoPin, LOW);
  delay(100);
 }
 else if(ditDaw == "daw") {
  digitalWrite(optoPin, HIGH);
  delay(300);
  digitalWrite(optoPin, LOW);
  delay(100);
 }
}
void morseCode(char Message[20]) {
 for(int x = 0; x < 20; x++) {
 if(message[x] == " ") {
  delay(200);
 }
 else [b]if(Message[x] == "A"[/b]) {
  pressButton(dit);
  pressButton(daw);
 }
 else if(Message[x] == "a") {
  pressButton(dit);
  pressButton(daw);
 }
 else if(Message[x] == "B") {
  pressButton(daw);
  pressButton(dit);
  pressButton(dit);
  pressButton(dit);
 }
 else if(Message[x] == "b") {
  pressButton(daw);
  pressButton(dit);
  pressButton(dit);
  pressButton(dit);
 }
 else if(Message[x] == "C") {
  pressButton(daw);
  pressButton(dit);
  pressButton(daw);
  pressButton(dit);
 }
 else if(Message[x] == "c") {
  pressButton(daw);
  pressButton(dit);
  pressButton(daw);
  pressButton(dit);
 }
 else if(Message[x] == "D") {
  pressButton(daw);
  pressButton(dit);
  pressButton(dit);
 }
 else if(Message[x] == "d") {
  pressButton(daw);
  pressButton(dit);
  pressButton(dit);
 }
}
You get the idea. I also plan to incorporate equalsIgnoreCase later on, so it's not so crowded.
But what can I replace if(Message[x] == "A" with to make each character in the array be compared to the alphabet? And how does toCharArray() work?
touppper() converts an alpha character to an upper case character. This would cut your code in half. Also, you should be comparing individual characters in the array, which calls for single quotes ('A'), not double quotes ("A"). Finally, this is a horribly inefficient way to do this. I Googled "Arduino source code for Morse code" and got 41,000 hits. Surely one of those will be of help to you.
OK, for clarification, I'm not really worried about making my code simpler. I just want to make it work.
PaulS: No, it didn't compile, because I don't know how to use it. And I don't understand technical lingo either. Would you mind showing me how that section should look? Thanks!
econjack: I'll look up touppper() (is it three P's, or two?), it sounds helpful. What is the difference between single quotes and double quotes? I'd never heard of using single quotes for anything. Finally, I'm not so much trying to make this program efficient. My goal is mostly just to come up with a project of my own and execute it by my own creativity. I just need help with making the code run.
Please don't take that wrong; I appreciate all your help. I just either don't know how to use it, or wasn't looking for it.
Normally, just two. If your fingers stutter 'cause you're old, three.
Jack, I never doubted that you knew the right answer and had merely mistyped it in your reply. What I was getting at was the fact that the correct spelling of the function was obvious considering what it does and that the OP could not guess that or consult Mr. Google.
The toCharArray() method needs to be told where you have allocated space for it to write to. So, where ARE you allocating that space?
Could someone show me how this code should be written? I didn't understand all the technical jargon, and I work better with actually seeing how it should be.
Thanks!
I have a project for which many people have advised me to use toupper(), but when I looked up how to use it, all the results that actually had anything to do with what I need, were in very technical jargon that I don't understand.
Here's where I need to use toupper:
void morseCode(char Message[20]) {
 for(int x = 0; x < 20; x++) {
 if(Message[x] == ' ') {
  delay(200);
 }
 else if(Message[x] == 'A') {
  pressButton(dit);
  pressButton(daw);
 }
 else if(Message[x] == 'a') {
  pressButton(dit);
  pressButton(daw);
 }
}
At the moment, I don't care whether it will compile or not (I've got another post about that). I just need someone to show me how I would use toupper in a way that would eliminate the need for two of every letter in the alphabet.
Could someone rewrite this code for me with toupper, and explain what they did?
Thank you so much!!!
ManmanAwesome:
Could someone show me how this code should be written? I didn't understand all the technical jargon, and I work better with actually seeing how it should be.
Thanks!
Still not sure what I should do with this line of code… see the original comment up ^^there.
Thanks