All right here it is, I already new how to do this with delays, like I wrote, I really wanted to use millis but obviously it's above my pay grade currently, as I cannot figure it out, I will one day!!! Until then I am using this! I am ultimately going to use this to make a tail light controller, I have already made one for another one of my scoots, but it doesn't use Morse code it is just flashes a couple times, to grab the person's attention behind me. It works almost perfect! Everything except for the stupid slight delay when letting of the brakes and it is blocked from the delay function. Funny thing is I am the only one who notices it! Yes, I know the output to the Serial monitor needs to be removed it was only in for testing. Before any on asks no it won't be flashing "Hello Cruel World"! Any and all POSITIVE criticism is welcome and appreciated, yes I know I need comments and I know it feels clumsy!
int charCounter = 0;
int SwitchState = LOW;
int duration = 200;
const int arraySize =37;
const byte outPut1 = 10;
const byte InputPin = 4;
String morseAlphabet[arraySize] = {"A.-","B-...","C-.-.","D-..","E.","F..-.",
"G--.","H....","I..","J.---","K-.-","L.-..","M--","N-.","O---","P.--.",
"Q--.-","R.-.","S...","T-","U..-","V...-","W.--","X-..-","Y-.--","Z--..",
"1.----","2..---","3...--","4....-","5.....","6-....","7--...",
"8---..","9----.","0-----", " "};
/******************************************************************************/
String myWord = "HELLO CRUEL WORLD";//HAS TO BE IN UPPER CASE LETTERS OR IT WON'T WORK!!
/******************************************************************************/
void setup(){
Serial.begin(9600);
pinMode(outPut1, OUTPUT);
pinMode(InputPin, INPUT);
digitalWrite(outPut1, LOW);
digitalWrite(InputPin, LOW);
}
void loop(){
SwitchState = digitalRead(InputPin);
if(SwitchState == HIGH && charCounter == 0){
for(int i =0; i< arraySize; i++){
if(morseAlphabet[i].charAt(0) == myWord.charAt(charCounter)){
for(int j = 0; j < morseAlphabet[i].length(); j++){
if(morseAlphabet[i].charAt(j) == '.'){
SwitchState = digitalRead(InputPin);
if(SwitchState){
Serial.print(" . ");
digitalWrite(outPut1, HIGH);
for(int a = 0; a < duration; a++){
delay(1);
}
digitalWrite(outPut1, LOW);
for(int a = 0; a < duration; a++){
delay(1);
}
}
}
if(morseAlphabet[i].charAt(j) == '-'){
SwitchState = digitalRead(InputPin);
if(SwitchState){
Serial.print(" - ");
digitalWrite(outPut1, HIGH);
for(int a = 0; a < (3*duration); a++){
delay(1);
}
digitalWrite(outPut1, LOW);
for(int a = 0; a < duration; a++){
delay(1);
}
}
}
if(morseAlphabet[i].charAt(j) == ' '){
SwitchState = digitalRead(InputPin);
if(SwitchState){
Serial.print(" ");
digitalWrite(outPut1, LOW);
for(int a = 0; a < (3*duration); a++){
delay(1);
}
digitalWrite(outPut1, LOW);
for(int a = 0; a < (3*duration); a++){
delay(1);
}
}
}
}
i =-1;
Serial.println();
charCounter++;
}
}
}
if(SwitchState == HIGH){
Serial.println("HIGH HIGH HIGH");
digitalWrite(outPut1, HIGH);
}
else{
charCounter = 0;
digitalWrite(outPut1, LOW);
}
}