-
Notifications
You must be signed in to change notification settings - Fork 0
/
LUMT_HPC_Test2.ino
97 lines (77 loc) · 2.6 KB
/
LUMT_HPC_Test2.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "HD44780_LCD_PCF8574.h"
// LCD Configuration
#define DISPLAY_DELAY_INIT 50 // mS
HD44780LCD myLCD(2, 16, 0x27, &Wire); // Instantiate an object
// FSR and Morse Code Variables
int fsrAnalogPin = 0;
int fsrReading;
unsigned long pressStartTime = 0, pressEndTime = 0, pressDuration = 0, lastPressEndTime = millis();
bool isPressed = false;
String pressSequence = "";
String displayString = ""; // String to hold the translated Morse code
// Morse Code Mapping
String morseCode[26] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--", "--.."
};
void setup() {
Serial.begin(9600);
pinMode(8, OUTPUT);
delay(DISPLAY_DELAY_INIT);
myLCD.PCF8574_LCDInit(myLCD.LCDCursorTypeOn);
myLCD.PCF8574_LCDClearScreen();
myLCD.PCF8574_LCDBackLightSet(true);
myLCD.PCF8574_LCDGOTO(myLCD.LCDLineNumberOne, 0);
}
String translateMorseCode(String morse) {
for (int i = 0; i < 26; i++) {
if (morseCode[i] == morse) {
return String(char('A' + i));
}
}
return ""; // Return empty string if not found
}
void loop() {
fsrReading = analogRead(fsrAnalogPin);
if (fsrReading >= 100) {
digitalWrite(8, HIGH);
if (!isPressed) {
pressStartTime = millis();
isPressed = true;
}
} else if (isPressed) {
digitalWrite(8, LOW);
pressEndTime = millis();
pressDuration = pressEndTime - pressStartTime;
isPressed = false;
if (pressDuration >= 100 && pressDuration < 500) {
pressSequence += ".";
} else if (pressDuration >= 500 && pressDuration <= 1500) {
pressSequence += "-";
} else if (pressDuration > 3000) {
myLCD.PCF8574_LCDClearScreen();
displayString = ""; // Clear the display string
Serial.println("LCD Cleared, Ready for New Input");
lastPressEndTime = millis(); // Reset the last press end time
return; // Exit the current loop iteration
}
lastPressEndTime = millis();
}
if (millis() - lastPressEndTime > 2000 && pressSequence.length() > 0) {
String letter = translateMorseCode(pressSequence);
if (letter != "") {
displayString += letter; // Append the letter to the display string
myLCD.PCF8574_LCDGOTO(myLCD.LCDLineNumberOne, 0);
myLCD.PCF8574_LCDSendString(displayString.c_str()); // Display the updated string
Serial.print("Morse Code: ");
Serial.print(pressSequence);
Serial.print(" -> Letter: ");
Serial.println(letter);
} else {
Serial.println("Invalid Morse Code");
}
pressSequence = "";
}
delay(100);
}