Code
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int buttonPin = 3;
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
// Not all the characters will fit on the display. This is normal.
// Library will draw what it can and the rest will be clipped.
display.write("ready");
display.display();
}
int button_state = 0,
hold_length = 0,
unhold_length = 0,
press = 100;
String current_string = "nat";
String total_string = "";
void loop(){
gatherButtonData();
convertToMorse();
}
void gatherButtonData(){
int button_state = digitalRead(buttonPin);
if(button_state == 1){
unhold_length = 0;
hold_length+=1;
delay(1);
}
else if(hold_length > 1){
press = hold_length;
hold_length = 0;
}
else if (unhold_length > 1000 && unhold_length < 1002){
total_string += morseToLetter(current_string);
current_string = "";
unhold_length += 1;
writetoScreen(total_string);
delay(1);
}
else if (unhold_length < 5000){
unhold_length += 1;
delay(1);
}
else{
total_string = "";
display.clearDisplay();
display.display();
}
}
void writetoScreen(String string){
int str_len = string.length() + 1;
char char_array[str_len];
string.toCharArray(char_array, str_len);
display.clearDisplay();
display.display();
display.setCursor(0, 0);
display.write(char_array);
display.display();
}
void convertToMorse(){
if(press > 0){
if(press < 200){
current_string += ".";
}
else{
current_string += "-";
}
writetoScreen(total_string + current_string);
press = 0;
}
}
char morseToLetter(String string){
if (string == ".") {
return 'e';
}
else if (string == "-") {
return 't';
}
else if (string == "..") {
return 'i';
}
else if (string == "--") {
return 'm';
}
else if (string == ".-") {
return 'a';
}
else if (string == "-.") {
return 'n';
}
else if (string == "...") {
return 's';
}
else if (string == "---") {
return 'o';
}
else if (string == "..-") {
return 'u';
}
else if (string == ".--") {
return 'w';
}
else if (string == "-..") {
return 'd';
}
else if (string == "-.-") {
return 'k';
}
else if (string == ".-.") {
return 'r';
}
else if (string == "--.") {
return 'g';
}
else if (string == "....") {
return 'h';
}
else if (string == "...-") {
return 'v';
}
else if (string == "..-.") {
return 'f';
}
else if (string == ".-..") {
return 'l';
}
else if (string == ".--.") {
return 'p';
}
else if (string == ".---") {
return 'j';
}
else if (string == "-...") {
return 'b';
}
else if (string == "-..-") {
return 'x';
}
else if (string == "-.-.") {
return 'c';
}
else if (string == "-.--") {
return 'y';
}
else if (string == "--..") {
return 'z';
}
else if (string == "--.-") {
return 'q';
}
return '?';
}