Hi all, Im trying to make a program that can multitask and detect button presses and other inputs while a set LED flashes a word in morse code. I am not using delay() but only millis() to create delays, but my program wont multitask (The "Test" string I want to print only prints before the morse runs, not during it as well). How can I use Millis diffrently to get this multitasking to work?
Thanks in Advance! Code below:
//How long the LED should be on for a Dot
const unsigned long intervalOfDot = 400;
//How long the LED should be on for a Dash
const unsigned long intervalOfDash = 1000;
//Interval of Off LED between dots / dashes
const unsigned long intervalBetweenDotDash = 300;
//Interval of Off LED between Letters
const unsigned long intervalBetweenLetters = 1250;
//Interval of OFF LED between the repeat of a word
const unsigned long intervalRepeat = 2750;
//Word I want to Morse
String message = "BISTRO";
//LED for Morse display
int port = 12;
//Variable used to grab a time at a point and then use to wait a certian interval.
unsigned long timeCheck;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(port, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
//Word printing here to see if multitasking works while flashing morse - problem is it only prints TEST once before the morse flash - doesnt repeat
Serial.println("TEST");
morse(message);
}
//for the dot of a morse
void smalls(int freq) {
for(int i = 0; i<freq; i++){
digitalWrite(port, HIGH);
//Waits for interval of Dot time before turning LED off
timeCheck = millis();
while(!(millis() > intervalOfDot + timeCheck)){
}
digitalWrite(port,LOW);
//Waits for interval of BetweemDotDash time before ending function - goes to next letter
timeCheck = millis();
while(!(millis() > intervalBetweenDotDash + timeCheck)){
}
}
}
//for the dash of a morse
void larges(int freq) {
for(int i = 0; i<freq; i++){
digitalWrite(port, HIGH);
//Waits for interval of Dash time before turning LED off
timeCheck = millis();
while(!(millis() > intervalOfDash + timeCheck)){
}
digitalWrite(port,LOW);
//Waits for interval of BetweemDotDash time before ending function - goes to next letter
timeCheck = millis();
while(!(millis() > intervalBetweenDotDash + timeCheck)){
}
}
}
//Method to get each character of the word and turn it into the proper morse dot/dash combonation
void morse(String code) {
for(int i = 0; i <=code.length(); i++){
//When i reaches the end of the word (equal to length), a timecheck is stored and a while loop is made to wait until current time passes
if(i == code.length()){
timeCheck = millis();
while(!(millis() > intervalRepeat + timeCheck)){
}
}
//Else, a Letter is taken and converted into Morse
else{
String section = code.substring(i,i+1);
if(section.equals("A") || section.equals("a")){
smalls(1);
larges(1);
}
else if(section.equals("B") || section.equals("b") ){
larges(1);
smalls(3);
}
else if(section.equals("C") || section.equals("c") ){
larges(1);
smalls(1);
larges(1);
smalls(1);
}
else if(section.equals("D") || section.equals("d")){
larges(1);
smalls(2);
}
else if(section.equals("E") || section.equals("e")){
smalls(1);
}
else if(section.equals("F") || section.equals("f")){
smalls(2);
larges(1);
smalls(1);
}
else if(section.equals("G") || section.equals("g")){
larges(2);
smalls(1);
}
else if(section.equals("H") || section.equals("h")){
smalls(4);
}
else if(section.equals("I") || section.equals("i")){
smalls(2);
}
else if(section.equals("J") || section.equals("j")){
smalls(1);
larges(3);
}
else if(section.equals("K") || section.equals("k")){
larges(1);
smalls(1);
larges(1);
}
else if(section.equals("L") || section.equals("l")){
smalls(1);
larges(1);
smalls(2);
}
else if(section.equals("M") || section.equals("m")){
larges(2);
}
else if(section.equals("N") || section.equals("n")){
larges(1);
smalls(1);
}
else if(section.equals("O") || section.equals("o")){
larges(3);
}
else if(section.equals("P") || section.equals("p")){
smalls(1);
larges(2);
smalls(1);
}
else if(section.equals("Q") || section.equals("q")){
larges(2);
smalls(1);
larges(1);
}
else if(section.equals("R") || section.equals("r")){
smalls(1);
larges(1);
smalls(1);
}
else if(section.equals("S") || section.equals("s")){
smalls(3);
}
else if(section.equals("T") || section.equals("t")){
larges(1);
}
else if(section.equals("U") || section.equals("u")){
smalls(2);
larges(1);
}
else if(section.equals("V") || section.equals("v")){
smalls(3);
larges(1);
}
else if(section.equals("W") || section.equals("w")){
smalls(1);
larges(2);
}
else if(section.equals("X") || section.equals("x")){
larges(1);
smalls(2);
larges(1);
}
else if(section.equals("Y") || section.equals("y")){
larges(1);
smalls(1);
larges(2);
}
else if(section.equals("Z") || section.equals("z")){
larges(2);
smalls(2);
}
else if(section.equals("0")){
larges(5);
}
else if(section.equals("1")){
smalls(1);
larges(4);
}
else if(section.equals("2")){
smalls(2);
larges(3);
}
else if(section.equals("3")){
smalls(3);
larges(2);
}
else if(section.equals("4")){
smalls(4);
larges(1);
}
else if(section.equals("5")){
smalls(5);
}
else if(section.equals("6")){
larges(1);
smalls(4);
}
else if(section.equals("7")){
larges(2);
smalls(3);
}
else if(section.equals("8")){
larges(3);
smalls(2);
}
else{
larges(4);
smalls(1);
}
//Once the letter is succesfully morsed, this statment waits the program in the loop until it is time to move on
timeCheck = millis();
while(!(millis() > intervalBetweenLetters + timeCheck)){
}
}
}
}