/* ===== pfod Command for Menu_4 ==== pfodApp msg {.} --> {,<+4>~Stepper Control`0~V1|A<+6>~Run|B<+6>~Stop|C<+5>~Home|!D<+3>~ |E<+3>~Stepper Plots} */ // Using Serial and 9600 baud for send and receive // Serial D0 (RX) and D1 (TX) on Arduino Uno, Micro, ProMicro, Due, Mega, Mini, Nano, Pro and Ethernet // This code uses Serial so remove shield when programming the board /* Code generated by pfodDesignerV3 V3.0.4181 */ /* * (c)2014-2021 Forward Computing and Control Pty. Ltd. * NSW Australia, www.forward.com.au * This code is not warranted to be fit for any purpose. You may only use it at your own risk. * This generated code may be freely used for both private and commercial use * provided this copyright is maintained. */ // download the libraries from http://www.forward.com.au/pfod/pfodParserLibraries/index.html // pfodParser.zip V3.52+ contains pfodParser, pfodSecurity, pfodDelay, pfodBLEBufferedSerial, pfodSMS and pfodRadio #include int swap01(int); // method prototype for slider end swaps float getPlotVarScaling(long varMax, long varMin, float displayMax, float displayMin); pfodParser parser("V2"); // create a parser to handle the pfod messages unsigned long plot_msOffset = 0; // set by {@} response bool clearPlot = false; // set by the {@} response code // plotting data variables int plot_1_varMin = 0; int plot_1_var = plot_1_varMin; float plot_1_scaling; float plot_1_varDisplayMin = 0.0; int plot_2_varMin = 0; int plot_2_var = plot_2_varMin; float plot_2_scaling; float plot_2_varDisplayMin = 0.0; int plot_3_varMin = 0; int plot_3_var = plot_3_varMin; float plot_3_scaling; float plot_3_varDisplayMin = 0.0; pfodDelay plotDataTimer; // plot data timer unsigned long PLOT_DATA_INTERVAL = 1000;// ms == 1 sec, edit this to change the plot data interval // the setup routine runs once on reset: void setup() { Serial.begin(9600); for (int i=3; i>0; i--) { // wait a few secs to see if we are being programmed delay(1000); } parser.connect(&Serial); // connect the parser to the i/o stream // calculate the plot vars scaling here once to reduce computation plot_1_scaling = getPlotVarScaling(1023,plot_1_varMin,1023.0,plot_1_varDisplayMin); plot_2_scaling = getPlotVarScaling(1023,plot_2_varMin,1023.0,plot_2_varDisplayMin); plot_3_scaling = getPlotVarScaling(1023,plot_3_varMin,1023.0,plot_3_varDisplayMin); plotDataTimer.start(PLOT_DATA_INTERVAL); // start plot timer // <<<<<<<<< Your extra setup code goes here } // the loop routine runs over and over again forever: void loop() { uint8_t cmd = parser.parse(); // parse incoming data from connection // parser returns non-zero when a pfod command is fully parsed if (cmd != 0) { // have parsed a complete msg { to } uint8_t* pfodFirstArg = parser.getFirstArg(); // may point to \0 if no arguments in this msg. pfod_MAYBE_UNUSED(pfodFirstArg); // may not be used, just suppress warning long pfodLongRtn; // used for parsing long return arguments, if any pfod_MAYBE_UNUSED(pfodLongRtn); // may not be used, just suppress warning if ('.' == cmd) { // pfodApp has connected and sent {.} , it is asking for the main menu if (!parser.isRefresh()) { sendMainMenu(); // send back the menu designed } else { sendMainMenuUpdate(); // menu is cached just send update } // handle {@} request } else if('@'==cmd) { // pfodApp requested 'current' time plot_msOffset = millis(); // capture current millis as offset rawdata timestamps clearPlot = true; // clear plot on reconnect as have new plot_msOffset parser.print(F("{@`0}")); // return `0 as 'current' raw data milliseconds // now handle commands returned from button/sliders } else if('A'==cmd) { // user pressed -- 'Run' // in the main Menu of Menu_4 // << add your action code here for this button parser.print(F("{}")); // change this return as needed. // always send back a pfod msg otherwise pfodApp will disconnect. } else if('B'==cmd) { // user pressed -- 'Stop' // in the main Menu of Menu_4 // << add your action code here for this button parser.print(F("{}")); // change this return as needed. // always send back a pfod msg otherwise pfodApp will disconnect. } else if('C'==cmd) { // user pressed -- 'Home' // in the main Menu of Menu_4 // << add your action code here for this button parser.print(F("{}")); // change this return as needed. // always send back a pfod msg otherwise pfodApp will disconnect. // } else if('D'==cmd) { // this is a label. pfodApp NEVER sends this cmd -- '' // // in the main Menu of Menu_4 } else if('E'==cmd) { // user pressed -- 'Stepper Plots' // in the main Menu of Menu_4 // return plotting msg. parser.print(F("{=Stepper Plots~mm:ss")); if (clearPlot) { clearPlot = false; parser.print(F("~C")); } parser.print(F("|time (mm:ss)|Position~~~|Speed~~~steps/s|Max loop()us ~~~max loop()us}")); } else if ('!' == cmd) { // CloseConnection command closeConnection(parser.getPfodAppStream()); } else { // unknown command parser.print(F("{}")); // always send back a pfod msg otherwise pfodApp will disconnect. } } sendData(); // <<<<<<<<<<< Your other loop() code goes here } void closeConnection(Stream *io) { // add any special code here to force connection to be dropped } void sendData() { if (plotDataTimer.justFinished()) { plotDataTimer.repeat(); // restart plot data timer, without drift // assign values to plot variables from your loop variables or read ADC inputs plot_1_var = plot_1_varMin; //<<< replace this Min value with your actual data plot_2_var = plot_2_varMin; //<<< replace this Min value with your actual data plot_3_var = plot_3_varMin; //<<< replace this Min value with your actual data // send plot data in CSV format parser.print(millis()-plot_msOffset);// time in milliseconds parser.print(','); parser.print(((float)(plot_1_var-plot_1_varMin)) * plot_1_scaling + plot_1_varDisplayMin); parser.print(','); parser.print(((float)(plot_2_var-plot_2_varMin)) * plot_2_scaling + plot_2_varDisplayMin); parser.print(','); parser.print(((float)(plot_3_var-plot_3_varMin)) * plot_3_scaling + plot_3_varDisplayMin); parser.println(); // end of CSV data record } } float getPlotVarScaling(long varMax, long varMin, float displayMax, float displayMin) { long varRange = varMax - varMin; if (varRange == 0) { varRange = 1; } // prevent divide by zero return (displayMax - displayMin)/((float)varRange); } void sendMainMenu() { // !! Remember to change the parser version string // every time you edit this method parser.print(F("{,")); // start a Menu screen pfod message // send menu background, format, prompt, refresh and version parser.print(F("<+4>~Stepper Control`0")); parser.sendVersion(); // send the menu version // send menu items parser.print(F("|A<+6>")); parser.print(F("~Run")); parser.print(F("|B<+6>")); parser.print(F("~Stop")); parser.print(F("|C<+5>")); parser.print(F("~Home")); parser.print(F("|!D<+3>")); parser.print(F("~ ")); parser.print(F("|E<+3>")); parser.print(F("~Stepper Plots")); parser.print(F("}")); // close pfod message } void sendMainMenuUpdate() { parser.print(F("{;")); // start an Update Menu pfod message // send menu items parser.print(F("|A")); parser.print(F("|B")); parser.print(F("|C")); parser.print(F("|!D")); parser.print(F("|E")); parser.print(F("}")); // close pfod message // ============ end of menu =========== } int swap01(int in) { return (in==0)?1:0; } // ============= end generated code =========