Hi guys!
I am a new to not only Arduino but programming anything! I am a old Harley Mechanic, at least that is what we use to be called now we are Technicians, LOL! I bought a Arduino Starter kit for my young daughter about a year ago trying to get her interested in something other than her phone and what an absolute failure that was! After doing the projects in the Arduino projects book with my daughter, and boy was that miserable, it peaked my interest and I came to a realization of the potential of an Arduino! I have made a couple of projects but that is all the experience I have! The only reason I am writing all this is to let you know I am very inexperienced! OK, so here is what I am trying to do. I wrote a sketch that gets a high signal when a button is depressed. I have a String in all caps that is used to traverses an array to find the corresponding Morse Code for a letter stored in the array and out put the code to an LED then move to the next letter in the String. I can get it to work with the delay function but I was trying to use the millis function. I have been using the Serial monitor to try and debug but that hasn't helped at all. I have been beating my head against a wall for more than a couple weeks and have run out of ideas on what is going on. I have tried creating a class that will just do the blinking, a function that will just do the blinking, making flags then trying to blink it at the end of the main loop before it reiterates, I think that's the word, restarts at the beginning of the main loop. My sketch now does nothing to the LED, unless it is happening so fast I can't see it, and doesn't print out code in the Serial monitor line by line like I had it with the delays it just prints the entire code for the String all at once. I to uploaded the sketch, like the forum rules say but it said new users can't upload, so I just copied and pasted!
int charCounter = 0;
int SwitchState = LOW;
int buttonState = 0;
const int arraySize =36;
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-----"};
unsigned long dotInterval = 200;
unsigned long dashInterval = 600;
unsigned long previousMillis = 0;
bool ledState = false;
/******************************************************************************/
String myWord = "SOS"; //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(){
unsigned long currentMillis = millis();
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) == '.'){
if(SwitchState == HIGH){
Serial.print(" . ");
if((unsigned long)(currentMillis = previousMillis)>= dotInterval){
ledState = !ledState;
digitalWrite(outPut1, ledState);
previousMillis = millis();
}
}
}
if(morseAlphabet[i].charAt(j) == '-'){
if(SwitchState == HIGH){
Serial.print(" - ");
if((unsigned long)(currentMillis = previousMillis)>= dashInterval){
ledState = !ledState;
digitalWrite(outPut1, ledState);
previousMillis = millis();
}
}
}
}
i =-1;
Serial.println();
charCounter++;
}
}
}
if(SwitchState == HIGH){
for(int a = 0; a < 600; a++){
delay(1);
}
}
charCounter = 0;
}