// // NOTE: "setFrequency(int value)" int = 0 turn off the frequencies // int = 1 turn on the frequencies // // ********************************************************************* void blinker(int loopTime, int delayTime){ for(int b=0; b0){ display.setTextSize(2); display.setCursor(0,0); // set to origin display line display.print("File "); // message line 1 display.print(fileNo); // state what file was in error display.setCursor(0,24); // set to next display line display.print("FAILURE"); // message line 2 display.display(); // DISPLAY TO THE SCREEN blinker(50, 125); // show the card is not plugged in while(1); // infinit loop } singleShot = 0; // readFile uses singleShot so restore it to zero (0) fetchMem(); // Everything is now stored in flash memory fetchProtoID(0); // Get the 'memGrp' number from parameter line 0 fetchProtoID(memGrp); // Now get the starting parameters for the program fileNoHold = fileNo; // this is the file number you are working with, save it display.setTextSize(1); // restore the font size to small display.display(); // UPDATE Display Parameters } // ********************************************************************* void fetchProtoID(int value){ protoID=value; // fetch protoID number //Serial.print("protoID = "); //Serial.println(protoID); if(protoID != 0){ EPromObject1 customVar1; // Variables to store custom objects in EEPROM eeAddress=(protoID-1)*36; // point to proper "Frequency Set" eeprom area EEPROM.get(eeAddress,customVar1); // Fetch data from EPROM protoID=customVar1.field0; // fetch the the 'set' related to this protoID F1=customVar1.field1; // Fetch and load Frequency 1 D1=customVar1.field2; // Fetch and load Duty Cycle 1 F2=customVar1.field3; // Fetch and load Frequency 2 D2=customVar1.field4; // Fetch and load Duty Cycle 2 F3=customVar1.field5; // Fetch and load Frequency 3 D3=customVar1.field6; // Fetch and load Duty Cycle 3 F4=customVar1.field7; // Fetch and load Frequency 4 D4=customVar1.field8; // Fetch and load Duty Cycle 4 } else { EPromObject2 customVar2; // Variables to store custom objects in EEPROM eeAddress=eeAddress2; // define starting eeprom address EEPROM.get(eeAddress,customVar2); // Fetch data from the EProm protoID=customVar2.field0; // should be a zero / changed to a number sign '#' memGrp=customVar2.field1; // protoID to start with modeID=customVar2.field2; // fetch the mode from EEPROM endTime1=customVar2.field3; // fetch the frequency run time from the EEPROM endTime2=customVar2.field4; // fetch the Program run time from the EEPROM startFreqGrpID=customVar2.field5; // fetch the starting Frequency Group ID stopFreqGrpID=customVar2.field6; // fetch the stopping Frequency Group ID startSweepGrpID=customVar2.field7;// fetch the starting Sweep Group ID stopSweepGrpID=customVar2.field8; // fetch the stopping Sweep Group ID sweepFreqInc=customVar2.field9; // fetch the sweep Frequency increment value fileNo=customVar2.field10; // file number ranging from 0 to 9 } // ************************************* FOR DEBUGGING *************************** /* if(protoID != 0){ // For DEBUGGING Serial.println("protoID, F1, D1, F2, D2, F3, D3, F4, D4"); Serial.print(protoID); Serial.print(" "); Serial.print(F1); Serial.print(" "); Serial.print(D1); Serial.print(" "); Serial.print(F2); Serial.print(" "); Serial.print(D2); Serial.print(" "); Serial.print(F3); Serial.print(" "); Serial.print(D3); Serial.print(" "); Serial.print(F4); Serial.print(" "); Serial.println(D4); } else { Serial.println("ENDING DATA"); Serial.print(protoID); Serial.print(", "); Serial.print(memGrp); Serial.print(", "); Serial.print(modeID); Serial.print(", "); Serial.print(endTime1); Serial.print(", "); Serial.print(endTime2); Serial.print(", "); Serial.print(startFreqGrpID); Serial.print(", "); Serial.print(stopFreqGrpID); Serial.print(", "); Serial.print(startSweepGrpID); Serial.print(", "); Serial.print(stopSweepGrpID); Serial.print(", "); Serial.print(sweepFreqInc); Serial.print("\n"); } */ } // ********************************************************************* void setFrequency(int value){ d1=D1; // create a backup for developing & debugging d1=D2; // create a backup for developing & debugging d3=D3; // create a backup for developing & debugging d4=D4; // create a backup for developing & debugging d1=1.00-D1; // invert duty cycle for hardware inversion d2=1.00-D2; // invert duty cycle for hardware inversion d3=1.00-D3; // invert duty cycle for hardware inversion d4=1.00-D4; // invert duty cycle for hardware inversion //------------------------------------ ledcSetup(channel1, (F1*FreqAdjust), prescaler); // channel, frequency, resolution delay(5); // give it a little delay to lock in if(value==0) dutyValue = dutyConst; // Restore to dutyConst to generate hardware low value else dutyValue = dutyConst * d1; // duty cycle calculation ledcWrite(channel1, dutyValue); // channel, LED_PIN //------------------------------------ ledcSetup(channel2, (F2*FreqAdjust), prescaler); // channel, frequency, resolution delay(5); // give it a little delay to lock in if(value==0) dutyValue = dutyConst; // Restore to dutyConst to generate hardware low value else dutyValue = dutyConst * d2; // duty cycle calculation ledcWrite(channel2, dutyValue); // channel, LED_PIN //------------------------------------ ledcSetup(channel3, (F3*FreqAdjust), prescaler); // channel, frequency, resolution delay(5); // give it a little delay to lock in if(value==0) dutyValue = dutyConst; // Restore to dutyConst to generate hardware low value else dutyValue = dutyConst * d3; // duty cycle calculation ledcWrite(channel3, dutyValue); // channel, LED_PIN //------------------------------------ ledcSetup(channel4, (F4*FreqAdjust), prescaler); // channel, frequency, resolution delay(5); // give it a little delay to lock in if(value==0) dutyValue = dutyConst; // Restore to dutyConst to generate hardware low value else dutyValue = dutyConst * d4; // duty cycle calculation ledcWrite(channel4, dutyValue); // channel, LED_PIN } // ********************************************************************* void saveCurrent(){ eeAddress=(protoID-1)*36; // point to proper "Frequency Set" eeprom area EPromObject1 customVar1; // Variables to store custom objects in EEPROM customVar1.field0=protoID; // protoID customVar1.field1=F1; // F1 customVar1.field2=D1; // D1 customVar1.field3=F2; // F2 customVar1.field4=D2; // D2 customVar1.field5=F3; // F3 customVar1.field6=D3; // D3 customVar1.field7=F4; // F4 customVar1.field8=D4; // D4 EEPROM.put(eeAddress,customVar1); // Update Flash memory0 eeAddress = eeAddress2; // save this FLash Memory address EPromObject2 customVar2; // Variables to store custom objects in EEPROM customVar2.field0=protoID; // Always zero (0) to indicate ENDING GROUP customVar2.field1=memGrp; // This will contain last Memory Group used by program customVar2.field2=modeID; // This will contain last mode used/set customVar2.field3=endTime1; // This will contain Frequency run time customVar2.field4=endTime2; // This will contain Program run time customVar2.field5=startFreqGrpID; // Contains Start Memory Group for Protocol Mode 2 customVar2.field6=stopFreqGrpID; // Contains Stop Memory Group for Protocol Mode 2 customVar2.field7=startSweepGrpID; // Contains Start Memory Group for Sweep Mode 3 customVar2.field8=stopSweepGrpID; // Comtains Stop Memory Group for Sweep Mode 3 customVar2.field9=sweepFreqInc; // sweep Frequency Increment value customVar2.field10=(fileNo); // file number a number from 0 to 9 converted to char EEPROM.put(eeAddress,customVar2); // Update Flash memory0 } // ********************************************************************* void fetchMem(){ if(DEBUG) Serial.println("Entering 'fetchFmMem()'"); intCnt = 0; protoID = 1; eeAddress = FLASH_START; EPromObject1 customVar1; // Variables to store custom objects in EEPROM while(protoID != 0){ eeAddress = FLASH_START+(36*intCnt); ++intCnt; EEPROM.get(eeAddress,customVar1); // Fetch data from EPROM protoID=customVar1.field0; // fetch the the 'set' related to this protoID if(protoID==0) break; F1=customVar1.field1; // Fetch and load Frequency 1 D1=customVar1.field2; // Fetch and load Duty Cycle 1 F2=customVar1.field3; // Fetch and load Frequency 2 D2=customVar1.field4; // Fetch and load Duty Cycle 2 F3=customVar1.field5; // Fetch and load Frequency 3 D3=customVar1.field6; // Fetch and load Duty Cycle 3 F4=customVar1.field7; // Fetch and load Frequency 4 D4=customVar1.field8; // Fetch and load Duty Cycle 4 if(DEBUG){ Serial.print(protoID); Serial.print(", "); Serial.print(F1); Serial.print(", "); Serial.print(D1); Serial.print(", "); Serial.print(F2); Serial.print(", "); Serial.print(D2); Serial.print(", "); Serial.print(F3); Serial.print(", "); Serial.print(D3); Serial.print(", "); Serial.print(F4); Serial.print(", "); Serial.print(D4); Serial.print("\n"); } } EPromObject2 customVar2; // Variables to store custom objects in EEPROM EEPROM.get(eeAddress,customVar2); // Fetch data from EPROM protoID=customVar2.field0; // fetch the the 'set' related to this protoID memGrp=customVar2.field1; // Fetch and load Frequency 1 modeID=customVar2.field2; // Fetch and load Duty Cycle 1 endTime1=customVar2.field3; // Fetch and load Frequency 2 endTime2=customVar2.field4; // Fetch and load Duty Cycle 2 startFreqGrpID=customVar2.field5; // Fetch and load Frequency 3 stopFreqGrpID=customVar2.field6; // Fetch and load Duty Cycle 3 startSweepGrpID=customVar2.field7; // Fetch and load Frequency 4 stopSweepGrpID=customVar2.field8; // Fetch and load Duty Cycle 4 sweepFreqInc=customVar2.field9; // sweep Frequency Increment value fileNo=customVar2.field10; // file number ranging from 0 to 9 if(DEBUG){ Serial.print(protoID); Serial.print(", "); Serial.print(memGrp); Serial.print(", "); Serial.print(modeID); Serial.print(", "); Serial.print(endTime1); Serial.print(", "); Serial.print(endTime2); Serial.print(", "); Serial.print(startFreqGrpID); Serial.print(", "); Serial.print(stopFreqGrpID); Serial.print(", "); Serial.print(startSweepGrpID); Serial.print(", "); Serial.print(stopSweepGrpID); Serial.print(", "); Serial.print(sweepFreqInc); Serial.print(", "); Serial.print(fileNo); Serial.print("\n"); Serial.println("Exiting 'fetchFmMem()'"); } } // ********************************************************************* void setClocks(byte function){ // check what you want to do if(function == false){ // on condition zero out the RunTimes(s) runTime1=runTime2=0; // zero the Runtimes } if(function == true){ // if true then set the RunTime(s) runTime1=endTime1+millis(); // set the RUN Frequency Set time runTime2=endTime2+millis(); // set the RUN Protocol time } } // ********************************************************************* void checkTime(){ runTime=millis(); // fetch the present millisecond time if(runFlag == 1 && !digitalRead(cursorPin)){ displaySuspended(); savePgmTime = runTime2 - runTime; // this id to restore time left for program time saveRunTime = runTime1 - runTime; // to restore time left for frequency runtime setFrequency(0); // turn off the four channel frequencies delay(1000); // used for debouncing the switch connection while(digitalRead(cursorPin)); // loop forever or until unsuspended displayRunning(); setFrequency(1); // turn on the four channel frequencies runTime=millis(); // fetch the present millisecond time runTime2 = runTime+savePgmTime; // restore Program Run time runTime1 = runTime+saveRunTime; // restore frequency Run Time delay(1000); // used for debouncing the swithc connection runFlag=1; // restore the runFlag value incase if flipped } if(runTime2stopFreqGrpID) protoID=startFreqGrpID; // on condition cycle back to start Frequency Group fetchProtoID(protoID); // fetch the frequency group setFrequency(1); // LIGHT UP THE FREQUENCIES in other words START them runTime1=endTime1+millis(); // set the RUN Frequency Set time // Display(); // The Display will need to be activated } // ********************************************************************* void sweep(){ fetchProtoID(startSweepGrpID); // get all 4 frequencies and duty cycle values F1=sweepAccum; // replace F1 from above fetch with accumulator value if(DEBUG) Serial.println(F1); sweepAccum+=sweepWork2; // sweep accumulator updated for next time trhough setFrequency(1); // LIGHT UP THE FREQUENCIES in other words START them if(sweepNumCnt==0){ sweepNumCnt=sweepWork1; // restore the number count sweepAccum = sweepF1; // restore the sweep accumulator F1=sweepAccum; // replace F1 from above fetch with accumulator value if(DEBUG) Serial.println(F1); setFrequency(1); // LIGHT UP THE FREQUENCIES in other words START them } --sweepNumCnt; // update the counter for next time throught } // ********************************************************************* void calcSweepParams(){ fetchProtoID(startSweepGrpID); // fetch the start to start frequency sweepAccum=F1; // intialize the Sweep Accummulater sweepF1=F1; // fetch beginning frequency & put into a work field fetchProtoID(stopSweepGrpID); // fetch frequency limi or stop frequency sweepF2=F1; // fetch ending frequency if(sweepFreqInc<=0.0){ // on condition calculate a frequency increment sweepWork1= endTime2/endTime1-1; // caculate how many frequency inc/dec possible sweepNumCnt=sweepWork1; // load above calculation to Sweep Number Count sweepWork2=(sweepF2-sweepF1)/sweepWork1; // equals how much to add to accumulator } else { sweepNumCnt=(sweepF2-sweepF1)/sweepFreqInc; // equals how much to add to accumulator sweepWork2=sweepFreqInc; // if here use sweep Frequency Increment value } sweepWork1=sweepNumCnt; // copy sweepNumCnt to sweepWork1 // ***** LEFT IN FOR DEBUGGING, works fine so presently can be removed desired ******** if(DEBUG) { Serial.print("sweepF1 = "); Serial.println(sweepF1); Serial.print("sweepF2 = "); Serial.println(sweepF2); Serial.print("sweepAccum = "); Serial.println(sweepAccum); Serial.print("sweepWork1 = "); Serial.println(sweepWork1); Serial.print("sweepWork2 = "); Serial.println(sweepWork2); Serial.print("sweepNumCnt = "); Serial.println(sweepNumCnt); } } // ********************************************************************* void checkSaving(){ if(!digitalRead(cursorPin)){ display.clearDisplay(); // Clear the OLED buffer display.setCursor(0,0); // Set the OLED cursor postion display.setTextSize(2); // Set the OLED text size display.print("Update the"); // Display the quoted text display.setCursor(0,20); // Set the OLED cursor postion display.print("SD Card ??"); // Display the quoted text curX = line0XC; // restore the CurX to origin curY = line0YC; // resotore the CurY to origin display.setCursor(curX,curY); // set user cursor position display.print("_"); // using the underscore to show display cursor position display.display(); // This command actually displays text to the OLED blinker(8, 250); } if(!digitalRead(cursorPin)){ display.clearDisplay(); // Clear the OLED buffer display.setCursor(0,0); // Set the OLED cursor postion display.print("SDram Card"); // Display the quoted text display.setCursor(0,24); // Set the OLED cursor postion display.print(" UPDATING"); // Display the quoted text display.display(); // This command actually displays text to the OLED saveFile(); } } // ********************************************************************* void saveFile(){ protoIDhold=protoID; // save entry protoID // fileNoHold = fileNo; // save back fileNoHold to see if another file read needed fileName[11]=(fileNoHold|0x30); // convert byte fileNo value into a character byte writeFile(SD, fileName, "Protocols-x #\n"); // writeFile(SD, "/protocols-0.txt", "Protocols-0 #\n"); for(protoID=1; protoID<=protoIDlast; ++protoID){ fetchProtoID(protoID); workStr = ""; workStr+=String(protoID); workStr+=(","); workStr+=String(F1); workStr+=(","); workStr+=String(D1); workStr+=(","); workStr+=String(F2); workStr+=(","); workStr+=String(D2); workStr+=(","); workStr+=String(F3); workStr+=(","); workStr+=String(D3); workStr+=(","); workStr+=String(F4); workStr+=(","); workStr+=String(D4); workStr+=("\n"); appendFile(SD, fileName, workStr.c_str()); // appendFile(SD, "/protocols-0.txt", workStr.c_str()); if(DEBUG) Serial.print(workStr); } fetchProtoID(0); workStr = ""; workStr+=String(0); workStr+=(","); workStr+=String(protoIDhold); workStr+=(","); workStr+=String(modeID); workStr+=(","); workStr+=String(endTime1); workStr+=(","); workStr+=String(endTime2); workStr+=(","); workStr+=String(startFreqGrpID); workStr+=(","); workStr+=String(stopFreqGrpID); workStr+=(","); workStr+=String(startSweepGrpID); workStr+=(","); workStr+=String(stopSweepGrpID); workStr+=(", "); workStr+=String(sweepFreqInc); workStr+=(", "); workStr+=String(fileNo); workStr+=("\n"); appendFile(SD, fileName, workStr.c_str()); // appendFile(SD, "/protocols-0.txt", workStr.c_str()); fetchProtoID(protoIDhold); blinker(20, 100); displayRunning(); if(DEBUG) Serial.print(workStr); }