running parts of sketch exclusively

Hi,

I’m new to programming and arduino and i am trying to work out how to run a glcd when serial data is not transferring and then when serial data is being sent and recieved i want it to speed up and just connect to serial exclusively for datalogging.

The program sort of works but it is a slower connection than if the glcd part is removed.

I’m sure it is a fairly simple fix but i just need some guidance.

Code below:

/*
  The GLCD circuit:
  See the inlcuded documentation in glcd/doc directory for how to wire
  up the glcd module. glcd/doc/GLCDref.htm can be viewed in your browser
  by clicking on the file.
 
 */

// include the library header
#include <glcd.h>

// include the Fonts
#include <fonts/allFonts.h>

/* Arduino Mega - DLP emulation */
const int DLP_CMD_DIGITAL_IN_CH1 = 0x41;
const int DLP_CMD_DIGITAL_IN_CH2 = 0x53;    
const int DLP_CMD_DIGITAL_IN_CH3 = 0x44;    
const int DLP_CMD_DIGITAL_IN_CH4 = 0x46;    
const int DLP_CMD_DIGITAL_IN_CH5 = 0x47;    
const int DLP_CMD_DIGITAL_IN_CH6 = 0x4E;    
const int DLP_CMD_DIGITAL_IN_CH7 = 0x4A;    
const int DLP_CMD_DIGITAL_IN_CH8 = 0x4B;

const int DLP_CMD_ANALOG_IN_CH1 = 0x5A;
const int DLP_CMD_ANALOG_IN_CH2 = 0x58;
const int DLP_CMD_ANALOG_IN_CH3 = 0x43;
const int DLP_CMD_ANALOG_IN_CH4 = 0x56;
const int DLP_CMD_ANALOG_IN_CH5 = 0x42;
const int DLP_CMD_ANALOG_IN_CH6 = 0x4E;
const int DLP_CMD_ANALOG_IN_CH7 = 0x4D;
const int DLP_CMD_ANALOG_IN_CH8 = 0x2C;

const int DLP_CMD_TEMPERATURE_IN_CH1 = 0x39;
const int DLP_CMD_TEMPERATURE_IN_CH2 = 0x30;
const int DLP_CMD_TEMPERATURE_IN_CH3 = 0x2D;
const int DLP_CMD_TEMPERATURE_IN_CH4 = 0x3D;
const int DLP_CMD_TEMPERATURE_IN_CH5 = 0x4F;
const int DLP_CMD_TEMPERATURE_IN_CH6 = 0x50;
const int DLP_CMD_TEMPERATURE_IN_CH7 = 0x5B;
const int DLP_CMD_TEMPERATURE_IN_CH8 = 0x5D;

const int DLP_SETUP_FARENHEIT = 0x4C; // not used
const int DLP_SETUP_CELCIUS = 0x3B; // not used
const int DLP_SETUP_ASCII = 0x60;
const int DLP_SETUP_BINARY = 0x5C;

const int DLP_CMD_PING = 0x27;
const int DLP_RESP_PING = 0x51;

const int SET_MODE_ASCII = 0;
const int SET_MODE_BINARY = 1;

const float AD_MULTIPLIER = 5.0/1024.0;

void setup() {
  // Initialize the GLCD 
  GLCD.Init();

 // Select the font for the default text area
  GLCD.SelectFont(Arial_bold_14);

  //GLCD.print("S13 Silvia");
  
  //DLP section
  Serial.begin(115200);
  pinMode(A0,INPUT);
  pinMode(A1,INPUT);
  pinMode(A2,INPUT);
  pinMode(A3,INPUT);
  pinMode(A4,INPUT);
  pinMode(A5,INPUT);
  pinMode(A6,INPUT);
  pinMode(A7,INPUT);
}
//dlp parts
int rxByte = 0;
int currMode = SET_MODE_ASCII;
int sensorValue = 0;

void loop() {
  if (Serial.available()>0){
    nistune();
  }
  else{
    bradDisplay();
  }
}

int bradDisplay(){
    // set the cursor to column 0, line 2
    // (note: line 2 is the third row, since counting begins with 0):
    GLCD.CursorTo(0, 0);
    GLCD.print("AFR ");
    GLCD.print(analogRead(A0)*5000/1024);
    GLCD.print("mV   ");
  
    GLCD.CursorTo(0, 1);
    GLCD.print("Boost ");
    GLCD.print(analogRead(A1)*5000/1024);
    GLCD.print("mV   ");
  
    GLCD.CursorTo(0, 2);
    GLCD.print("Fuel P ");
    GLCD.print(analogRead(A2)*5000/1024);
    GLCD.print("mV   ");

    GLCD.CursorTo(0, 3);
    GLCD.print("A3 ");
    GLCD.print(analogRead(A3)*5000/1024);
    GLCD.print("mV   ");

    GLCD.CursorTo(9, 0);
    GLCD.print("X ");
    GLCD.print(analogRead(A4)*0.006103515625-2.0625);
    GLCD.print("g's   ");
  
    GLCD.CursorTo(9, 1);
    GLCD.print("Y ");
    GLCD.print(analogRead(A5)*0.006103515625-2.0625);
    GLCD.print("g's   ");

    GLCD.CursorTo(9, 2);
    GLCD.print("Z ");
    GLCD.print(analogRead(A6)*0.006103515625-2.0625);
    GLCD.print("g's   ");
  
    //A7 optional for display - Nistune can only read 7 inputs
    GLCD.CursorTo(9, 3);
    GLCD.print("A7 ");
    GLCD.print(analogRead(A7)*5000/1024);
    GLCD.print("mV   ");
  
     if (Serial.available() > 0){
       GLCD.CursorTo(0, 7);
       GLCD.SelectFont(System5x7);
       GLCD.print("NISTUNE ");
     }
     else{
       GLCD.SelectFont(Arial_bold_14);
       delay(100);
     }
  }
  
int nistune(){
  //dlp loop
    if (Serial.available() > 0)
  {
     rxByte = Serial.read();
     int sendADVal = false;
     int sendTempVal = false;
     
     switch (rxByte)
     {
       case DLP_SETUP_ASCII:
         currMode = SET_MODE_ASCII;
         break;
       case DLP_SETUP_BINARY:
         currMode = SET_MODE_BINARY;
         break;
       case DLP_SETUP_FARENHEIT:
         break; // not used
       case DLP_SETUP_CELCIUS:
         break; // not used
       case DLP_CMD_PING:
         // ping response
         Serial.write(DLP_RESP_PING);
         break;
       case DLP_CMD_ANALOG_IN_CH1:
         sensorValue = analogRead(A0);
         sendADVal = true;
         break;
       case DLP_CMD_ANALOG_IN_CH2:
         sensorValue = analogRead(A1);
         sendADVal = true;
         break;
       case DLP_CMD_ANALOG_IN_CH3:
         sensorValue = analogRead(A2);
         sendADVal = true;
         break;
       case DLP_CMD_ANALOG_IN_CH4:
         sensorValue = analogRead(A3);
         sendADVal = true;
         break;
       case DLP_CMD_ANALOG_IN_CH5:
         sensorValue = analogRead(A4);
         sendADVal = true;
         break;
       case DLP_CMD_ANALOG_IN_CH6:
         sensorValue = analogRead(A5);
         sendADVal = true;
         break;
       case DLP_CMD_ANALOG_IN_CH7:
         sensorValue = analogRead(A6);
         sendADVal = true;
         break;
       case DLP_CMD_ANALOG_IN_CH8:
         sensorValue = 0; // not available
         sendADVal = true;
         break;
       case DLP_CMD_TEMPERATURE_IN_CH1:
       case DLP_CMD_TEMPERATURE_IN_CH2:
       case DLP_CMD_TEMPERATURE_IN_CH3:
       case DLP_CMD_TEMPERATURE_IN_CH4:       
       case DLP_CMD_TEMPERATURE_IN_CH5:       
       case DLP_CMD_TEMPERATURE_IN_CH6:       
       case DLP_CMD_TEMPERATURE_IN_CH7:       
       case DLP_CMD_TEMPERATURE_IN_CH8:       
         sensorValue = 0; // not available
         sendTempVal = true;       
         break;       
       default:
         break;
     }
     
     if (sendADVal || sendTempVal)
     {
        if (currMode == SET_MODE_BINARY)
        {
           Serial.write((char)(sensorValue >> 8));
           Serial.write((char)(sensorValue));
        }
        else
        {
           Serial.print((float)sensorValue * AD_MULTIPLIER,2);
           Serial.println("V");
        }
     }
  }
}

Serial data arrives very slowly compared to how fast the Arduino can process it so I suspect that the display part of your program is running, perhaps many times, between bytes being available to Serial. As the display part of the program is itself quite slow then it will appear to slow down the receipt and action on serial data.

Code such as

GLCD.print(analogRead(A4)*0.006103515625-2.0625);

of which there are several examples in your program will slow it down considerably. Do you really need all that (apparent) accuracy ?

You could probably improve things with a couple of complementary changes.

If possible wait until you have several (all?) characters in the Serial buffer before calling nistune() - that allows the data input to happen in the background.

Only call bradDisplay() at specific time intervals - as infrequently as possible. Use millis() and the technique in the Blink Without Delay example sketch to manage the timing.

...R

Would there be a way i could run the nistune() function independently when serial is connected or data flowing then when there is no serial connection then run the bradDisplay() function?

Would there be a way i could run the nistune() function independently when serial is connected or data flowing then when there is no serial connection then run the bradDisplay() function?

I don’t understand the question. If there is no serial data coming in, the nistune() will never be called. Once nistune() has done its thing, the current sketch runs bradDisplay(). All of this happens very, very quickly.

What problem are you trying to solve?

PaulS:

Would there be a way i could run the nistune() function independently when serial is connected or data flowing then when there is no serial connection then run the bradDisplay() function?

I don't understand the question. If there is no serial data coming in, the nistune() will never be called. Once nistune() has done its thing, the current sketch runs bradDisplay(). All of this happens very, very quickly.

What problem are you trying to solve?

At the moment it works and when serial is connected nistune() is called but bradDisplay() still runs when serial data is transferring which is slowing the whole thing down.

This is used for datalogging and it needs to run full speed. When i comment out the display section it runs at the speed im after.

So the problem im trying to solve is running nistune() exclusively.

I am using mega if it makes a difference??

At the moment it works and when serial is connected nistune() is called but bradDisplay() still runs when serial data is transferring which is slowing the whole thing down.

That’s because, in nistune(), you only process one character, regardless of how many have arrived.

Change the if statement to a while statement, and process all serial data that has arrived.

See if that fixes the problem before trying more complicated solutions.

So i have used the suggestions and implemented the technique in blink without delay example and it is working faster than it was. As suggested it turns out between serial read/writes there was cycles of the bradDisplay() running. Here is my latest iteration of the sketch.

Is there any way to detect usb connection and use that as a switch for my functions?

/*
  The GLCD circuit:
  See the inlcuded documentation in glcd/doc directory for how to wire
  up the glcd module. glcd/doc/GLCDref.htm can be viewed in your browser
  by clicking on the file.
 
 */

// include the library header
#include <glcd.h>

// include the Fonts
#include <fonts/allFonts.h>

/* Arduino Mega - DLP emulation */
const int DLP_CMD_DIGITAL_IN_CH1 = 0x41;
const int DLP_CMD_DIGITAL_IN_CH2 = 0x53;    
const int DLP_CMD_DIGITAL_IN_CH3 = 0x44;    
const int DLP_CMD_DIGITAL_IN_CH4 = 0x46;    
const int DLP_CMD_DIGITAL_IN_CH5 = 0x47;    
const int DLP_CMD_DIGITAL_IN_CH6 = 0x4E;    
const int DLP_CMD_DIGITAL_IN_CH7 = 0x4A;    
const int DLP_CMD_DIGITAL_IN_CH8 = 0x4B;

const int DLP_CMD_ANALOG_IN_CH1 = 0x5A;
const int DLP_CMD_ANALOG_IN_CH2 = 0x58;
const int DLP_CMD_ANALOG_IN_CH3 = 0x43;
const int DLP_CMD_ANALOG_IN_CH4 = 0x56;
const int DLP_CMD_ANALOG_IN_CH5 = 0x42;
const int DLP_CMD_ANALOG_IN_CH6 = 0x4E;
const int DLP_CMD_ANALOG_IN_CH7 = 0x4D;
const int DLP_CMD_ANALOG_IN_CH8 = 0x2C;

const int DLP_CMD_TEMPERATURE_IN_CH1 = 0x39;
const int DLP_CMD_TEMPERATURE_IN_CH2 = 0x30;
const int DLP_CMD_TEMPERATURE_IN_CH3 = 0x2D;
const int DLP_CMD_TEMPERATURE_IN_CH4 = 0x3D;
const int DLP_CMD_TEMPERATURE_IN_CH5 = 0x4F;
const int DLP_CMD_TEMPERATURE_IN_CH6 = 0x50;
const int DLP_CMD_TEMPERATURE_IN_CH7 = 0x5B;
const int DLP_CMD_TEMPERATURE_IN_CH8 = 0x5D;

const int DLP_SETUP_FARENHEIT = 0x4C; // not used
const int DLP_SETUP_CELCIUS = 0x3B; // not used
const int DLP_SETUP_ASCII = 0x60;
const int DLP_SETUP_BINARY = 0x5C;

const int DLP_CMD_PING = 0x27;
const int DLP_RESP_PING = 0x51;

const int SET_MODE_ASCII = 0;
const int SET_MODE_BINARY = 1;

const float AD_MULTIPLIER = 5.0/1024.0;

int serialState = LOW;
long previousMillis = 0;
long interval = 250;      //interval at which to run display

void setup() {
  // Initialize the GLCD 
  GLCD.Init();

 // Select the font for the default text area
  GLCD.SelectFont(Arial_bold_14);

  //GLCD.print("S13 Silvia");
  
  //DLP section
  Serial.begin(115200);
  pinMode(A0,INPUT);
  pinMode(A1,INPUT);
  pinMode(A2,INPUT);
  pinMode(A3,INPUT);
  pinMode(A4,INPUT);
  pinMode(A5,INPUT);
  pinMode(A6,INPUT);
  pinMode(A7,INPUT);
}
//dlp parts
int rxByte = 0;
int currMode = SET_MODE_ASCII;
int sensorValue = 0;

void loop() {
  nistune();
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval){
    previousMillis = currentMillis;
    bradDisplay();
  }
}
  
int nistune(){
  //dlp loop
    if (Serial.available() > 0)
  {
     rxByte = Serial.read();
     int sendADVal = false;
     int sendTempVal = false;
     
     switch (rxByte)
     {
       case DLP_SETUP_ASCII:
         currMode = SET_MODE_ASCII;
         break;
       case DLP_SETUP_BINARY:
         currMode = SET_MODE_BINARY;
         break;
       case DLP_SETUP_FARENHEIT:
         break; // not used
       case DLP_SETUP_CELCIUS:
         break; // not used
       case DLP_CMD_PING:
         // ping response
         Serial.write(DLP_RESP_PING);
         break;
       case DLP_CMD_ANALOG_IN_CH1:
         sensorValue = analogRead(A0);
         sendADVal = true;
         break;
       case DLP_CMD_ANALOG_IN_CH2:
         sensorValue = analogRead(A1);
         sendADVal = true;
         break;
       case DLP_CMD_ANALOG_IN_CH3:
         sensorValue = analogRead(A2);
         sendADVal = true;
         break;
       case DLP_CMD_ANALOG_IN_CH4:
         sensorValue = analogRead(A3);
         sendADVal = true;
         break;
       case DLP_CMD_ANALOG_IN_CH5:
         sensorValue = analogRead(A4);
         sendADVal = true;
         break;
       case DLP_CMD_ANALOG_IN_CH6:
         sensorValue = analogRead(A5);
         sendADVal = true;
         break;
       case DLP_CMD_ANALOG_IN_CH7:
         sensorValue = analogRead(A6);
         sendADVal = true;
         break;
       case DLP_CMD_ANALOG_IN_CH8:
         sensorValue = 0; // not available
         sendADVal = true;
         break;
       case DLP_CMD_TEMPERATURE_IN_CH1:
       case DLP_CMD_TEMPERATURE_IN_CH2:
       case DLP_CMD_TEMPERATURE_IN_CH3:
       case DLP_CMD_TEMPERATURE_IN_CH4:       
       case DLP_CMD_TEMPERATURE_IN_CH5:       
       case DLP_CMD_TEMPERATURE_IN_CH6:       
       case DLP_CMD_TEMPERATURE_IN_CH7:       
       case DLP_CMD_TEMPERATURE_IN_CH8:       
         sensorValue = 0; // not available
         sendTempVal = true;       
         break;       
       default:
         break;
     }
     
     if (sendADVal || sendTempVal)
     {
        if (currMode == SET_MODE_BINARY)
        {
           Serial.write((char)(sensorValue >> 8));
           Serial.write((char)(sensorValue));
        }
        else
        {
           Serial.print((float)sensorValue * AD_MULTIPLIER,2);
           Serial.println("V");
        }
     }
  }
}

int bradDisplay(){
    // set the cursor to column 0, line 2
    // (note: line 2 is the third row, since counting begins with 0):
    GLCD.CursorTo(0, 0);
    GLCD.print("AFR ");
    GLCD.print(analogRead(A0)*AD_MULTIPLIER);
    GLCD.print("V   ");
  
    GLCD.CursorTo(0, 1);
    GLCD.print("Boost ");
    GLCD.print(analogRead(A1)*AD_MULTIPLIER);
    GLCD.print("V   ");
  
    GLCD.CursorTo(0, 2);
    GLCD.print("Fuel P ");
    GLCD.print(analogRead(A2)*AD_MULTIPLIER);
    GLCD.print("V   ");

    GLCD.CursorTo(0, 3);
    GLCD.print("A3 ");
    GLCD.print(analogRead(A3)*AD_MULTIPLIER);
    GLCD.print("V   ");

    GLCD.CursorTo(9, 0);
    GLCD.print("X ");
    GLCD.print(analogRead(A4)*0.006-2.063);
    GLCD.print("g   ");
  
    GLCD.CursorTo(9, 1);
    GLCD.print("Y ");
    GLCD.print(analogRead(A5)*0.006-2.063);
    GLCD.print("g   ");

    GLCD.CursorTo(9, 2);
    GLCD.print("Z ");
    GLCD.print(analogRead(A6)*0.006-2.063);
    GLCD.print("g   ");
  
    //A7 optional for display - Nistune can only read 7 inputs
    GLCD.CursorTo(9, 3);
    GLCD.print("A7 ");
    GLCD.print(analogRead(A7)*AD_MULTIPLIER);
    GLCD.print("V   ");
  
     /*if (Serial.available() > 0){
       GLCD.CursorTo(0, 7);
       GLCD.SelectFont(System5x7);
       GLCD.print("NISTUNE ");
     }
     else{
       GLCD.SelectFont(Arial_bold_14);
       delay(100);
     }*/
  }

looseendsdrifters:
At the moment it works and when serial is connected nistune() is called but bradDisplay() still runs when serial data is transferring which is slowing the whole thing down.

Perhaps you missed Reply #2 ?

...R

Perhaps you missed Reply #2 ?

That's not the only reply OP missed/failed to read.

OP, you are STILL reading one character per call to nistune()? Why are you NOT paying attention?

Is there any way to detect usb connection and use that as a switch for my functions?

There is not need to, yet.