Threading or Background processing.

I have a tft display and I need to created an box that does not stop the rest of the code from running while it flashes.

I need the process to run when a digital pin goes high. ad run until that pin goes low. (Change States). I can get all of this to work except when I start the Flashing box my code stops until the flashing process is done.

The digital read is not in the following code yet because I am not using it yet. I am testing my code.

The timed action library gave me the same result as it not being there. The RightTurn and LeftTurn procedures do not run in the background. They stay in the foreground.

I am not sure how to set up background processes in arduino. most of my experience is with VB. Any help would be appreciated.

#include <TimedAction.h>
#include <Event.h>
TimedAction LeftTurnAction = TimedAction(50,LeftTurn);
TimedAction RightTurnAction= TimedAction(50, RightTurn);
   
  int LCDRestart = 0 ;
  int FIn = 8 ; //47ohm Resistor to +5vdc
  int BIn = 9 ; //4.7K to Ground and 47K to 12V
  int WIn = 10 ; //47ohm Resistor to +5vdc
  int OIn = 11 ;  //100ohm Resistor to +5vdc  
  
  int FVal = 0;  float FValSnapshot = 0 ; float FuelLevel = 0 ; float FuelLevelSnapshot = 0 ;  
  int BVal = 0;  float BVolts = 0 ;  float BVoltsSnapshot = 0 ;  
  int WVal = 0 ; int WTemp = 0 ; float WTempSnapshot = 0 ;   
  int OVal = 0 ;  int OilPressure = 0 ;  float OilPressureSnapshot = 0 ;  
  int SensorReadLoop = 30;  int LoopCount = 30 ;

  #include <UTFT.h>
  UTFT    myGLCD(GEEE32,38,39,40,41);  
  extern uint8_t Inconsola[]; //24 x 32
  extern uint8_t OCR_A_Extended_M[]; //16 x 24
  
void setup(){
  myGLCD.InitLCD(LANDSCAPE);
  myGLCD.clrScr();
  myGLCD.setFont(OCR_A_Extended_M);
  myGLCD.fillScr(VGA_BLUE) ;
  myGLCD.setBackColor(VGA_BLUE);  
  myGLCD.setColor (VGA_YELLOW);//Red,Green,Blue
  myGLCD.print("Oil Pressure",0,40);
  myGLCD.print("Water Temperature",0,80);
  myGLCD.print("Fuel Level",0,120);
  myGLCD.print("Voltage",0,160);
}
void loop(){
  myGLCD.setColor (VGA_YELLOW) ;
  SensorReadings();
  LeftTurnAction.check();
  RightTurnAction.check();
}

void SensorReadings(){
  if (SensorReadLoop <= LoopCount){
    OilRead(); WaterRead(); FuelRead(); BatteryRead();     
    OilPressureSnapshot = OilPressureSnapshot + OilPressure;
    WTempSnapshot = WTempSnapshot + WTemp;
    FuelLevelSnapshot = FuelLevelSnapshot + FuelLevel;
    BVoltsSnapshot = BVoltsSnapshot + BVolts;    
    SensorReadLoop = SensorReadLoop - 1;
    myGLCD.print("  Update in     " + String(SensorReadLoop),RIGHT,0);
  } 
  if (SensorReadLoop == 0){
    myGLCD.print("                    ",0,0);
    OilPressure = OilPressureSnapshot/LoopCount;
    WTemp=WTempSnapshot/LoopCount;
    FuelLevel=FuelLevelSnapshot/LoopCount;
    BVolts=BVoltsSnapshot/LoopCount;    
    OilTFT(); delay(100); WaterTFT(); delay(100); FuelTFT(); delay(100); BatteryTFT() ;delay(100); 
    SensorReadLoop=LoopCount;    
    OilPressureSnapshot=0; WTempSnapshot=0; FuelLevelSnapshot=0; BVoltsSnapshot=0;
    myGLCD.print("                    ",0,0);
  } 
}
int CheckGauges(String(Gauge)){
      for (int WarnLoop=0; WarnLoop <= 2; WarnLoop++){
          myGLCD.setBackColor (VGA_RED); 
          myGLCD.print("                              ",CENTER,0);
          myGLCD.print("Check " + String(Gauge),CENTER,0);
          delay (200) ;
          myGLCD.setBackColor (VGA_BLUE);
   } 
}
void OilTFT(){ 
  if (OilPressure <=10){
    myGLCD.setBackColor (VGA_RED); 
    myGLCD.print(" Oil                    ",0,40);
    myGLCD.print(String(OilPressure),150,40);
    myGLCD.print("PSI",RIGHT,40);
    CheckGauges("Oil Pressure");
    }
  if (OilPressure >=11){
    myGLCD.print("Oil Pressure OK",CENTER,0);
    myGLCD.setBackColor(VGA_BLUE);
    myGLCD.setColor (VGA_YELLOW);//Red,Green,Blue
    myGLCD.print(" Oil                ",0,40);
    myGLCD.print(String(OilPressure),150,40);
    myGLCD.print("PSI",RIGHT,40);
    }
}
void WaterRead () {
  WVal = analogRead(WIn) ; // WInput
  WVal = map(WVal, 1, 1024, 1024, 1);//Invert Reading Scale 
  WTemp = (WVal /4.021053) ; 
  WTemp=250;
}
void WaterTFT(){
    if (WTemp <=220){
      myGLCD.setBackColor(VGA_BLUE);
      myGLCD.print("       Water Temp OK       ",CENTER,0);
      myGLCD.print(" Water            ",0,80);
      myGLCD.print(String(WTemp),150,80);
      myGLCD.print(" `F",RIGHT,80);
    }
    if (WTemp >=220){
      myGLCD.setBackColor (VGA_RED);//Red,Green,Blue
      myGLCD.print(" Water             ",0,80);
      myGLCD.print(String(WTemp),150,80);
      myGLCD.print(" `F",RIGHT,80); // Degree is ascii 0176
      CheckGauges("Water Temp");
    }
}
void FuelRead(void){
  FVal = analogRead(FIn); // FInput
  FVal = map(FVal, 1, 1024, 1024, 1);//Invert Reading Scale to compensate for 10 Ohm Full 80 Ohm Empty Scale.
  if (FVal < 388 ) { // Prevent Negative numbers from being displayed on the digital readout.
      FVal = 388 ;
  }
  FVal=1000;
}
void FuelTFT(){
  FuelLevel = (FVal /27.7)-14.0;
  if (FuelLevel <=1){
    myGLCD.setBackColor (VGA_RED);//Red,Green,Blue
    myGLCD.print(" Fuel                  ",0,120);
    myGLCD.printNumF((FuelLevel),2,150,120);
    myGLCD.print("GAL",RIGHT,120);
    CheckGauges("Fuel Level");
    }
  if (FuelLevel >=1.01){
    myGLCD.setBackColor(VGA_BLUE);
    myGLCD.print("          Fuel Level OK          ",CENTER,0);
    myGLCD.print(" Fuel                  ",0,120);
    myGLCD.printNumF((FuelLevel),2,150,120);
    myGLCD.print("GAL",RIGHT,120);
    }
}
void BatteryRead (void){   
  float BVOut = 0.00;
  float BR1 = 47000;
  float BR2 = 4700;
    BVal = analogRead(BIn);  //BInput 
    //BVal =1000;
    BVOut = ((BVal * 5.0) / 1024.0);
    BVolts = BVOut / (BR2/(BR1+ BR2)); 
   BVolts=13.8; 
  }
void BatteryTFT(){
  if (BVolts <=12.5){
    myGLCD.setBackColor (VGA_RED);//Red,Green,Blue
    myGLCD.print(" Battery               ",0,160);    
    myGLCD.printNumF((BVolts), 2, 150, 160);
    myGLCD.print("Volts",RIGHT,160);
    CheckGauges("Alternator");
    }
  if (BVolts >=12.5){
    myGLCD.setBackColor(VGA_BLUE);
    myGLCD.print("      Battery Voltage OK      ",CENTER,0);
    myGLCD.print(" Battery              ",0,160);
    myGLCD.printNumF((BVolts), 2, 150, 160);
    myGLCD.print("Volts",RIGHT,160);
    }
}
void LeftTurn(){
  myGLCD.setFont(Inconsola);
  myGLCD.setColor(VGA_LIME);
  myGLCD.fillRoundRect(5,190,135,235);
  myGLCD.setColor(VGA_BLUE);
  myGLCD.setBackColor(VGA_LIME);
  myGLCD.print("   < ",10,200);delay(10);
  myGLCD.print("  <  ",10,200);delay(10);
  myGLCD.print(" <   ",10,200);delay(100);
  myGLCD.setFont(OCR_A_Extended_M); 
  myGLCD.setColor(VGA_BLUE);
  myGLCD.setBackColor(VGA_BLUE);
  myGLCD.fillRoundRect(5,190,135,235);
  delay(1000);
}
void RightTurn(){
  myGLCD.setFont(Inconsola);
  myGLCD.setColor(VGA_LIME);
  myGLCD.fillRoundRect(175,190,319,235);//delay(10);
  myGLCD.setColor(VGA_BLUE);
  myGLCD.setBackColor(VGA_LIME);
  myGLCD.print(" >    ",RIGHT,200);delay(10);
  myGLCD.print("  >   ",RIGHT,200);delay(10);
  myGLCD.print("   >  ",RIGHT,200);delay(100);
  myGLCD.setFont(OCR_A_Extended_M); 
  myGLCD.setColor(VGA_BLUE);
  myGLCD.setBackColor(VGA_BLUE);
  myGLCD.fillRoundRect(175,190,319,235);
  delay(1000);
}

yeah, you have to get rid of those blocking delay()s...

have you studied the BlinkWithoutDelay sketch included in the IDE examples?

Arduino does not support either multithreaded programming or background processing.

You have to use cooperative multitasking. To do that, each time through your loop, you check for things you need to do and respond to them. You should never use delay in that case, since it blocks.

Instead, follow the model in blinkWithoutDelay, where you check to see if enough time as passed, and if it has, you do something, and if not, you skip that block of code.

It can get tricky if you're managing a bunch of actions that are supposed to happen at different intervals. In that case I would suggest setting up a table system where you have an array of action times and function pointers, and possibly blocks of state information. When the millis() time is >= a time from the array, you'd call the function in the function pointer, optionally passing in the saved block of data for that time trigger.

kilgorq:
I have a tft display and I need to created an box that does not stop the rest of the code from running while it flashes.

I need the process to run when a digital pin goes high. ad run until that pin goes low. (Change States). I can get all of this to work except when I start the Flashing box my code stops until the flashing process is done.

The digital read is not in the following code yet because I am not using it yet. I am testing my code.

The timed action library gave me the same result as it not being there. The RightTurn and LeftTurn procedures do not run in the background. They stay in the foreground.

I am not sure how to set up background processes in arduino. most of my experience is with VB. Any help would be appreciated.

#include <TimedAction.h>

#include <Event.h>
TimedAction LeftTurnAction = TimedAction(50,LeftTurn);
TimedAction RightTurnAction= TimedAction(50, RightTurn);
 
 int LCDRestart = 0 ;
 int FIn = 8 ; //47ohm Resistor to +5vdc
 int BIn = 9 ; //4.7K to Ground and 47K to 12V
 int WIn = 10 ; //47ohm Resistor to +5vdc
 int OIn = 11 ;  //100ohm Resistor to +5vdc  
 
 int FVal = 0;  float FValSnapshot = 0 ; float FuelLevel = 0 ; float FuelLevelSnapshot = 0 ;  
 int BVal = 0;  float BVolts = 0 ;  float BVoltsSnapshot = 0 ;  
 int WVal = 0 ; int WTemp = 0 ; float WTempSnapshot = 0 ;  
 int OVal = 0 ;  int OilPressure = 0 ;  float OilPressureSnapshot = 0 ;  
 int SensorReadLoop = 30;  int LoopCount = 30 ;

#include <UTFT.h>
 UTFT    myGLCD(GEEE32,38,39,40,41);  
 extern uint8_t Inconsola[]; //24 x 32
 extern uint8_t OCR_A_Extended_M[]; //16 x 24
 
void setup(){
 myGLCD.InitLCD(LANDSCAPE);
 myGLCD.clrScr();
 myGLCD.setFont(OCR_A_Extended_M);
 myGLCD.fillScr(VGA_BLUE) ;
 myGLCD.setBackColor(VGA_BLUE);  
 myGLCD.setColor (VGA_YELLOW);//Red,Green,Blue
 myGLCD.print("Oil Pressure",0,40);
 myGLCD.print("Water Temperature",0,80);
 myGLCD.print("Fuel Level",0,120);
 myGLCD.print("Voltage",0,160);
}
void loop(){
 myGLCD.setColor (VGA_YELLOW) ;
 SensorReadings();
 LeftTurnAction.check();
 RightTurnAction.check();
}

void SensorReadings(){
 if (SensorReadLoop <= LoopCount){
   OilRead(); WaterRead(); FuelRead(); BatteryRead();     
   OilPressureSnapshot = OilPressureSnapshot + OilPressure;
   WTempSnapshot = WTempSnapshot + WTemp;
   FuelLevelSnapshot = FuelLevelSnapshot + FuelLevel;
   BVoltsSnapshot = BVoltsSnapshot + BVolts;    
   SensorReadLoop = SensorReadLoop - 1;
   myGLCD.print("  Update in     " + String(SensorReadLoop),RIGHT,0);
 }
 if (SensorReadLoop == 0){
   myGLCD.print("                    ",0,0);
   OilPressure = OilPressureSnapshot/LoopCount;
   WTemp=WTempSnapshot/LoopCount;
   FuelLevel=FuelLevelSnapshot/LoopCount;
   BVolts=BVoltsSnapshot/LoopCount;    
   OilTFT(); delay(100); WaterTFT(); delay(100); FuelTFT(); delay(100); BatteryTFT() ;delay(100);
   SensorReadLoop=LoopCount;    
   OilPressureSnapshot=0; WTempSnapshot=0; FuelLevelSnapshot=0; BVoltsSnapshot=0;
   myGLCD.print("                    ",0,0);
 }
}
int CheckGauges(String(Gauge)){
     for (int WarnLoop=0; WarnLoop <= 2; WarnLoop++){
         myGLCD.setBackColor (VGA_RED);
         myGLCD.print("                              ",CENTER,0);
         myGLCD.print("Check " + String(Gauge),CENTER,0);
         delay (200) ;
         myGLCD.setBackColor (VGA_BLUE);
  }
}
void OilTFT(){
 if (OilPressure <=10){
   myGLCD.setBackColor (VGA_RED);
   myGLCD.print(" Oil                    ",0,40);
   myGLCD.print(String(OilPressure),150,40);
   myGLCD.print("PSI",RIGHT,40);
   CheckGauges("Oil Pressure");
   }
 if (OilPressure >=11){
   myGLCD.print("Oil Pressure OK",CENTER,0);
   myGLCD.setBackColor(VGA_BLUE);
   myGLCD.setColor (VGA_YELLOW);//Red,Green,Blue
   myGLCD.print(" Oil                ",0,40);
   myGLCD.print(String(OilPressure),150,40);
   myGLCD.print("PSI",RIGHT,40);
   }
}
void WaterRead () {
 WVal = analogRead(WIn) ; // WInput
 WVal = map(WVal, 1, 1024, 1024, 1);//Invert Reading Scale
 WTemp = (WVal /4.021053) ;
 WTemp=250;
}
void WaterTFT(){
   if (WTemp <=220){
     myGLCD.setBackColor(VGA_BLUE);
     myGLCD.print("       Water Temp OK       ",CENTER,0);
     myGLCD.print(" Water            ",0,80);
     myGLCD.print(String(WTemp),150,80);
     myGLCD.print(" F",RIGHT,80);    }    if (WTemp >=220){      myGLCD.setBackColor (VGA_RED);//Red,Green,Blue      myGLCD.print(" Water             ",0,80);      myGLCD.print(String(WTemp),150,80);      myGLCD.print(" F",RIGHT,80); // Degree is ascii 0176
     CheckGauges("Water Temp");
   }
}
void FuelRead(void){
 FVal = analogRead(FIn); // FInput
 FVal = map(FVal, 1, 1024, 1024, 1);//Invert Reading Scale to compensate for 10 Ohm Full 80 Ohm Empty Scale.
 if (FVal < 388 ) { // Prevent Negative numbers from being displayed on the digital readout.
     FVal = 388 ;
 }
 FVal=1000;
}
void FuelTFT(){
 FuelLevel = (FVal /27.7)-14.0;
 if (FuelLevel <=1){
   myGLCD.setBackColor (VGA_RED);//Red,Green,Blue
   myGLCD.print(" Fuel                  ",0,120);
   myGLCD.printNumF((FuelLevel),2,150,120);
   myGLCD.print("GAL",RIGHT,120);
   CheckGauges("Fuel Level");
   }
 if (FuelLevel >=1.01){
   myGLCD.setBackColor(VGA_BLUE);
   myGLCD.print("          Fuel Level OK          ",CENTER,0);
   myGLCD.print(" Fuel                  ",0,120);
   myGLCD.printNumF((FuelLevel),2,150,120);
   myGLCD.print("GAL",RIGHT,120);
   }
}
void BatteryRead (void){  
 float BVOut = 0.00;
 float BR1 = 47000;
 float BR2 = 4700;
   BVal = analogRead(BIn);  //BInput
   //BVal =1000;
   BVOut = ((BVal * 5.0) / 1024.0);
   BVolts = BVOut / (BR2/(BR1+ BR2));
  BVolts=13.8;
 }
void BatteryTFT(){
 if (BVolts <=12.5){
   myGLCD.setBackColor (VGA_RED);//Red,Green,Blue
   myGLCD.print(" Battery               ",0,160);    
   myGLCD.printNumF((BVolts), 2, 150, 160);
   myGLCD.print("Volts",RIGHT,160);
   CheckGauges("Alternator");
   }
 if (BVolts >=12.5){
   myGLCD.setBackColor(VGA_BLUE);
   myGLCD.print("      Battery Voltage OK      ",CENTER,0);
   myGLCD.print(" Battery              ",0,160);
   myGLCD.printNumF((BVolts), 2, 150, 160);
   myGLCD.print("Volts",RIGHT,160);
   }
}
void LeftTurn(){
 myGLCD.setFont(Inconsola);
 myGLCD.setColor(VGA_LIME);
 myGLCD.fillRoundRect(5,190,135,235);
 myGLCD.setColor(VGA_BLUE);
 myGLCD.setBackColor(VGA_LIME);
 myGLCD.print("   < ",10,200);delay(10);
 myGLCD.print("  <  ",10,200);delay(10);
 myGLCD.print(" <   ",10,200);delay(100);
 myGLCD.setFont(OCR_A_Extended_M);
 myGLCD.setColor(VGA_BLUE);
 myGLCD.setBackColor(VGA_BLUE);
 myGLCD.fillRoundRect(5,190,135,235);
 delay(1000);
}
void RightTurn(){
 myGLCD.setFont(Inconsola);
 myGLCD.setColor(VGA_LIME);
 myGLCD.fillRoundRect(175,190,319,235);//delay(10);
 myGLCD.setColor(VGA_BLUE);
 myGLCD.setBackColor(VGA_LIME);
 myGLCD.print(" >    ",RIGHT,200);delay(10);
 myGLCD.print("  >   ",RIGHT,200);delay(10);
 myGLCD.print("   >  ",RIGHT,200);delay(100);
 myGLCD.setFont(OCR_A_Extended_M);
 myGLCD.setColor(VGA_BLUE);
 myGLCD.setBackColor(VGA_BLUE);
 myGLCD.fillRoundRect(175,190,319,235);
 delay(1000);
}

This extended demo of BWoD several things at a time may help.

...R