-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram
More file actions
56 lines (47 loc) · 1.78 KB
/
program
File metadata and controls
56 lines (47 loc) · 1.78 KB
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
const int ledPin = 13; // Pin for LED
const int buttonPin = 2; // Pin for button
const char* kinhoMorse = "-.- .. -. .... ---"; // Morse code for my first name "KIN HO"
volatile bool ledState = false;
unsigned long previousMillis = 0;
const long dotDuration = 250; // Duration of a dot
const long dashDuration = 750; // Duration of a dash
const long spaceDuration = 1000; // Duration between characters
int morseIndex = 0; // Index for the current character in Morse code
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(buttonPin), toggleButtonState, FALLING);
}
void loop() {
if (ledState) {
blinkMorseCode();
}
}
void blinkMorseCode() {
unsigned long currentMillis = millis();
if (kinhoMorse[morseIndex] == '\0') { // Check if we reached the end of the Morse code
ledState = false; // Reset LED state
digitalWrite(ledPin, LOW); // Turn off the LED
morseIndex = 0; // Reset the Morse code index
return;
}
long interval = (kinhoMorse[morseIndex] == '.') ? dotDuration :
(kinhoMorse[morseIndex] == '-') ? dashDuration : spaceDuration;
if (currentMillis - previousMillis >= interval) {
if (kinhoMorse[morseIndex] != ' ') {
digitalWrite(ledPin, !digitalRead(ledPin)); // Toggle LED state
}
previousMillis = currentMillis; // Update the timing
if (digitalRead(ledPin) == LOW || kinhoMorse[morseIndex] == ' ') {
morseIndex++; // Move to the next Morse code character
}
}
}
void toggleButtonState() {
ledState = !ledState; // Toggle the LED state
if (!ledState) {
digitalWrite(ledPin, LOW); // Turn off the LED immediately if the button is pressed
morseIndex = 0; // Reset the Morse code index
}
previousMillis = millis(); // Reset the timing
}