I'm trying to make a stopwatch display a time delay of a sensor when it detects the object on conveyor. Just wondering if there's anyway to store the millis() value in a point of time and minus to another ongoing millis() and get the work as a stopwatch.
In your Arduino IDE, open File -> Examples -> 02. Digital -> BlinkWithoutDelay.
That should give you an idea of how it's done.
Always use unsigned long variables for millis() and micros(), for example:
unsigned long start_time = millis();
...
unsigned long elapsed_time = millis() - start_time;
Sure
unsigned long start_mS = millis();
// some code here
unsigned long stop_mS = millis();
Serial.print(F("elapsed time mS:")); Serial.println(stop_mS - start_mS);
Also check out my tutorial on How to write Timers and Delays in Arduino
I tried this but it display 00:00
try using micros() instead of millis();
and post the actual code you are running
#include <TM1637.h>
#include <TM1637Display.h>
#define CLK 8
#define DIO 9
int zoneinput = 3;
int inhiritoutput = 2;
int zonecheck;
TM1637 tm(CLK,DIO);
void setup() {
pinMode(zoneinput, INPUT);
pinMode(inhiritoutput, OUTPUT);
Serial.begin(9600);
tm.init();
tm.set(2);
}
void loop() {
zonecheck = digitalRead(zoneinput);
Serial.println(zonecheck);
delay(500);
if(zonecheck == 1) // sensor haven't triggered
{
tm.display(3,10);
tm.display(2,1);
tm.display(1,0);
tm.display(0,15);
tm.point(POINT_OFF);
}
else if(zonecheck == 0) // sensor triggered and time start counts
{
int total;
unsigned long prev = millis();
unsigned long sec = millis();
total = sec - prev;
// This part will display my time on module.
tm.display (3,total%10);
int pos2 = total/100;
tm.display (2,pos2%10);
int pos1 = pos2 /10;
tm.display (1,pos1%10);
int pos0 = pos1 /10;
tm.display (0,pos0%10);
tm.point(POINT_ON);
}
}
Two points
i) total should be also unsigned long and
ii) nothing happens between these two statements
unsigned long prev = millis();
unsigned long sec = millis();
so no change in millis(); (well almost always no change)
you need to change the code so that
prev = millis() is only called once when the timer starts.
That's exactly what I'm doing ![]()
finding a way to only call prev when it's triggered
Well you can add a global boolean to say triggered
like
bool triggered = false;
unsigned long prev; // make this a global
and
else if(zonecheck == 0) // sensor triggered and time start counts
{
unsigned long total;
if (!triggered) {
triggered = true;
prev = millis();
}
unsigned long sec = millis();
total = sec - prev;
BUT... how do you decide when to un-trigger the timer?
Finally instead of the delay(500) to slow down the prints try a millis() timer
see How to write Timers and Delays in Arduino
and Multi-tasking in Arduino
This worked! Appreciate a lot.
I'll figure out later how to un-triggered. You the best, thanks again!
I want it to untriggered when the sensor is no longer detect the object . And the program starts again in loop.
how about
if (zonecheck == 1)
// stop/reset timer
triggered = false;
} else if (zonecheck == 0) {
etc..
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.