Seemed simple enough following the directions here.
Below is the code I wrote to flash an LED in Morse Code to signal SOS. The FSM is in the void flash() function. The issue I'm having when I run the code is that the LED blinks HIGH for 700ms and LOW for 700ms unless I only call the function once. The intent of the code is for the LED to blink 3 short times using flash(dot), 3 long times using flash(dash), and 3 short times again using flash(dot).
The program works fine if I remove the FSM and only use delays in the void & flash functions. Once I get the void flash() issue solved, then I plan to convert the void loop into a FSM.
PS: This is not for a homework assignment. I'm merely trying to get away from using delay() and start using FSMs in the future.
Your code doesn't implement a fsm. You are still stepping through loop() one Morse character at a time. Loop() should go round freely many thousands of times per second.
You probably need an array to store the Morse characters and your fsm will step through the array slowly with timing given by millis().
Try the tutorial again.
There's tutorials on here for using millis for timing and doing several things at once. Study those.
I've put this together for you as an example, it kind of does what you are trying to achieve but I've left plenty of room for improvement. Note how loop() is free to loop and how morse() just checks if the right amount of time has elapsed and only does something if there is something to be done. I'm not even sure if this properly counts as a FSM it's so simple, I suppose it does. There are 2 variables that keep track of the states, these are index and toneOn.
PerryBebbington:
I've put this together for you as an example, it kind of does what you are trying to achieve but I've left plenty of room for improvement. Note how loop() is free to loop and how morse() just checks if the right amount of time has elapsed and only does something if there is something to be done. I'm not even sure if this properly counts as a FSM it's so simple, I suppose it does. There are 2 variables that keep track of the states, these are index and toneOn.
Thanks. I'll start working through it. It might take a minute or two to digest.
So I'm getting hung up on the remainder operator (%). I've looked at what Reference says about % and saw it even used an array as an example. Is there an advantage (other than less code) to using % rather than a simple counter?
waitTime = SOS[index]; // waitTime duration = SOS array
index++; // increment index + 1
if(index >= 9) // if index greater than 8
{
index = 0; // reset index to 0
}
The advantage of using % is that it's simple and tidy, nothing else as far as I know. When I first learnt C I didn't use it because I found it easier to understand if I wrote out the code the long way as you have done. Do it whichever way you prefer and don't worry about it.
The Morse code programs I have looked at ALL decide on a basic unit of Morse code time. They all pick the length of time for a DOT. A dash might be two or three dots in length. The time between letters might be 4 dots. The time between words might be 5 dots.
Then if you want the code to be faster or slower, only the actual timing of a dot needs to be changed.
Paul