I want my program to turn on output 9 when the setup triggers, but no matter what I do I cant seem to get it to work. I feel so dumb, what am I doing wrong here?
int tonePin = 2;
int toneFreq = 2500;
int ledPin = 13;
int buttonPin = 8;
int outputPin = 9; // <-- Added for activating on ORDERS
int debounceDelay = 30;
int dotLength = 440; // dotLength = basic unit of speed in milliseconds
int dashLength = dotLength * 3;
int letterSpace = dotLength * 3;
int wordSpace = dotLength * 3.5;
float wpm = 1200. / dotLength;
int t1, t2, onTime, gap;
bool newLetter, newWord, letterFound, keyboardText;
int lineLength = 0;
int maxLineLength = 20;
char* letters[] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", // A-I
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." // S-Z
};
char* numbers[] = {
"-----", ".----", "..---", "...--", "....-", //0-4
".....", "-....", "--...", "---..", "----." //5-9
};
String dashSeq = "";
char keyLetter, ch;
int i, index;
String morseInput = ""; // Track the incoming Morse code sequence
unsigned long lastInputTime = 0; // Track last input time for timeout
const unsigned long timeoutDuration = 20000; // 20 seconds timeout for new word
void setup() {
delay(500);
pinMode(ledPin, OUTPUT);
pinMode(tonePin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(outputPin, OUTPUT); // <-- Configure pin 6 as output
Serial.begin(9600);
Serial.println();
Serial.println("-------------------------------");
Serial.println("Morse Code decoder/encoder");
Serial.print("Speed=");
Serial.print(wpm);
Serial.print("wpm, ");
Serial.print("dot=");
Serial.print(dotLength);
Serial.println("ms");
// Test the LED and tone
tone(tonePin, toneFreq);
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW);
noTone(tonePin);
delay(600);
// Flash the word "HOWDY" in Morse code
Serial.print("H .... ");
flashSequence("...."); // H
delay(wordSpace);
Serial.print("O --- ");
flashSequence("---"); // O
delay(wordSpace);
Serial.print("W .-- ");
flashSequence(".--"); // W
delay(wordSpace);
Serial.print("D -.. ");
flashSequence("-.."); // D
delay(wordSpace);
Serial.print("Y -.-- ");
flashSequence("-.--"); // Y
delay(wordSpace);
Serial.println();
Serial.println("-------------------------------");
Serial.println("Click field in Serial Monitor,");
Serial.println("type text and press Enter, or");
Serial.println("Key in Morse Code to decode:");
Serial.println("-------------------------------");
newLetter = false;
newWord = false;
keyboardText = false;
}
void loop() {
if (millis() - lastInputTime > timeoutDuration) {
morseInput = "";
Serial.println("\nTimeout! Waiting for new Morse code input...");
}
if (digitalRead(buttonPin) == LOW) {
newLetter = true;
newWord = true;
t1 = millis();
digitalWrite(ledPin, HIGH);
tone(tonePin, toneFreq);
delay(debounceDelay);
while (digitalRead(buttonPin) == LOW) {
delay(debounceDelay);
}
delay(debounceDelay);
t2 = millis();
onTime = t2 - t1;
digitalWrite(ledPin, LOW);
noTone(tonePin);
if (onTime <= dotLength * 1.5) {
dashSeq = dashSeq + ".";
} else {
dashSeq = dashSeq + "-";
}
lastInputTime = millis();
}
gap = millis() - t2;
if (newLetter == true && gap >= letterSpace) {
letterFound = false;
keyLetter = 63;
for (i = 0; i <= 25; i++) {
if (dashSeq == letters[i]) {
keyLetter = i + 65;
letterFound = true;
break;
}
}
if (!letterFound) {
for (i = 0; i <= 9; i++) {
if (dashSeq == numbers[i]) {
keyLetter = i + 48;
letterFound = true;
break;
}
}
}
Serial.print(keyLetter);
if (!letterFound) {
tone(tonePin, 100, 500);
}
morseInput += dashSeq + " ";
dashSeq = "";
if (morseInput == "--- .-. -.. . .-. ... ") {
Serial.println("\nDetected 'ORDERS' in Morse code. Activating setup...");
triggerSetup();
}
newLetter = false;
lineLength = lineLength + 1;
}
if (lineLength >= maxLineLength) {
Serial.println();
lineLength = 0;
}
}
void triggerSetup() {
digitalWrite(outputPin, HIGH); // <-- Turn on pin 6 when "ORDERS" is received
// Flash "INITIATE BOMB SEQUENCE" in Morse
Serial.print("I .. "); flashSequence(".."); delay(wordSpace);
Serial.print("N -. "); flashSequence("-."); delay(wordSpace);
Serial.print("I .. "); flashSequence(".."); delay(wordSpace);
Serial.print("T - "); flashSequence("-"); delay(wordSpace);
Serial.print("I .. "); flashSequence(".."); delay(wordSpace);
Serial.print("A .- "); flashSequence(".-"); delay(wordSpace);
Serial.print("T - "); flashSequence("-"); delay(wordSpace);
Serial.print("E . "); flashSequence("."); delay(wordSpace);
Serial.print("B -... "); flashSequence("-..."); delay(wordSpace);
Serial.print("O --- "); flashSequence("---"); delay(wordSpace);
Serial.print("M -- "); flashSequence("--"); delay(wordSpace);
Serial.print("B -... "); flashSequence("-..."); delay(wordSpace);
Serial.print("S ... "); flashSequence("..."); delay(wordSpace);
Serial.print("E . "); flashSequence("."); delay(wordSpace);
Serial.print("Q --.- "); flashSequence("--.-"); delay(wordSpace);
Serial.print("U ..- "); flashSequence("..-"); delay(wordSpace);
Serial.print("E . "); flashSequence("."); delay(wordSpace);
Serial.print("N -. "); flashSequence("-."); delay(wordSpace);
Serial.print("C -.-. "); flashSequence("-.-."); delay(wordSpace);
Serial.print("E . "); flashSequence("."); delay(wordSpace);
Serial.println();
Serial.println("-------------------------------");
Serial.println("Click field in Serial Monitor,");
Serial.println("type text and press Enter, or");
Serial.println("Key in Morse Code to decode:");
Serial.println("-------------------------------");
}
void flashSequence(char* sequence) {
int i = 0;
while (sequence[i] == '.' || sequence[i] == '-') {
flashDotOrDash(sequence[i]);
i++;
}
}
void flashDotOrDash(char dotOrDash) {
digitalWrite(ledPin, HIGH);
tone(tonePin, toneFreq);
if (dotOrDash == '.') {
delay(dotLength);
} else {
delay(dashLength);
}
digitalWrite(ledPin, LOW);
noTone(tonePin);
delay(dotLength);
}