How do I compare each successive char in a char array with given criteria?

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. :confused:

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?

Why don't you use char array intead of String object? It seams to be easiest that way.

toupper may prove useful

AWOL:
toupper may prove useful

What's toupper?

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.

W8TEE

  morseCode(message.toCharArray(char[], 20));

Did that even compile?

The toCharArray() method needs to be told where you have allocated space for it to write to. So, where ARE you allocating that space?

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. :slight_smile:

touppper() (is it three P's, or two?),

The function converts the character used as its argument to uppercase. How many ps are there in upper ?

What is the difference between single quotes and double quotes? I'd never heard of using single quotes for anything.

Single quotes are used around single chars
Double quotes (quotation marks) are used around strings of multiple characters.

How many ps are there in upper ?

Normally, just two. If your fingers stutter 'cause you're old, three.

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.

Yep, I did get that...just thought I'd lighten things up a tad after not checking my spelling!

With a bit of jiggery pokery you can, of course, make touppper() into what looks like a valid function.

#include "spelling.h"

void setup() 
{
  Serial.begin(115200);
  Serial.println((char)touppper('a'));
}

void loop() 
{
}

Thanks! Now I understand toupper() a little better. Thing is, I never realized that it was to-upper. I'd thought it was just one word. LOL

PaulS:

  morseCode(message.toCharArray(char[], 20));

Did that even compile?

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!!!

else if ((toupper(Message[x]) == 'A' ) {

the key is that if Message[X] is already upper case, it is not changed.

Use toupper() to make sure that the message is all upper case, then dispense with all the comparisons to lower case letters. See this http://www.cplusplus.com/reference/cctype/toupper/

jremington:
Use toupper() to make sure that the message is all upper case, then dispense with all the comparisons to lower case letters. See this http://www.cplusplus.com/reference/cctype/toupper/

Thanks! I think I get this now. :sweat_smile:

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

Situation completely resolved! :smiley: :smiley: :smiley: :smiley: :smiley: :smiley: :grin: :smiley: :smiley: :smiley: :grinning: