How to implement a timer on an arduino micro

Hello all,

I am new to Arduino programming and electronics in general.

I am designing a vacuum system that uses a diaphragm pump. I want to implement a timer in my system to detect it the pump has been running for more than 45secs and thus cause an led to indicate this 45secs runtime status. Please I am lost on how to implement this function in an Arduino program. Thank you for your help


const int GLED = 13;
const int PRESS = A5;
const int POT = A3;
const int PUMP = 4;

bool gledVal = LOW;

unsigned long previousMillis = 0;        // will store last time LED was updated

const long interval = 45000;           // interval at which to blink (milliseconds)

int PRESS_INIT = 0;
float PRESS_INIT_VOLT = 0.0;
float PRESS_INIT_BAR = 0.0;
float PRESS_INIT_MMHG = 0.0;

int pressVal = 0;
float pressVal_Volts = 0.0;
float pressVal_Bar = 0.0;
float pressVal_mmHg = 0.0;
float pressVal_Gauge = 0.0;

int potVal = 0;
int pumpVal = 0;

void setup() {
  Serial.begin(9600);
  
  pinMode(GLED,OUTPUT);
  digitalWrite(GLED,gledVal);

  pinMode(PRESS,INPUT);
  PRESS_INIT = analogRead(PRESS);
  PRESS_INIT_VOLT = (PRESS_INIT*5.0)/1023.0;
  PRESS_INIT_BAR = (PRESS_INIT_VOLT - (3.3*0.1))/(0.8*3.3);
  PRESS_INIT_MMHG =  PRESS_INIT_BAR * 750;
  
  pinMode(POT,INPUT);
  pinMode(PUMP,OUTPUT);
}

void loop() {
  pressVal = analogRead(PRESS);
  pressVal_Volts = (pressVal*5.0)/1023.0;
  pressVal_Bar = (pressVal_Volts - (3.3*0.1))/(0.8*3.3);
  pressVal_mmHg = pressVal_Bar * 750;
  pressVal_Gauge = PRESS_INIT_MMHG - pressVal_mmHg;

//Portion that implements the timer
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    if (gledVal == LOW) {
      gledVal = HIGH;
    } else {
      gledVal = LOW;
    }
    
    digitalWrite(GLED, gledVal);
  }

  potVal = analogRead(POT);
  pumpVal = map(potVal,30,688,50,175);
  analogWrite(PUMP,pumpVal);
  
}

Save the value of millis() when the pump is started. Then, each time through loop(), if the pump is running, test whether the current value of millis() minus the saved value is greater than the required period. If so, the pump has been running that long

Using a boolean set to true when the pump is running and false when it is not makes the process easier

Hi @orhema ,
post your sketch ( use tags</>) and your schematic.

RV mineirin

Please read

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

See this section Delay execution until condition has been true for X secs for a timer based delay.

Please can you check what's wrong with my code above, thank you.

I just posted my code, thank you.

One obvious problem is that you unconditionally check whether the period has passed whether or not the pump is running

You need to do something like this

Save the value of millis() when the pump starts and set a boolean to true to indicate that timing is taking place.

Each time through loop(), if the boolean is true then test whether the required period has elapsed by subtracting the start value from the current value of millis(). If so, act accordingly.

If the pump stops before the period elapses then set the boolean to false to stop timing

My millisDelay class has that boolean internally to keep track of if the timer is running. But good coding practice to handle all those details yourself.

I see the problem may not have to do with my timer, but the map function I have used to control the speed of the pump.

The mapped values I have do not actually control the speed or power delivered to the pump, instead it just control the ON/OFF state, which isn't what I wrote the program for anyway.

Please can anyone help in this regard

Hi @orhema
in this line: pumpVal = map (potVal, 30, 688, 50, 175);
portVal receives the value of an anlog port and they can be from 0 to 1023.
and in this line:
analogWrite (PUMP, pumpVal);
and dutty can be from 0 to 255.

Correct to use like this:
pumpVal = map (potVal, 0, 1023, 0, 255);

RV mineirin

Thank you for your reply. However, I tried that and it still doesn't change the speed for me. I am using g a 12V/2A pump driven by a MOSFET connected to the Arduino, and all it does is switch the pump on/off despite the cnage in POT value.

Hi
You can post a schematic of your project (can be done by hand).

RV minirin

const int PUMP = 4;
analogWrite(PUMP,pumpVal);

I do not think that pin4 is a pwm pin on the micro.

Please can you explain why that matters, and what pins are pwm, because the diagram above does not specify

Arduino micro PWM pins :
3, 5 , 6 , 9, 10, 11 , 13

PWM Channels 7 See https://store.arduino.cc/usa/arduino-micro

RV mineirin

The little ~ before a pin indicates it is pwm capable.

Many pinout diagrams have a better key, and I did not pay close attention.

Here's one with better labeling

https://www.etechnophiles.com/arduino-micro-pinout-schematic-and-specifications/

Thank you for your help, it was indeed the PWM Pins that was the problem

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.