Hello there. I'm trying to learn this program. What this program does is that I send an input through the serial monitor and it will display in the LCD together with its corresponding Morse code. Also the LED will blink and the speaker will activate depending on the type of Morse code.
My Question is that.. I dont know what the for function is for. Any kind of help would be greatly appreciated.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, NEGATIVE);
#define I2C_ADDR 0x27 // Define the address of the serial communication display
#define LED_OFF 0
#define LED_ON 1
boolean okay = true; // A boolean holds one of two values, true or false.
int ledPin = 13; //The pin, your LED is connected to.
int speakerPin = 9; //The pin, your buzzer or speaker is connected to.
int pitch1 = 140; // i'll get to this
int pitch2 = 140; //i'll get to this
int dot = 50; //i'll get to this
int dash = 3 * dot; //i'll get to this
String morseATable[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", // A-G
"....", "..", ".---", "_._", ".-..", "--", "-.", // H-N
"---", ".--.", "--.-", ".-.", "...", "-", "..-", // O-U
"...-", ".--", "-..-", "-.--", "--.." // V-Z
};
String morseNTable[] = { "-----", ".----", "..---", "...--", "....-", // 0-4
".....", "-....", "--...", "---..", "----."
}; // 5-9
void setup()
{
lcd.begin(16, 2); //Initialize the display
lcd.setBacklight(LED_OFF);
pinMode(ledPin, OUTPUT);
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
if (okay)
Serial.println("\nSTART");
lcd.print("START");
delay(2000);
lcd.clear();
}
void loop ()
{
if (true) //!true hindi nageeexicute yung program
{
convert2Morse(Serial.readString());
}
}
void convert2Morse(String message)
{
for (int pos = 0; pos < message.length(); pos++) //What does this part do?
{
if (okay)
{
Serial.print(message.charAt(pos)); message.charAt(pos) kinukuha nya yung sa string
Serial.print(" ");
lcd.print(message.charAt(pos));
lcd.print(" ");
}
if ((message.charAt(pos) >= 97) && (message.charAt(pos) <= 122)) //a-z
morseLED(morseATable[message.charAt(pos) - 97]);
if ((message.charAt(pos) >= 65) && (message.charAt(pos) <= 90)) //A-Z
morseLED(morseATable[message.charAt(pos) - 65]);
if ((message.charAt(pos) >= 48) && (message.charAt(pos) <= 57)) // 0-9
morseLED(morseNTable[message[pos] - 48]);
if (message.charAt(pos) == 32)
morseLED(" ");
if ((message.charAt(pos) >= 33) && (message.charAt(pos) <= 47)) // symbols
{
lcd.print("<-- THIS IS AN" );
lcd.setCursor(2, 1);
lcd.print(" INVALID INPUT ");
Serial.print("INVALID INPUT");
}
if ((message.charAt(pos) >= 58) && (message.charAt(pos) <= 64)) // symbols
{
lcd.print("<-- THIS IS AN" );
lcd.setCursor(2, 1);
lcd.print(" INVALID INPUT ");
Serial.print("INVALID INPUT");
}
if ((message.charAt(pos) >= 91) && (message.charAt(pos) <= 96)) // symbols
{
lcd.print("<-- THIS IS AN" );
lcd.setCursor(2, 1);
lcd.print(" INVALID INPUT ");
Serial.print("INVALID INPUT");
}
if ((message.charAt(pos) >= 123) && (message.charAt(pos) <= 126))
{
lcd.print("<-- THIS IS AN" );
lcd.setCursor(2, 1);
lcd.print(" INVALID INPUT ");
Serial.print("INVALID INPUT");
}
if (okay)
Serial.println();
delay(2000);
lcd.clear();
}
}
void morseLED(String sequence)
{
for (int pos = 0; pos < sequence.length(); pos++)
flashLED(sequence.charAt(pos));
delay(2 * dot);
}
void flashLED(char dotOrDash)
{
if (okay)
Serial.print(dotOrDash);
lcd.print(dotOrDash);
if (dotOrDash == 46)
{
digitalWrite(ledPin, HIGH);
tone (speakerPin, pitch1);
delay(dot);
digitalWrite(ledPin, LOW);
noTone(speakerPin);
delay(dot);
}
else if (dotOrDash == 45)
{
digitalWrite(ledPin, HIGH);
tone (speakerPin, pitch2);
delay(dash);
digitalWrite(ledPin, LOW);
noTone(speakerPin);
delay(dot);
}
else if (dotOrDash == 32)
delay(dot);
}