Arduino time interval (using millis) issue

Dear All,

I have a motor which I want to run for a spefic time entered by user through the keypad.

The problem occurs when the time interval is more than 30 s.

The motor (mentioned as auger in the code) runs perfectly for intervals less than 30 seconds. But if the interval is more than 30 s the motor keeps running non stop.

I would highly appreciate if you guys can help me on this.

Below is the part of my code responsible for running the motor.

regards,

modeState = digitalRead(calibSwitch);
  if (modeState==LOW)
  {
   if (modeState!=oldState)
   {
    oldState=modeState;
    displayCalibMenu();
   }
  else
   {
    oldState=modeState;
   }
  }
 checkCalibKeypad();

  augerTime = sandCalib * 1000;
  unsigned long currentMillisCalib = millis(); 
  Serial.println(augerTime);

 // check the button
 if (digitalRead(runSwitch) == LOW) {
 // update the time when button was pushed
 buttonPushedMillis = currentMillisCalib;
 ledReady = true;

 }
   
 // make sure this code isn't checked until after button has been let go
 if (ledReady) {
   if ((unsigned long)(currentMillisCalib - buttonPushedMillis) >= turnOnDelay) {
     // okay, enough time has passed since the button was let go.
     digitalWrite(augerSwitch, HIGH);
    
     digitalWrite(augerLED, HIGH);
     
     
     
     // setup our next "state"
     augerState = true;
     

     augerLED_State = true;
     
     
     // save when the LED turned on
     ledTurnedOnAt = currentMillisCalib;
     // wait for next button press
     ledReady = false;
     
   }
 }
  
 // see if we are watching for the time to turn off LED
 if (augerState ) {
   // okay, led on, check for how long
     
   if ((unsigned long)(currentMillisCalib - ledTurnedOnAt) >= augerTime) 
   {
     augerState = false;
     digitalWrite(augerSwitch, LOW);
     augerLED_State = false;
     digitalWrite(augerLED, LOW);
   }
}

30, or 32, second proper functioning suggests that you are using an int somewhere where you should be using an unsigned long.

Post ALL of your code, so we can see what types all the variables are.

Dear Paul Thank you for your reply.

30, or 32,

Yes, slightly more. But thats not a big problem for me.

Post ALL of your code, so we can see what types all the variables are.

I didnt post the complete code as it is very long (994 lines to be exact) and it would be difficult for you guys to get to the problem.

Here are the variables.

#include <EEPROM.h>
#include <SPI.h>
#include <LiquidCrystal.h>
#include <Keypad.h>

int cementAddr = 0;
int sandAddr = 1;

int x = 0;
int y = 0;

int cementSet = 0;
int sandSet = 0;

int cementCalib = 0;
int sandCalib = 0;
int waterCalib = 0;

int value=0;
int cementWeight = 0;
int sandWeight = 0;
int waterWeight = 0;
char selection;

int page_counter=1;   
const int previous = 11;             
const int next = 10;

const byte emergency = 16;
const byte runSwitch= 53;
const byte modeSwitch= 23;
const byte autoSwitch= 24;
const byte calibSwitch =25;

const byte augerSwitch = 47;
const byte conveyorSwitch = 46;
const byte waterSwitch = 48;
const byte foamSwitch = 49; 

const byte manualAuger = 44;
const byte manualConveyor = 45;

const int sandVibRelay = 43;
const int cementVibRelay = 42;
const int cementValveRelay =40;
const byte sandVibLED = 36;
const byte cementVibLED = 35;

const byte sandVibSwitch = 20;
const byte cementVibSwitch = 19;
const byte cementValveSwitch =17;

const byte augerLED = 38;
const byte conveyorLED = 37;
const byte waterLED = 3;
const byte emergencyLED = 39;

unsigned long buttonPushedMillis; // when button was released
unsigned long ledTurnedOnAt; // when led was turned on
unsigned long turnOnDelay = 2500; // wait to turn on LED
unsigned long augerTime = 0; // turn off LED after this time
unsigned long conveyorTime = 0; // turn off LED after this time
unsigned long waterTime = 8000;
unsigned long foamTime = 5000;

int modeState = 0;
int oldState = 0;

int calibState = 0;
int oldCalibState = 0;

bool ledReady = false; // flag for when button is let go
bool augerState = false; // for LED is on or not.
bool conveyorState = false; // for LED is on or not.
bool waterState = false;
bool foamState = false;

bool augerLED_State = false;
bool conveyorLED_State = false;
bool waterLED_State = false;

bool sandVibState = LOW;      
bool cementVibState = LOW;   

unsigned long previousMillis = 0;        
const long interval = 1000;  
unsigned long previousMillisCement =0;

boolean current_previous = LOW;         
boolean last_previous=LOW;           
boolean last_next = LOW;
boolean current_next = LOW;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] =
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {33,32,31,30}; //row pinouts of the keypad (L1, L2, L3, L4)
byte colPins[COLS] = {29,28,27,26}; //column pinouts of the keypad (R1, R2, R3)

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

LiquidCrystal lcd1(2,3,4,5,6,7);
LiquidCrystal lcd2(2,9,4,5,6,7);
int sandCalib = 0;

  augerTime = sandCalib * 1000;

So, sandCalib is an int and 1000 is an int. When sandCalib contains a value greater than 32, the result of the multiplication will not fit into an int register, which is what will be used to hold the value. So, the copy of the incorrect bit pattern to augerTime will not result in the correct value in augerTime.

1000UL, on the other hand, isn't an int, so an unsigned long register will be used to hold the result of the multiplication, and the correct bit pattern will be copied to augerTime.

See reply #1. You can post it as an attachment.

@PaulS

I tried your recommendation and it works perfectly now.

Thanks for your help.