I would like to record the time(seconds) between when my mz80 sensor is activated and when it's not activated anymore with using millis() and I wanna print the result on my LCD (16*2). Imagine it's like you touched a button with your finger and a few seconds later you pulled your finger back, I want my lcd to show you how many seconds you touch the button for. My code is below.
I am having trouble getting the time difference between when the sensor is high and low. Could someone please take a look at my code and help me. I couldn't find anything on recording the time between 2 events. I dont know how to code the if else part and the millis part. Please help me.My code is below.
I have;
1 arduino uno, 1 breadboard, 1 mz80, 1 lcd(16*2), jumper wires
</>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long startMillis ;
unsigned long currentMillis;
int sensor_pin=2;
void setup() {
// put your setup code here, to run once:
lcd.begin();
pinMode(sensor_pin, INPUT );
startMillis = millis();
}
void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(4,0);
lcd.print("Time");
currentMillis = millis();
bool info=digitalRead(sensor_pin);
if(info==0 ){
startMillis = currentMillis;
lcd.setCursor(0,1);
lcd.print("Result:");
lcd.setCursor(7,2);
lcd.print(currentMillis - startMillis);
} else {
lcd.setCursor(0,1);
lcd.print("");
}
delay(400);}
When the sensor becomes active (not is active) save the value of millis() as the start time then, when the sensor becomes inactive (not is inactive), save the value of millis() as the end time. Subtract the start time fom the end time to get the elapsed time in milliseconds
So i was trying to start the timer when sensor is activated with startMillis and stop the timer somehow with currentMillis and print the result . I saw it in an another arduiono dorum questions that someone posted " startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state." it was this. I know that this code is not what my project needs but tried it anyway because i dont know what to do
I just checked the StateChangeDetection example in the IDE and I thinks its works for me but how do i print the elapsed time in seconds. Is it like
lcd.print((startTime - endTime)/1000);
I know some places on my is completely wrong. But it does show me the seconds. The thing is the moment i power up my arduino uno its starts counting the seconds but what i want it to do is that to just show me how many seconds had past between when sensor is on and off.
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long sensorActive ;
unsigned long lastSensor;
int sensor_pin=2;
int sensorState=0;
void setup() {
// put your setup code here, to run once:
lcd.begin();
pinMode(sensor_pin, INPUT );
}
void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(4,0);
lcd.print("Time");
sensorState=digitalRead(sensor_pin);
if(sensorState != lastSensor ){
if (sensorState == HIGH){
sensorActive = millis();
lcd.setCursor(0,1);
lcd.print(sensorActive/1000);
} else {
lcd.print("off");
}
delay(50);
}
lastSensor = sensorState;}
I did something similar to StateChangeDetection example but how and where do i define " save the value of millis() as the start time then, when the sensor becomes inactive (not is inactive), save the value of millis() as the end time. Subtract the start time fom the end time to get the elapsed time in seconds"
yes, I want to print the time in seconds that's why I divide the result by 1000 . But I couldn't save the value of millis() as the start time when the sensor is active(HIGH), and save a value as the end time when the sensor becomes inactive. Should I save them in void loop ? inside of the if statement maybe? and then perhaps Subtract start time and the end time to get the elapsed time in seconds.
When I change this part of the code if (sensorState == HIGH) to if (sensorState == 0) it worked
Thank you so much buddy for everything you really help me a lot. I have one more question it shows the seconds as like 2.36 or 10.38 can I round that number up to an integer