Need help with millis instead of delay

Inn my code in the second if statement i have a delay of 2000 milliseconds. I do not want to delay my whole program so i have been trying to figure out a way to use millis but with no luck. What i want to happen is if ebswitchvalue == low and tps_average<=85 start a timer. while the timer is counting i still want the code to be reading the inputs and if the ebswitchvalue goes high or the tps_average value goes above 85 stop the timer so i do not turn on the relay and start outputing my pid pwm signal.

here is my code. Thanks

#include <PID_v1.h>
#include <PWM.h>
#include <SPI.h>
#include <LiquidCrystal.h>

int32_t frequency = 167; //frequency (in Hz)

const int numReadings= 3;

//exhaust pressure sensor
int ebp_readings[numReadings];      
int ebp_index = 0;                  
float ebp_total = 0;                  
float ebp_average = 0; 
const int ebp = A0;

//throttle position sensor
int tps_readings[numReadings];      
int tps_index = 0;                  
float tps_total = 0;                  
float tps_average = 0; 
const int tps = A1;

//vehicle speed sensor
int vss_readings[numReadings];      
int vss_index = 0;                  
float vss_total = 0;                  
float vss_average = 0; 
const int vss = A2;

// exhaust brake switch that enables or disabled exhaust brake
const int ebswitch = 12;
int ebswitchvalue = 0;

//Relay
int relay = 11;

//PWM pin
int pwmOut = 9;

//pid information
double Setpoint, Input, pidOutput;
PID myPID(&Input, &pidOutput, &Setpoint,1,0,0, DIRECT);

 long start_time;
 long current_time;
 long eb_delay = 2000;

// initialize lcd
LiquidCrystal lcd (8, 7, 6, 5, 4, 3);

void setup() {
  pinMode(ebswitch, INPUT);
  digitalWrite(ebswitch, HIGH);
  pinMode(relay, OUTPUT);
  pinMode(pwmOut, OUTPUT);
  
  //exhaust pressure sensor
  for (int ebp_thisReading = 0; ebp_thisReading < numReadings; ebp_thisReading++)
    ebp_readings[ebp_thisReading] = 0;

    //throttle position sensor
  for (int tps_thisReading = 0; tps_thisReading < numReadings; tps_thisReading++)
    tps_readings[tps_thisReading] = 0;  
 
  //vehicle speed sensor
  for (int vss_thisReading = 0; vss_thisReading < numReadings; vss_thisReading++)
    vss_readings[vss_thisReading] = 0;    

  //initialize all timers except for 0, to save time keeping functions
  InitTimersSafe(); 
  //sets the frequency for pwmOut pin 9
  bool success = SetPinFrequencySafe(pwmOut, frequency);  //change to pwmOut after testing
  //if the pin frequency was set successfully, turn pin 13 on
  if(success) {
    pinMode(pwmOut, OUTPUT);
  }
  //initialize PID variables
  Input = ebp_average;
  Setpoint = 356;  //test value = 20 psig
  myPID.SetOutputLimits(38, 115);

  //turn the PID on
  myPID.SetMode(AUTOMATIC);    
  
  // lcd setup
  lcd.begin(16, 2);
  lcd.clear();
  lcd.print("BRAKE%:");
  lcd.setCursor(0,1);
  lcd.print("EBP:");

}

void loop() {

  //exhaust pressure sensor
  ebp_total= ebp_total - ebp_readings[ebp_index];         
  ebp_readings[ebp_index] = analogRead(ebp); 
  ebp_total= ebp_total + ebp_readings[ebp_index];         
  ebp_index = ebp_index + 1;                    
  if (ebp_index >= numReadings)              
    ebp_index = 0;                           
    
  ebp_average = ebp_total / numReadings;    
  
  //throttle position sensor
  tps_total= tps_total - tps_readings[tps_index];         
  tps_readings[tps_index] = analogRead(tps); 
  tps_total= tps_total + tps_readings[tps_index];         
  tps_index = tps_index + 1;                    
  if (tps_index >= numReadings)              
    tps_index = 0;                           
    
  tps_average = tps_total / numReadings; 
  
/*  //vehicle speed sensor
  vss_total= vss_total - vss_readings[vss_index];         
  vss_readings[vss_index] = analogRead(vss); 
  vss_total= vss_total + vss_readings[vss_index];         
  vss_index = vss_index + 1;                    
  if (vss_index >= numReadings)              
    vss_index = 0;                           
    
  vss_average = vss_total / numReadings; 
 */ 
  //exhaust brake enable/disable switch
  ebswitchvalue = digitalRead(ebswitch);
  
  if(ebswitchvalue == LOW){
    lcd.setCursor(14,0);
    lcd.print("  ");
    lcd.setCursor(14,0);
    lcd.print("EN");
  }
  else{
    lcd.setCursor(14,0);
    lcd.print("  ");
    lcd.setCursor(14,0);
    lcd.print("DD");
  }    
  
  //exhaust brake delay
  current_time = millis();
  
  // output
  if(ebswitchvalue == LOW && tps_average <= 85){
    delay(2000);
    digitalWrite(relay, HIGH);
    Input = ebp_average;
    myPID.Compute();
    pwmWrite(pwmOut, pidOutput); 
    //print pidoutput
    lcd.setCursor(7, 0);
    lcd.print("   ");
    lcd.setCursor(7,0);
    lcd.print(((pidOutput / 255) * 100) * 2, 0);   

  }
  
  else{
    digitalWrite(relay, LOW);
    lcd.setCursor(7,0);
    lcd.print("   ");
    lcd.setCursor(7,0);
    lcd.print("OFF");
  }
  
  //print exhaust pressure sensor measurement
  lcd.setCursor(4, 1);
  lcd.print("   ");
  lcd.setCursor(4, 1);
  lcd.print(((ebp_average * 0.00488 * 19) - 13), 0);
  
  //print tps raw value
  lcd.setCursor(12,1);
  lcd.print("    ");
  lcd.setCursor(12,1);
  lcd.print(tps_average,0);
  
  //print start time
  lcd.setCursor(10,0);
  lcd.print("    ");
  lcd.setCursor(10,0);
  lcd.print(start_time);
  
  delay(1); 
}

Take a look at http://playground.arduino.cc//Code/AvoidDelay this gives examples of millis.

I have gone through the example you posted and many others and I still cannot seem to wrap my head around this. I am new to arduino and programming in general.

Thanks for all you'r help!

http://www.gammon.com.au/blink

Try to imagine you are cooking dinner and listening for the doorbell at the same time. How would you do it?

mitchedwards: I have gone through the example you posted and many others and I still cannot seem to wrap my head around this. I am new to arduino and programming in general.

Thanks for all you'r help!

Whenever I see this statement about the millis thing all I can think is that the person might have glanced at the page but definitely didn't study it very hard. We're not talking about rocket surgery. The concept of keeping up with what time it is now and what time something happened is about as simple as it gets. If you can't wrap your head around it then maybe programming isn't for you.

I mean how in the world do you ever make it to work on time?

Imagine that it is 11:00 and I tell you to do study Nick's page for the next hour and a half. How would you manage that? How would you know when an hour and a half had passed?

That sounded mean and it wasn't supposed to. What it was trying to say is that you have to stop trying to think of this as a programming problem. The time issue is not a programming problem. It's a simple problem that you solve every day without thinking about it. Once you realize that, the code that's doing it is dirt simple.

Here is the timer i have. It seems as tho it is not storing the time that tps_average wentt below 81.
Any advice?
Delta_G, i am new to this and am trying to learn as much as i can. When someone posts a link i do in fact read and study it.

Thank you everyone for the help it is much appreciated!

#include <PID_v1.h>
#include <PWM.h>
#include <SPI.h>
#include <LiquidCrystal.h>

int32_t frequency = 167; //frequency (in Hz)

const int numReadings= 3;

//exhaust pressure sensor
int ebp_readings[numReadings];      
int ebp_index = 0;                  
float ebp_total = 0;                  
float ebp_average = 0; 
const int ebp = A0;

//throttle position sensor
int tps_readings[numReadings];      
int tps_index = 0;                  
float tps_total = 0;                  
float tps_average = 0; 
const int tps = A1;

//vehicle speed sensor
int vss_readings[numReadings];      
int vss_index = 0;                  
float vss_total = 0;                  
float vss_average = 0; 
const int vss = A2;

// exhaust brake switch that enables or disabled exhaust brake
const int ebswitch = 12;
int ebswitchvalue = 0;

//Relay
int relay = 11;

//PWM pin
int pwmOut = 9;

//pid information
double Setpoint, Input, pidOutput;
PID myPID(&Input, &pidOutput, &Setpoint,1,0,0, DIRECT);


unsigned long start_time;
unsigned long current_time;
unsigned long previous_time;
int eb_delay = 0;
int eb_timer = 0;

// initialize lcd
LiquidCrystal lcd (8, 7, 6, 5, 4, 3);

void setup() {
  pinMode(ebswitch, INPUT);
  digitalWrite(ebswitch, HIGH);
  pinMode(relay, OUTPUT);
  pinMode(pwmOut, OUTPUT);
  
  //exhaust pressure sensor
  for (int ebp_thisReading = 0; ebp_thisReading < numReadings; ebp_thisReading++)
    ebp_readings[ebp_thisReading] = 0;

    //throttle position sensor
  for (int tps_thisReading = 0; tps_thisReading < numReadings; tps_thisReading++)
    tps_readings[tps_thisReading] = 0;  
 
  //vehicle speed sensor
  for (int vss_thisReading = 0; vss_thisReading < numReadings; vss_thisReading++)
    vss_readings[vss_thisReading] = 0;    

  //initialize all timers except for 0, to save time keeping functions
  InitTimersSafe(); 
  //sets the frequency for pwmOut pin 9
  bool success = SetPinFrequencySafe(pwmOut, frequency);  //change to pwmOut after testing
  //if the pin frequency was set successfully, turn pin 13 on
  if(success) {
    pinMode(pwmOut, OUTPUT);
  }
  //initialize PID variables
  Input = ebp_average;
  Setpoint = 356;  //test value = 20 psig
  myPID.SetOutputLimits(38, 115);

  //turn the PID on
  myPID.SetMode(AUTOMATIC);    
  
  // lcd setup
  lcd.begin(16, 2);
  lcd.clear();
  lcd.print("BRAKE%:");
  lcd.setCursor(0,1);
  lcd.print("EBP:");

}

void loop() {

  //exhaust pressure sensor
  ebp_total= ebp_total - ebp_readings[ebp_index];         
  ebp_readings[ebp_index] = analogRead(ebp); 
  ebp_total= ebp_total + ebp_readings[ebp_index];         
  ebp_index = ebp_index + 1;                    
  if (ebp_index >= numReadings)              
    ebp_index = 0;                           
    
  ebp_average = ebp_total / numReadings;    
  
  //throttle position sensor
  tps_total= tps_total - tps_readings[tps_index];         
  tps_readings[tps_index] = analogRead(tps); 
  tps_total= tps_total + tps_readings[tps_index];         
  tps_index = tps_index + 1;                    
  if (tps_index >= numReadings)              
    tps_index = 0;                           
    
  tps_average = tps_total / numReadings; 
  
/*  //vehicle speed sensor
  vss_total= vss_total - vss_readings[vss_index];         
  vss_readings[vss_index] = analogRead(vss); 
  vss_total= vss_total + vss_readings[vss_index];         
  vss_index = vss_index + 1;                    
  if (vss_index >= numReadings)              
    vss_index = 0;                           
    
  vss_average = vss_total / numReadings; 

*/
  
  if(tps_average < 81){
    start_time = millis();
    previous_time = start_time;
  }
  if(millis() - previous_time >= 2000){
    eb_delay = 1;
  }
  else{
    eb_delay = 0;
  }
    

  // output
  if(eb_delay = 1){
    digitalWrite(relay, HIGH);
    Input = ebp_average;
    myPID.Compute();
    pwmWrite(pwmOut, pidOutput); 
    //print pidoutput
    lcd.setCursor(7, 0);
    lcd.print("   ");
    lcd.setCursor(7,0);
    lcd.print(((pidOutput / 255) * 100) * 2, 0);   

  }
  
  else{
    digitalWrite(relay, LOW);
    lcd.setCursor(7,0);
    lcd.print("   ");
    lcd.setCursor(7,0);
    lcd.print("OFF");
  }
  
  //print exhaust pressure sensor measurement
  lcd.setCursor(4, 1);
  lcd.print("   ");
  lcd.setCursor(4, 1);
  lcd.print(((ebp_average * 0.00488 * 19) - 13), 0);
  
  lcd.setCursor(8,1);
  lcd.print("        ");
  lcd.setCursor(8,1);
  lcd.print(previous_time);
  
  lcd.setCursor(15,0);
  lcd.print(" ");
  lcd.setCursor(15,0);
  lcd.print(eb_timer);
  

  delay(100); 
}
  if(tps_average < 81){
    start_time = millis();
    previous_time = start_time;
  }
  if(millis() - previous_time >= 2000){
    eb_delay = 1;
  }
  else{
    eb_delay = 0;
  }

As you could suspect, here’s where your error is.
You are doing the right stuff, but in the wrong order.
You are registering an event (tps_average < 81).
Then you put a timestamp on it: start_time = millis();
Immediatly after that, you set the reference to that same timestamp.
And afterwards, you have a look to see if the difference between the timestamp and the reference is larger than 2 seconds, which is impossible.
You need to shuffle some line(s) to get things right.
I’m not telling what you need to do, i’m telling you what went wrong and hinting you to the solution, offering an opportunity to learn.

Just thought i would let you know guys know that i decided to use a counter instead of trying to use millis. The downside to this is i don’t know the exact time between counts until i have my code completely finished but i think this is a simpler way of doing it.

Thanks again for all your help!

#include <PID_v1.h>
#include <PWM.h>
#include <SPI.h>
#include <LiquidCrystal.h>

int32_t frequency = 167; //frequency (in Hz)

const int numReadings= 3;

//exhaust pressure sensor
int ebp_readings[numReadings];      
int ebp_index = 0;                  
float ebp_total = 0;                  
float ebp_average = 0; 
const int ebp = A0;

//throttle position sensor
int tps_readings[numReadings];      
int tps_index = 0;                  
float tps_total = 0;                  
float tps_average = 0; 
const int tps = A1;
int tps_counter = 0;

//vehicle speed sensor
int vss_readings[numReadings];      
int vss_index = 0;                  
float vss_total = 0;                  
float vss_average = 0; 
const int vss = A2;

//Relay
int relay = 12;

//PWM pin
int pwmOut = 9;

//pid information
double Setpoint, Input, pidOutput;
PID myPID(&Input, &pidOutput, &Setpoint,1,0,0, DIRECT);


unsigned long start_time;
unsigned long current_time;
unsigned long previous_time = 0;
int eb_delay;
int eb_timer = 0;

// initialize lcd
LiquidCrystal lcd (8, 7, 6, 5, 4, 3);

void setup() {
  pinMode(relay, OUTPUT);
  pinMode(pwmOut, OUTPUT);
  
  //exhaust pressure sensor
  for (int ebp_thisReading = 0; ebp_thisReading < numReadings; ebp_thisReading++)
    ebp_readings[ebp_thisReading] = 0;

    //throttle position sensor
  for (int tps_thisReading = 0; tps_thisReading < numReadings; tps_thisReading++)
    tps_readings[tps_thisReading] = 0;  
 
  //vehicle speed sensor
  for (int vss_thisReading = 0; vss_thisReading < numReadings; vss_thisReading++)
    vss_readings[vss_thisReading] = 0;    

  //initialize all timers except for 0, to save time keeping functions
  InitTimersSafe(); 
  //sets the frequency for pwmOut pin 9
  bool success = SetPinFrequencySafe(pwmOut, frequency);  //change to pwmOut after testing
  //if the pin frequency was set successfully, turn pin 13 on
  if(success) {
    pinMode(pwmOut, OUTPUT);
  }
  //initialize PID variables
  Input = ebp_average;
  Setpoint = 356;  //test value = 20 psig
  myPID.SetOutputLimits(38, 115);

  //turn the PID on
  myPID.SetMode(AUTOMATIC);    
  
  // lcd setup
  lcd.begin(16, 2);
  lcd.clear();
  lcd.print("BRAKE%:");
  lcd.setCursor(0,1);
  lcd.print("EBP:");

}

void loop() {

  //exhaust pressure sensor
  ebp_total= ebp_total - ebp_readings[ebp_index];         
  ebp_readings[ebp_index] = analogRead(ebp); 
  ebp_total= ebp_total + ebp_readings[ebp_index];         
  ebp_index = ebp_index + 1;                    
  if (ebp_index >= numReadings)              
    ebp_index = 0;                           
    
  ebp_average = ebp_total / numReadings;    
  
  //throttle position sensor
  tps_total= tps_total - tps_readings[tps_index];         
  tps_readings[tps_index] = analogRead(tps); 
  tps_total= tps_total + tps_readings[tps_index];         
  tps_index = tps_index + 1;                    
  if (tps_index >= numReadings)              
    tps_index = 0;                           
    
  tps_average = tps_total / numReadings; 
  
/*  //vehicle speed sensor
  vss_total= vss_total - vss_readings[vss_index];         
  vss_readings[vss_index] = analogRead(vss); 
  vss_total= vss_total + vss_readings[vss_index];         
  vss_index = vss_index + 1;                    
  if (vss_index >= numReadings)              
    vss_index = 0;                           
    
  vss_average = vss_total / numReadings; 

*/
  if(tps_average <=81){
    tps_counter = tps_counter + 1;
  }
  else{
    tps_counter = 0;
  }



  //Output
  if(tps_counter >= 20){
    digitalWrite(relay, HIGH);
    Input = ebp_average;
    myPID.Compute();
    pwmWrite(pwmOut, pidOutput); 
    //print pidoutput
    lcd.setCursor(7, 0);
    lcd.print("   ");
    lcd.setCursor(7,0);
    lcd.print(((pidOutput / 255) * 100) * 2, 0); 
  }
    else{
    pwmWrite(pwmOut, 0);
    digitalWrite(relay, LOW);
    lcd.setCursor(7,0);
    lcd.print("   ");
    lcd.setCursor(7,0);
    lcd.print("OFF");
  }
  
  //print exhaust pressure sensor measurement
  lcd.setCursor(4, 1);
  lcd.print("   ");
  lcd.setCursor(4, 1);
  lcd.print(((ebp_average * 0.00488 * 19) - 13), 0);
  
  lcd.setCursor(7,1);
  lcd.print("         ");
  lcd.setCursor(7,1);
  lcd.print(tps_counter);

  delay(100); 
}

It's a shame you gave up on this as the solution is very easy, and you get to learn a bit.

But if you don't need to use something you don't fully understand, it's OK to come up with another approach, maybe easier to implement.

by all means use a counter but use millis as the counter. I.E. subtract your number from the present millis this will give you a counter that you CAN be sure of the number of before you finish the program.

mitchedwards:
if ebswitchvalue == low and tps_average<=85 start a timer. while the timer is counting i still want the code to be reading the inputs and if the ebswitchvalue goes high or the tps_average value goes above 85 stop the timer so i do not turn on the relay and start outputing my pid pwm signal.

This is a really good description of the problem. Did you read your post after you posted? This bunch o words says exactly how to code it… I think you’re getting buried in code and forgetting the simple version of the problem. I think you should set up some functions so you don’t have to drink so much code at once. Your main loop should be about 6 lines…

readInputs();
if ( ebswitchvalue == low and tps_average<=85) {
   StartTimer();
} else {
  StopTimer();
}