How to read time on Arduino IDE
10 December, 2018 by
How to read time on Arduino IDE
Alejandro Jabalquinto

Introduction

In this post it will be seen how to read the time since the Arduino based PLC board began running with the function millis().  Basically this function return the number of milliseconds since the controller began running the current program. This function is really useful when you want to control process comparing time

*This value will overflow after 50 days approximately

Requirements

Ethernet or 20 I/Os PLC:      Ethernet PLC     20 I/Os PLC    

Industrial Shields boards:   Industrial Shields Boards


Function

time = millis();


Parameters

Nothing


Return

Unsigned long of millisecond since the controller program started


Example

Code Example, blinking of an analog outputs:

unsigned long actualtime = 0;
unsigned long beforetime = 0;
unsigned long deltatime = 1000;
bool laststate = true;

void setup() {
  Serial.begin(9600);
  pinMode(Q0_0, OUTPUT);
}

void loop() {
  //Save actual time on actualtime variable
  actualtime = millis(); 
  //Compare if it have passed a second
  if (actualtime - beforetime >= deltatime){
    if (laststate){
      //Digital output at HIGH position
      digitalWrite(Q0_0, HIGH);
      laststate = false;
    }
    else{
      //Digital output at LOW position
      digitalWrite(Q0_0, LOW);
      laststate = true;
    }
    //Set beforetime to the actual value of microseconds
    beforetime = millis(); 
  }
}
How to read time on Arduino IDE
Alejandro Jabalquinto
10 December, 2018
Share this post
Archive

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>

Do you want more information?

Just fill the form!

Tell me more!