Millis ????????????????

No matter how i try and get my head round this millis function i can never seem to figure it out!!

Im trying to turn on and led for 5 seconds once a button is pressed, then turn off after the time is up. Now i can do it fine with DELAY, but i cant use this as it will interfere with my main running program.

Could anyone please help me out here??

Here is the code for what i have with the delay ( im running 4 buttons into 1 analog input using voltage dividers BTW).

int potPin = 9;    // select the input pin for the potentiometer
int pump_1 = 4;   // select the pin for the LED
int pump_2 = 5;
int pump_3 = 6;
int pump_4 = 7;

int val = 0;       // variable to store the value coming from the sensor

void setup() {
  pinMode(pump_1, OUTPUT); 
  pinMode(pump_2, OUTPUT);
  pinMode(pump_3, OUTPUT);
  pinMode(pump_4, OUTPUT);// declare the ledPin as an OUTPUT
  Serial.begin(9600);
}

void loop() {
  {
  val = analogRead(potPin);    // read the value from the sensor
  Serial.print(   val   );                  // stop the program for some time
  }

{
if (val==511)
  {
    digitalWrite (pump_1, HIGH);
    delay(5000);
    digitalWrite (pump_1, LOW);
  }
}


                  // stop the program for some time
}

VR.

You have to remember that “millis” returns a “long”.
An “int” won’t work.
So, you need to read the time at the start of the delay, add 5000 to it, and keep reading “millis” until the time returned is greater than your end value.

Also, an exact match “== 511” on an analogue pin is a pretty rare event - you may want to match a range of values.

BTW, if you hold your keyboard upside-down and give it a good shake, it’ll probably loosen whatever it is that’s causing your question mark key to stick.

Any ideas on how to code that? I can see what your saying but have no idea how to write the code for that.

And thanks for the analog input info. so far its reacting to single number but im sure as time goes buy that input number will change as the switch wears!

Ps. Key board shook!!!

byte state = 0;
long end;

void loop() {

  switch (state) {
    case 0: 
      val = analogRead(potPin);
      Serial.print(   val   );
      if (val==511){
        digitalWrite (pump_1, HIGH);
        end = millis () + PUMP_ON_TIME;
        state = 1;
      }  
    break;
    
    case 1:
      if (end <= millis ()) {
        digitalWrite (pump_1, LOW);
        state = 0;
      }
    break;
  }
}

AWOL thanks so much for your help.

I was able to look over your code, multiply it and adjust it to suit my application and it works great!!!

Here is the code for my application ( calibration of 4 pumps ):

int cal_button = 9;                       // Select the input pin for the button array
int pump_1     = 4;                       // Select the pin for dose pump 1 output
int pump_2     = 5;                       // Select the pin for dose pump 2 output
int pump_3     = 6;                       // Select the pin for dose pump 3 output
int pump_4     = 7;                       // Select the pin for dose pump 4 output

int val = 0;                              // Variable to store the analog value coming from the calibrate button array

byte state1 = 0;                          // Variable to store the output state of dose pump 1
long end1;

byte state2 = 0;                          // Variable to store the output state of dose pump 2
long end2;

byte state3 = 0;                          // Variable to store the output state of dose pump 3
long end3;

byte state4 = 0;                          // Variable to store the output state of dose pump 4
long end4;

void setup() {
  pinMode(pump_1, OUTPUT);                // Set the Pin as an OUTPUT
  pinMode(pump_2, OUTPUT);                // Set the Pin as an OUTPUT
  pinMode(pump_3, OUTPUT);                // Set the Pin as an OUTPUT
  pinMode(pump_4, OUTPUT);                // Set the Pin as an OUTPUT
  Serial.begin(9600);
}

void loop() {

// **************************************** PUMP 1 *************************************************************  
  switch (state1) {
    case 0:
      val = analogRead(cal_button);
      Serial.print(   val   );
      if ((val>500)&&(val<600)){        // Activate dose pump 1 if analog value of switch is between 500 & 600
        digitalWrite (pump_1, HIGH);
        end1 = millis () + 60000;       // Activate for 60 seconds
        state1 = 1;
      }  
    break;
    
    case 1:
      if (end1 <= millis ()) {
        digitalWrite (pump_1, LOW);     // After time delay, turn dose pump 1 off
        state1 = 0;
      }
    break;
  }
  

// **************************************** PUMP 2 ************************************************************* 
  switch (state2) {
    case 0:
      val = analogRead(cal_button);
      Serial.print(   val   );
      if ((val>600)&&(val<700)){        // Activate dose pump 2 if analog value of switch is between 600 & 700
        digitalWrite (pump_2, HIGH);
        end2 = millis () + 60000;       // Activate for 60 seconds
        state2 = 1;
      }  
    break;
    
    case 1:
      if (end2 <= millis ()) {
        digitalWrite (pump_2, LOW);     // After time delay, turn dose pump 2 off
        state2 = 0;
      }
    break;
  }
  
  
// **************************************** PUMP 3 *************************************************************   
    switch (state3) {
    case 0:
      val = analogRead(cal_button);
      Serial.print(   val   );
      if ((val>700)&&(val<800)){        // Activate dose pump 3 if analog value of switch is between 700 & 800
        digitalWrite (pump_3, HIGH);
        end3 = millis () + 60000;       // Activate for 60 seconds
        state3 = 1;
      }  
    break;
    
    case 1:
      if (end3 <= millis ()) {
        digitalWrite (pump_3, LOW);     // After time delay, turn dose pump 3 off
        state3 = 0;
      }
    break;
  }
  
  
// **************************************** PUMP 4 ************************************************************* 
    switch (state4) {
    case 0:
      val = analogRead(cal_button);
      Serial.print(   val   );
      if ((val>800)&&(val<900)){        // Activate dose pump 4 if analog value of switch is between 800 & 900
        digitalWrite (pump_4, HIGH);
        end4 = millis () + 60000;       // Activate for 60 seconds
        state4 = 1;
      }  
    break;
    
    case 1:
      if (end4 <= millis ()) {
        digitalWrite (pump_4, LOW);     // After time delay, turn dose pump 4 off
        state4 = 0;
      }
    break;
  }
  



}

VR :slight_smile: