Hello again,
I am building a box that flashes a yellow LED based on a set of words programed and the user must figure out what the morse is.
I have all of the buttons and 7 segment display pieces working as intended but the flashing led for dot/dash of the morse is failing to work in 2 ways.
First I know I am doing something wrong with my "void morseBlinkWord(){" that is supposed to read an array for the morse and based on if the number in the array is a 0,1,2 it needs to either flash a "dot" or a "dash". If it is a 0 it should just "pause" for a fixed # of ms.
This ^^^ breaks everything else and may be the fix for the below issue too.
Secondly the dot and dash is not the correct ms length that I have it set to use. It stays on and off for far to long when it does work at all?
morse.h:
/*
* The Morse Code module is a module that consists of a light flashing in Morse Code, a radio with a displayed frequency and a TX button.
* The user must interpret the flashing Morse Light as dots and dashes to form a word in Morse Code.
*/
#define PIN_MORSE_LED_1 3 // yellow flashing LED
#define PIN_MORSE_LED_GREEN 12 // module complete LED
#define PIN_MORSE_BUTTON_1 4 // left button
#define PIN_MORSE_BUTTON_2 5 // right button
#define PIN_MORSE_BUTTON_3 6 // TX button
#define MORSE_BUTTON_PRESS_DELAY 50 // time required to prevent button bounce
int morseLatch=9; // 74HC595 pin 9 STCP
int morseClock=10; // 74HC595 pin 10 SHCP
int morseData=8; // 74HC595 pin 8 DS
int morseCurrentDisplayNumber=1; // what station is displayed
int buttonLeftState=0; // current state of the left button
int buttonRightState=0; // current state of the right button
int buttonSubmitState=0; // current state of the submit button
int lastButtonLeftState=0; // previous state of the left button
int lastButtonRightState=0; // previous state of the right button
int lastButtonSubmitState=0; // previous state of the submit button
int morseCorrectNumber=1;
const unsigned long morseYellowLEDDot = 100; // length of dot
const unsigned long morseYellowLEDDash = 400; // length of dash
const unsigned long morseLetterLEDDelay = 500; // length of delay between letters
const unsigned long morseDotDashLEDDelay = 200; // length of delay between dot/dash in same letter
const unsigned long morseWordLEDDelay = 1500; // delay between word repeat
unsigned long morseDelayCounter = 0; //a value that will count up while waiting for new letter/word //pointless action for while loop
unsigned long morseYellowLEDtimerOn;
unsigned long morseYellowLEDtimerOff;
unsigned long morseLetterFinishedtimer;
unsigned long morseWordFinishedtimer;
unsigned char morseTable[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
// 0x3f="0",0x06="1",0x5b="2",0x4f="3",0x66="4",0x6d="5",0x7d="6",0x07="7",0x7f="8",0x6f="9",
//0x77="A",0x7c="b",0x39="C",0x5e="d",0x79="E",0x71="F",0x00="OFF"
int morseWord1[] = {1,1,1,0,1,1,1,1,0,1,0,1,2,1,1,0,1,2,1,1}; //word shell where 1=dot 2=dash, 0=new letter pause
void morseSetup()
{
pinMode(morseLatch,OUTPUT);
pinMode(morseClock,OUTPUT);
pinMode(morseData,OUTPUT);
pinMode(PIN_MORSE_LED_1,OUTPUT);
pinMode(PIN_MORSE_LED_GREEN,OUTPUT);
pinMode(PIN_MORSE_BUTTON_1,INPUT);
pinMode(PIN_MORSE_BUTTON_2,INPUT);
pinMode(PIN_MORSE_BUTTON_3,INPUT);
morseYellowLEDtimerOn = millis ();
morseYellowLEDtimerOff = millis ();
morseLetterFinishedtimer = millis ();
morseWordFinishedtimer = millis ();
}
void morseDisplay(unsigned char num)
{
digitalWrite(morseLatch,LOW);
shiftOut(morseData,morseClock,MSBFIRST,morseTable[num]);
digitalWrite(morseLatch,HIGH);
}
void morseYellowLEDOn ()
{
digitalWrite (PIN_MORSE_LED_1, HIGH);
morseYellowLEDtimerOn = millis ();
}
void morseYellowLEDOff ()
{
digitalWrite (PIN_MORSE_LED_1, LOW);
morseYellowLEDtimerOff = millis ();
}
void morseDot(){
if ((digitalRead (PIN_MORSE_LED_1) == LOW) && ((millis () - morseYellowLEDtimerOff) >= morseDotDashLEDDelay)) {
morseYellowLEDOn ();
}
else if ( (millis () - morseYellowLEDtimerOn) >= morseYellowLEDDot) {
morseYellowLEDOff ();
}
}
void morseDash(){
if ((digitalRead (PIN_MORSE_LED_1) == LOW) && ((millis () - morseYellowLEDtimerOff) >= morseDotDashLEDDelay)) {
morseYellowLEDOn ();
}
else if ( (millis () - morseYellowLEDtimerOn) >= morseYellowLEDDash) {
morseYellowLEDOff ();
}
}
void morseBlinkWord(){ // not expanded for other words as only 1 word array is built for testing
if (morseCorrectNumber==1) {
for (int i = 0; i < 20; i = i + 1) {
if(i == 1){
morseDot();
}
else if(i == 2){
morseDash();
}
else if(i == 0){
morseLetterFinishedtimer = millis () ;
Serial.print(morseLetterFinishedtimer);
while (millis () - morseLetterFinishedtimer < morseLetterLEDDelay){
Serial.print(millis () - morseWordFinishedtimer);
morseDelayCounter+1 ;
}
}
morseWordFinishedtimer = millis () ;
while (millis () - morseWordFinishedtimer < morseWordFinishedtimer){
Serial.print(millis () - morseWordFinishedtimer);
morseDelayCounter+1 ;
}
}
}
}
void morseLeftButtonPressed(){
if (morseCurrentDisplayNumber > 0) {
morseCurrentDisplayNumber=morseCurrentDisplayNumber-1;
morseDisplay(morseCurrentDisplayNumber);
delay(MORSE_BUTTON_PRESS_DELAY);
} else {
morseDisplay(morseCurrentDisplayNumber);
}
}
void morseRightButtonPressed(){
if (morseCurrentDisplayNumber < 15) {
morseCurrentDisplayNumber=morseCurrentDisplayNumber+1;
morseDisplay(morseCurrentDisplayNumber);
delay(MORSE_BUTTON_PRESS_DELAY);
} else {
morseDisplay(morseCurrentDisplayNumber);
}
}
void morseSubmitButtonPressed(){
Serial.println(morseCurrentDisplayNumber);
Serial.println(morseCorrectNumber);
if (morseCurrentDisplayNumber==morseCorrectNumber) {
morseModuleDefused = true;
digitalWrite(PIN_MORSE_LED_GREEN,HIGH);
}
else { //if (morseCurrentDisplayNumber!=morseCorrectNumber) {
addStrike();
morseDisplay(0);
delay(500);
morseDisplay(morseCurrentDisplayNumber);
}
}
void morseLoop()
{
// read the pushbutton input pins:
buttonLeftState = digitalRead(PIN_MORSE_BUTTON_1);
buttonRightState = digitalRead(PIN_MORSE_BUTTON_2);
buttonSubmitState = digitalRead(PIN_MORSE_BUTTON_3);
// compare the buttonLeftState to its previous state
if (buttonLeftState != lastButtonLeftState) {
// if the state has changed, increment the counter
if (buttonLeftState == HIGH) {
Serial.println("leftOn");
morseLeftButtonPressed();
} else {
Serial.println("leftOff");
}
// Delay to avoid bouncing
delay(MORSE_BUTTON_PRESS_DELAY);
}
// save current state as last state
lastButtonLeftState = buttonLeftState;
if (buttonRightState != lastButtonRightState) {
if (buttonRightState == HIGH) {
Serial.println("rightOn");
morseRightButtonPressed();
} else {
Serial.println("rightOff");
}
// Delay to avoid bouncing
delay(MORSE_BUTTON_PRESS_DELAY);
}
lastButtonRightState = buttonRightState;
if (buttonSubmitState != lastButtonSubmitState) {
if (buttonSubmitState == HIGH) {
Serial.println("submitOn");
morseSubmitButtonPressed();
} else {
Serial.println("submitOff");
}
delay(MORSE_BUTTON_PRESS_DELAY);
}
lastButtonSubmitState = buttonSubmitState;
// display starting number (always fixed lowest # in array)
morseDisplay(morseCurrentDisplayNumber);
// IF THIS STATEMENT IS ENABLED I CAN NO LONGER PRESS BUTTONS AND THE FLASHING DOES NOT WORK AS INTENDED CURRENTLY
if (!morseModuleDefused) morseBlinkWord();
}
void morseModuleBoom()
{
// if the WRONG ANSWER is input the display with the frequency should clear and the flashing led should stop
digitalWrite(PIN_MORSE_LED_1,LOW); // stop flashing yellow morse
digitalWrite(PIN_MORSE_LED_GREEN,LOW); // turn off green module complete LED
morseDisplay(16); //shows " " on module tx display
}