I am trying to make a timer with a morse code transmitter so that if a word is not put in in 30 seconds, the buzzer goes off. truthfully, i barely know anything on coding but this is what i have so far.
Using millis needs a different kind of thinking.
When there is input, then reset a millis-timer. When that timer reaches 30 seconds (and has not been reset) then a buzzer can be activated.
A millis-timer runs in the main section of the loop(). Starting (or resetting) a millis-timer is done with a certain condition.
void loop()
{
if (Serial.available() > 0) // something received ?
{
get the serial data and send the morse code
reset millis-timer
}
millis-timer for 30 seconds
{
has 30 seconds finished ? then sound buzzer
}
}
You sketch is a good effort, but we know standard ways to write code for certain problems. It is possible to write your sketch in a better way, and that would it even make easier to add things, such as a millis-timer. But you have to start somewhere, so it is okay
void loop()
{
unsigned long lastInput = 0; // New variable.
while (Serial.available())
{
lastInput = millis(); // Hold the last time we got something.
code = Serial.readString();
Serial.print(code);
Serial.print(" = ");
String2Morse();
Serial.println("");
Buttonlatch ();
}
if (millis() - lastInput > 30000) // Has it been 30 seconds since we got something?
{
Serial.println("Nothing received for 30 seconds"); // Turn on your buzzer here.
}
}
As per previous comments you should investigate using arrays.. your code could be massively simpler. Something like this to hold the morse codes...
char alphabet[36] [5] = {".-", // A
"-...", // B
"-.-.", // C
"-.."}; // D, etc...
This is my test to see if your morse works, so I changed your sketch until it worked with the timeout. I hope that you do not just copy it, but check my notes about the changes that I made.
// For: https://forum.arduino.cc/t/morse-code-circuit-with-timer/1071378
//
// 30 December 2022
//
// Changes by Koepel:
// - Formatted the text.
// - Added a timeout.
// - Added a piezo buzzer, it needs the tone() function.
// - Added the word "Pin" to the variable names for pins.
// - Removed delay(1000); from loop().
// - Moved the important functions to the top.
// - Used 'tone()' for the buzzer.
// - Added a LED next to the buzzer at pin 4, just for fun.
// - Added functions StartAlarm and StopAlarm.
// - Added 'bool' variable to indicate if alarm is on
// - Renamed 'starTime' to 'startTime'.
// - Added code to ignore LineFeed and CarriageReturn
// - Reduced the timeout for Serial.readString() to 100ms
// - Still thinking about the code.trim(),
// it also removes spaces at the beginning.
// - Added UL to the number of 30000 for safety.
//
const int ledPin = 13;
const int buzPin = 5;
const int buttonPin = 10;
const int alarmLedPin = 4;
String code = "";
int len = 0;
char ch;
char new_char;
int unit_delay = 250;
bool pinState = true;
bool btnState = true;
bool prevBtnState = true;
bool alarm = false;
unsigned long interval = 30000UL; // UL means Unsigned Long
unsigned long startTime = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
Serial.setTimeout(100); // 100 ms timeout for Serial.readString()
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzPin, OUTPUT);
pinMode(alarmLedPin, OUTPUT);
Serial.println("I am ready...");
startTime = millis(); // start the timer
}
void loop() {
// put your main code here, to run repeatedly:
while (Serial.available() > 0)
{
// Something has been entered, stop the alarm.
if(alarm) // check if alarm is on
{
StopAlarm();
alarm = false;
}
code = Serial.readString();
// code.trim(); // could be useful
Serial.print(code);
Serial.print(" = ");
String2Morse();
Serial.println("");
startTime = millis(); // reset the timer
}
// The millis-timer runs on its own in the main section of the loop()
if(millis() - startTime >= interval)
{
if(!alarm) // check if the alarm is off
{
StartAlarm();
alarm = true;
}
}
}
void StartAlarm () {
// Only sound the buzzer, when the switch is turned on (to the right)
if ( digitalRead(buttonPin) == HIGH)
tone(buzPin, 230);
digitalWrite(alarmLedPin, HIGH);
Serial.println("TYPE SOMETHING BRO");
}
void StopAlarm () {
// Stop the buzzer, regardless if it was on or off
noTone(buzPin);
digitalWrite(alarmLedPin, LOW);
}
void String2Morse()
{
len = code.length();
for (int i = 0; i < len; i++)
{
ch = code.charAt(i);
morse();
}
}
void dot()
{
Serial.print(".");
digitalWrite(ledPin, HIGH);
tone(buzPin, 1000);
delay(unit_delay);
digitalWrite(ledPin, LOW);
noTone(buzPin);
delay(unit_delay);
}
void dash()
{
Serial.print("-");
digitalWrite(ledPin, HIGH);
tone(buzPin, 1000);
delay(unit_delay * 3);
digitalWrite(ledPin, LOW);
noTone(buzPin);
delay(unit_delay);
}
void morse()
{
if (ch == 'A' || ch == 'a')
{
A();
Serial.print(" ");
}
else if (ch == 'B' || ch == 'b')
{
B();
Serial.print(" ");
}
else if (ch == 'C' || ch == 'c')
{
C();
Serial.print(" ");
}
else if (ch == 'D' || ch == 'd')
{
D();
Serial.print(" ");
}
else if (ch == 'E' || ch == 'e')
{
E();
Serial.print(" ");
}
else if (ch == 'f' || ch == 'f')
{
f();
Serial.print(" ");
}
else if (ch == 'G' || ch == 'g')
{
G();
Serial.print(" ");
}
else if (ch == 'H' || ch == 'h')
{
H();
Serial.print(" ");
}
else if (ch == 'I' || ch == 'i')
{
I();
Serial.print(" ");
}
else if (ch == 'J' || ch == 'j')
{
J();
Serial.print(" ");
}
else if (ch == 'K' || ch == 'k')
{
K();
Serial.print(" ");
}
else if (ch == 'L' || ch == 'l')
{
L();
Serial.print(" ");
}
else if (ch == 'M' || ch == 'm')
{
M();
Serial.print(" ");
}
else if (ch == 'N' || ch == 'n')
{
N();
Serial.print(" ");
}
else if (ch == 'O' || ch == 'o')
{
O();
Serial.print(" ");
}
else if (ch == 'P' || ch == 'p')
{
P();
Serial.print(" ");
}
else if (ch == 'Q' || ch == 'q')
{
Q();
Serial.print(" ");
}
else if (ch == 'R' || ch == 'r')
{
R();
Serial.print(" ");
}
else if (ch == 'S' || ch == 's')
{
S();
Serial.print(" ");
}
else if (ch == 'T' || ch == 't')
{
T();
Serial.print(" ");
}
else if (ch == 'U' || ch == 'u')
{
U();
Serial.print(" ");
}
else if (ch == 'V' || ch == 'v')
{
V();
Serial.print(" ");
}
else if (ch == 'W' || ch == 'w')
{
W();
Serial.print(" ");
}
else if (ch == 'X' || ch == 'x')
{
X();
Serial.print(" ");
}
else if (ch == 'Y' || ch == 'y')
{
Y();
Serial.print(" ");
}
else if (ch == 'Z' || ch == 'z')
{
Z();
Serial.print(" ");
}
else if (ch == '0')
{
zero();
Serial.print(" ");
}
else if (ch == '1')
{
one();
Serial.print(" ");
}
else if (ch == '2')
{
two();
Serial.print(" ");
}
else if (ch == '3')
{
three();
Serial.print(" ");
}
else if (ch == '4')
{
four();
Serial.print(" ");
}
else if (ch == '5')
{
five();
Serial.print(" ");
}
else if (ch == '6')
{
six();
Serial.print(" ");
}
else if (ch == '7')
{
seven();
Serial.print(" ");
}
else if (ch == '8')
{
eight();
Serial.print(" ");
}
else if (ch == '9')
{
nine();
Serial.print(" ");
}
else if (ch == ' ')
{
delay(unit_delay * 7);
Serial.print("/ ");
}
else if(ch == '\n' or ch == '\r')
{
// ignore the LineFeed and CarriageReturn
// do nothing
}
else
Serial.println("Unknown symbol!");
}
void A()
{
dot();
delay(unit_delay);
dash();
delay(unit_delay);
}
void B()
{
dash();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
}
void C()
{
dash();
delay(unit_delay);
dot();
delay(unit_delay);
dash();
delay(unit_delay);
dot();
delay(unit_delay);
}
void D()
{
dash();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
}
void E()
{
dot();
delay(unit_delay);
}
void f()
{
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dash();
delay(unit_delay);
dot();
delay(unit_delay);
}
void G()
{
dash();
delay(unit_delay);
dash();
delay(unit_delay);
dot();
delay(unit_delay);
}
void H()
{
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
}
void I()
{
dot();
delay(unit_delay);
dot();
delay(unit_delay);
}
void J()
{
dot();
delay(unit_delay);
dash();
delay(unit_delay);
dash();
delay(unit_delay);
dash();
delay(unit_delay);
}
void K()
{
dash();
delay(unit_delay);
dot();
delay(unit_delay);
dash();
delay(unit_delay);
}
void L()
{
dot();
delay(unit_delay);
dash();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
}
void M()
{
dash();
delay(unit_delay);
dash();
delay(unit_delay);
}
void N()
{
dash();
delay(unit_delay);
dot();
delay(unit_delay);
}
void O()
{
dash();
delay(unit_delay);
dash();
delay(unit_delay);
dash();
delay(unit_delay);
}
void P()
{
dot();
delay(unit_delay);
dash();
delay(unit_delay);
dash();
delay(unit_delay);
dot();
}
void Q()
{
dash();
delay(unit_delay);
dash();
delay(unit_delay);
dot();
delay(unit_delay);
dash();
delay(unit_delay);
}
void R()
{
dot();
delay(unit_delay);
dash();
delay(unit_delay);
dot();
delay(unit_delay);
}
void S()
{
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
}
void T()
{
dash();
delay(unit_delay);
}
void U()
{
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dash();
delay(unit_delay);
}
void V()
{
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dash();
delay(unit_delay);
}
void W()
{
dot();
delay(unit_delay);
dash();
delay(unit_delay);
dash();
delay(unit_delay);
}
void X()
{
dash();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dash();
delay(unit_delay);
}
void Y()
{
dash();
delay(unit_delay);
dot();
delay(unit_delay);
dash();
delay(unit_delay);
dash();
delay(unit_delay);
}
void Z()
{
dash();
delay(unit_delay);
dash();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
}
void one()
{
dot();
delay(unit_delay);
dash();
delay(unit_delay);
dash();
delay(unit_delay);
dash();
delay(unit_delay);
dash();
delay(unit_delay);
}
void two()
{
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dash();
delay(unit_delay);
dash();
delay(unit_delay);
dash();
delay(unit_delay);
}
void three()
{
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dash();
delay(unit_delay);
dash();
delay(unit_delay);
}
void four()
{
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dash();
delay(unit_delay);
}
void five()
{
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
}
void six()
{
dash();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
}
void seven()
{
dash();
delay(unit_delay);
dash();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
}
void eight()
{
dash();
delay(unit_delay);
dash();
delay(unit_delay);
dash();
delay(unit_delay);
dot();
delay(unit_delay);
dot();
delay(unit_delay);
}
void nine()
{
dash();
delay(unit_delay);
dash();
delay(unit_delay);
dash();
delay(unit_delay);
dash();
delay(unit_delay);
dot();
delay(unit_delay);
}
void zero()
{
dash();
delay(unit_delay);
dash();
delay(unit_delay);
dash();
delay(unit_delay);
dash();
delay(unit_delay);
dash();
delay(unit_delay);
}
That's ambiguous and while common sense might well serve to figure out what you mean, since you are having trouble with the code, or were, it would be better to say when the alarm is sounding, and when it is not. Sounding. Or ringing or beeping or clanging or whatever.
When you type text "abcde" and then press the 'Enter' key, the Serial Monitor might add one or two extra characters. It has always been like that for serial communication.
A "Line Feed" is '\n' in the C-language.
A "Carriage Return" is a '\r' in the C-language.