I have taken a program for an LCD that prints Hello world and displays seconds increasing from boot.
I have modified this to try to make a timer that controls a solenoid while a circuit is closed and for 'x' seconds after the circuit is opened
I am trying to make a countdown timer that will start from a preset value. I modified the loop to start at 9 and decrease by one and display on the LCD. I also added a Serial.Println to see what was happening and I added a LED to come on at the end.
IT is not doing what I expected (my expectations may be wrong). I wanted it to stop after one loop and the LED to stay on.
The loop continues, countdown starts at 9 and goes to 1 and then starts again. When I had the LED to turn on with the IF statement if i was less than one it would not come on, changed to i<2 and it does light up until loop restarts and i is >2, I understand this.
So why does countdown not go to 0 and why does it keep starting over.
Also when watching the serial console, the LED seems to light when 9 is output and not when 1 is output, I am assuming this is a timing issue.
Thank you for any help!
/*
LiquidCrystal Library - Hello World
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch prints "Hello World!" to the LCD
and shows the time.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// LED output -- added by me
pinMode(8, OUTPUT);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Post Flow Time:");
}
void loop() {
Serial.begin(9600);
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// countdown and output to serial:
for (int i=9; i>=1; i--) {
lcd.setCursor(0, 1);
lcd.print(i);
// Print to console for info
Serial.print("i is :");
Serial.println(i);
delay(1000);
// read timer and light LED of time is less that 1
if (i<2) {
digitalWrite(8, HIGH);
} else {
digitalWrite(8, LOW);
}
}
}
