# include <LiquidCrystal_I2C.h>
// #include <Wire.h>
int timeStart = 0;
int buttonMorse = 9;
int buttonEnd = 12;
int buttonMorseState = 0;
int buttonEndState = 0;
int timeButtonPressed = 0;
int buttonStartEnd = 11;
int buttonStartEndState = 0;
int cont = 0;
int timeButtonNotPressed_Start = 0;
int timeButtonNotPressed = 0;
int buzzer = 10;
int lcdPos = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
// morse character
String character = "";
// list of letters and numbers
String lettersAndNumbers[37] = {" ", "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", "9"};
// respective morse code for each letter and number
String morseCode[37] = {"-..-.", ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."
};
void setup() {
// put your setup code here, to run once:
pinMode(buttonMorse, INPUT_PULLUP);
pinMode(buttonEnd, INPUT_PULLUP);
pinMode(buttonStartEnd, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
lcd.init(); // Inicialize communication with the display
lcd.backlight(); // Turn the display lights on
lcd.clear(); // Clean the display
lcd.setCursor(0, 0);
Serial.begin(9600);
Serial.println("setup");
}
# define PRESSED LOW
void loop() {
if (0) if (digitalRead (buttonStartEnd) == HIGH) {
//if (lcdPos % 32 == 0) {
// Everytime all the 32 positions of the display are being used, clean it up
lcd.clear();
}
buttonStartEndState = digitalRead(buttonStartEnd) == PRESSED;
// Everytime the button thats start and pauses the program is pressed, the program increases 'cont' in one
if (buttonStartEndState) {
cont++;
delay(500);
Serial.println(cont);
Serial.println(" botão press");
}
// If 'cont' is even, the program starts, if it's odd, the program pauses
// When the program is running, the time that the button of the morse code is not pressed is count
timeButtonNotPressed = millis();
// If the button is not pressed for 3 seconds, a space is done
if (0) if (timeButtonNotPressed % 3000 == 0) {
Serial.println(" % 3000 == 0");
lcd.print("");
lcdPos++;
delay(300);
}
buttonMorseState = digitalRead(buttonMorse) == PRESSED;
buttonEndState = digitalRead(buttonEnd) == PRESSED;
// timeStart restart before press the button
timeStart = millis();
// while the button is pressed the time is count
// Serial.println(buttonMorseState);
while (buttonMorseState) {
buttonMorseState = digitalRead(buttonMorse) == PRESSED;
tone(buzzer, 800);
}
noTone(buzzer);
// finalize the time and store in timeButtonPressed
timeButtonPressed = millis() - timeStart;
// if the button is pressed for less than 200 miliseconds and more than 5 miliseconds, so the character is "."
if (timeButtonPressed < 200 and timeButtonPressed > 10) { // why? crude combo debounce and state change?
Serial.println("= . ");
character = character + ".";
// the time that the button is not pressed is turned to 0
timeButtonNotPressed = 0;
}
// if the button is pressed for more than 200 seconds, so the character is "-"
if (timeButtonPressed > 200) {
character = character + "-";
Serial.println(" = - ");
// the time that the button is not pressed is turned to 0
timeButtonNotPressed = 0;
}
// if the button that ends the character is pressed
if (buttonEndState) {
Serial.print(" >");
Serial.print(character);
Serial.println("<");
for (int i = 0; i < 37; i++) {
if (morseCode[i] == character) {
Serial.print(lettersAndNumbers[i]);
lcd.setCursor(lcdPos % 16,lcdPos / 16);
lcd.print(lettersAndNumbers[i]);
lcdPos += 8;
lcdPos &= 31;
}
}
character = "";
delay(500);
}
}