#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <stdio.h>
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
int b1ls = 1;
int b2ls = 1;
int b3ls = 1;
int b4ls = 1;
String code;
String codels;
String pesan;
String pesanls;
String simbol[36][2] =
{
{ ".-","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" },
{ ".----","1" },
{ "..---","2" },
{ "...--","3" },
{ "....-","4" },
{ ".....","5" },
{ "-....","6" },
{ "--...","7" },
{ "---..","8" },
{ "----.","9" },
{ "-----","0" }
};
void setup() {
Serial.begin(115200);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("WELCOME TO");
LCD.setCursor(0, 1);
LCD.print ("MORSE TL");
pinMode(25,INPUT);
pinMode(26,INPUT);
pinMode(27,INPUT);
pinMode(33,INPUT);
delay(2000);
LCD.clear();
}
void loop() {
int b1 = analogRead(25); delay(10);
int b2 = analogRead(26); delay(10);
int b3 = analogRead(27); delay(10);
int b4 = analogRead(33); delay(10);
Serial.println(b1);
Serial.println(b2);
Serial.println(b3);
Serial.println(b4);
if( codels != code ){LCD.clear();}
if(pesanls != pesan){LCD.clear(); code = "";}
LCD.setCursor(0,0);
LCD.print(pesan);
pesanls = pesan;
LCD.setCursor(0,1);
LCD.print(code);
codels = code;
if(!b1ls >=1 && b1 >=1){code += "-";}
if(!b2ls >=1 && b2 >=1){code += ".";}
if(!b3ls >=1 && b3 >=1){
for(int i = 0; i < 36; i++){
if (simbol[i][0] == code)
{pesan += simbol[i][1];}}}
if(!b4ls >=1 && b4 >=1){
if(code.length() >= 1){code.remove(code.length() - 1);}
else if(code.length() == 0){pesan.remove(pesan.length() - 1);}
}
b1ls = b1;
b2ls = b2;
b3ls = b3;
b4ls = b4;
}
/*
#define CARKABUTTONPIN 12
#define TECKABUTTONPIN 8
#define ODDELBUTTONPIN 10
#define DISPLAY_NUMOFCOLUMNS 16 // Working with a 16x2 display
int carkaButtonState = 0;
int carkaButtonLastState = 0;
int teckaButtonState = 0;
int teckaButtonLastState = 0;
int oddelButtonState = 0;
int oddelButtonLastState = 0;
String tonesBuffer;
String text;
String expectedText;
String symbolsAlphabet[][2] =
{
{ ".-", "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" },
{ ".----", "1" },
{ "..---", "2" },
{ "...--", "3" },
{ "....-", "4" },
{ ".....", "5" },
{ "-....", "6" },
{ "--...", "7" },
{ "---..", "8" },
{ "----.", "9" },
{ "-----", "0" }
};
LiquidCrystal_I2C lcd(0x27, DISPLAY_NUMOFCOLUMNS, 2);
char getToneFromButtonStates()
{
// returns when a button is released
// i.e., when the current state is 0, the previous state is 1
if (!carkaButtonState && carkaButtonLastState)
return '-';
if (!teckaButtonState && teckaButtonLastState)
return '.';
if (!oddelButtonState && oddelButtonLastState)
return ' ';
return (char)0;
}
char getSymbolFromBuffer()
{
if (tonesBuffer == "")
return ' '; // makes a space if there are no previous characters
for (int i = 0; i < sizeof symbolsAlphabet / sizeof symbolsAlphabet[0]; i++)
// Goes through all the symbols and compares the buffer with the alphabet
if (tonesBuffer == symbolsAlphabet[i][0])
return symbolsAlphabet[i][1][0]; // if it matches, returns the corresponding symbol
// The buffer does not match any symbol, so nothing is sent
return (char)0;
}
void extractActionFromTonesBuffer()
{
if (tonesBuffer == "......") // 6x dot
text.remove(text.length() - 1, 1); // deletes one character
if (tonesBuffer == "------") // 6x dash
text = ""; // deletes the entire text
}
void setup() {
lcd.init();
lcd.backlight();
lcd.print("Morseovkovnitko");
lcd.setCursor(0, 1);
lcd.print("6x.Smaze1 6x-Vse");
pinMode(CARKABUTTONPIN, INPUT);
pinMode(TECKABUTTONPIN, INPUT);
pinMode(ODDELBUTTONPIN, INPUT);
}
void loop() {
// read button states
carkaButtonState = digitalRead(CARKABUTTONPIN);
teckaButtonState = digitalRead(TECKABUTTONPIN);
oddelButtonState = digitalRead(ODDELBUTTONPIN);
char tone = getToneFromButtonStates(); // checks which button is pressed
if (tone != (char)0)
{
if (tone == ' ') // ends the tone sequence, looking for a symbol
{
char symbol = getSymbolFromBuffer();
if (symbol != (char)0) // A tone sequence has found a symbol, writes it to the text
{
text += symbol;
if (text.length() > DISPLAY_NUMOFCOLUMNS) // If the number of characters exceeds the display size,
// writes the new character in the first place. Others are deleted.
{
text = (String)symbol;
}
}
else // The tone sequence does not correspond to any symbol, but perhaps some action (like deleting a character)
{
extractActionFromTonesBuffer();
}
tonesBuffer = ""; // clears the buffer (dots and dashes)
}
else // dash or dot
{
tonesBuffer += tone;
if (tonesBuffer.length() > DISPLAY_NUMOFCOLUMNS) // if there are more tones than the display size, clears the buffer
{
tonesBuffer = (String)tone;
}
}
// writing to the display is only performed if a button was pressed
lcd.clear(); // clears the display
lcd.print(text); // writes the text
lcd.setCursor(0, 1);
lcd.print(tonesBuffer); // writes the tone sequence
}
// updates the previous state
carkaButtonLastState = carkaButtonState;
teckaButtonLastState = teckaButtonState;
oddelButtonLastState = oddelButtonState;
}
*/