So I don't really know much about coding and this is my first custom project with my Ardunio Uno. It's basically a simple timer, one button to start and reset, then a sensor with a digital output for a stop. I want the elapsed time showing on the screen while the test is in progress and then the ending time showing on the screen once the sensor is tripped, until it is reset with the reset start/reset button.
I've been playing around with the code while researching how to do this and currently what I have here seems to be doing exactly what I want which is great! But I have a feeling it's butchered and has remnants of previous attempts and not really following best practices. Particularly with how it displays the test complete time, I think it's just leaving whatever was displayed on the second line of the screen screen during the test's run time. It's the correct result, but I imagine there's a better way to do this. Any ideas or feedback appreciated!
#include <Time.h>
#include <TimeLib.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
time_t elapsedTime;
void setup()
{
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
Serial.begin(9600);
pinMode(8, INPUT);
digitalWrite(8, HIGH);
pinMode(9, INPUT);
digitalWrite(9, HIGH);
}
void loop()
{
lcd.setCursor(0,0);
lcd.print("Press Start ");
lcd.setCursor(0,1);
lcd.print(" ");
delay(100);
if(digitalRead(8) == LOW)
{
setTime(000000);
while(digitalRead(9) == HIGH)
{
lcd.setCursor(0,0);
lcd.print("Test in Progress");
elapsedTime = now();
lcd.setCursor (2,1);
lcd.print(hour(elapsedTime));
lcd.print(" h ");
lcd.setCursor (7,1);
lcd.print(minute(elapsedTime));
lcd.print(" m ");
lcd.setCursor (12,1);
lcd.print(second(elapsedTime));
lcd.print(" s ");
}
if(digitalRead(9) == LOW)
{
while(digitalRead(8) == HIGH)
{
lcd.setCursor(0,0);
lcd.print("Test Complete ");
}
}
}
}
For what you want, it's not so bad. If you wanted to add so little as a blinking status light, that is when you find need to learn more.
pinMode(8, INPUT);
digitalWrite(8, HIGH);
pinMode(9, INPUT);
digitalWrite(9, HIGH);
This is good and used to be what we had to do. Then Arduino added a mode, INPUT_PULLUP.
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
GoForSmoke:
For what you want, it's not so bad. If you wanted to add so little as a blinking status light, that is when you find need to learn more.
pinMode(8, INPUT);
digitalWrite(8, HIGH);
pinMode(9, INPUT);
digitalWrite(9, HIGH);
This is good and used to be what we had to do. Then Arduino added a mode, INPUT_PULLUP.
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
That's a good idea about the light, the sensor I'm using actually has a power led and another led that lights up when it trips, maybe I'll wire it up to a secondary led or try to code in a separate led that goes off.
As far as that code with the pin goes, those two codes are interchangeable?
Made a few changes, I want to include 'days' in the test along with seconds, minutes, and hours, but I couldn't really figure out how to do that with the timelib, the days from that always seemed to start at 1.
I 'm giving this a shot, making elapsed days an int a, then a separate int b which is int a - 1.
This displays a 0 for days when the test begins as it should, but I'm unsure by the time day 1 rolls around if it will properly update.
#include <Time.h>
#include <TimeLib.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
time_t elapsedTime;
void setup()
{
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
Serial.begin(9600);
pinMode(8, INPUT);
digitalWrite(8, HIGH);
pinMode(9, INPUT);
digitalWrite(9, HIGH);
}
void loop()
{
lcd.setCursor(0,0);
lcd.print("Press Start ");
lcd.setCursor(0,1);
lcd.print(" ");
delay(100);
if(digitalRead(8) == LOW)
{
setTime(000000);
while(digitalRead(9) == HIGH)
{
lcd.setCursor(0,0);
lcd.print("In Progress");
elapsedTime = now();
int a = (day(elapsedTime));
int b = a-1;
lcd.setCursor (13,0);
lcd.print(b);
lcd.setCursor (14,0);
lcd.print(" ");
lcd.setCursor (15,0);
lcd.print("d");
lcd.setCursor (2,1);
lcd.print(hour(elapsedTime));
lcd.print(" h ");
lcd.setCursor (7,1);
lcd.print(minute(elapsedTime));
lcd.print(" m ");
lcd.setCursor (12,1);
lcd.print(second(elapsedTime));
lcd.print(" s ");
}
if(digitalRead(9) == LOW)
{
while(digitalRead(8) == HIGH)
{
lcd.setCursor(0,0);
lcd.print("Complete ");
}
}
}
}
rkrausko:
As far as that code with the pin goes, those two codes are interchangeable?
They do exactly the same thing. Next time you can save a little typing FWIW.
A status light has to run independent of what the sketch does. When it blinks on time you know that void loop() is running fast enough to catch the desired change state time -on time or very close-. The way your code works (and it does work) can't add a (run-)status light, just another power light.
With the do-many-things-at-once lesson you can make sketches that have little sketches inside and they all run on every pass through void loop(), even if just to see that conditions aren't right (like has a time interval finished yet?) and return to let the other little sketches run -- and then void loop() ends and void loop() runs again.. has that interval finished yet?
Imagine you are in a swivel chair surrounded by a circle of switches with leds over them. You see the led and if it is ON you click the switch up, if it is OFF you click the switch down. And then you move to the next, etc, always circling. That is like tasking on Arduino with you as the chip and the led+switch sets are all tasks and who knows what connects to those switches or what drives those leds . but . the . coder.