I am trying to put together a timer that starts at 30 mins, and if a button is pressed it will add 5 mins, then counts down to 0, which will trigger a relay, and then start over.
I have my lcd display working.
I have the debounce code working for my button.... I think ( it powers on/off an led without flicker)
Button does not add to timer
My timer starts counting down from 3 seconds not minutes, and has the last two blocks on the the screen filled in. (time display not formatted correctly.)
Can I get some advice on how to clean up this code? I know I am close!!!!!!
/*
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
modified 7 Nov 2016
by Arturo Guadalupi
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystalHelloWorld
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// constants won't change. They're used here to set pin numbers:
const int button2Pin = 7; // the number of the second pushbutton pin
const int relay = 1; // the number of the relay driver pin
const int ledPin = 13; //led attached to this pin
// variables will change:
// variable for beginging amount for countdown timer
int min = 30;
int sec = 30;
int starTime = 0;
unsigned long oneSecond = 1000UL;
unsigned long startTime;
int buttonState = LOW; //this variable tracks the state of the button, low if not pressed, high if pressed
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
//************************************************************************************************************
void setup() {
sec = sec + 60 * min;
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Time Remaining:");
// initialize the Relay pin as an output:
pinMode(relay, OUTPUT);
// initialize the pushbutton2 pin as an input:
pinMode(button2Pin, INPUT);
pinMode(ledPin, OUTPUT);
} // Close void setup
void loop() {
//sample the state of the button - is it pressed or not?
buttonState = digitalRead(button2Pin);
//filter out any noise by setting a time buffer
if ( (millis() - lastDebounceTime) > debounceDelay) {
//if the button has been pressed, add time to existing timer"
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
// add 5 mins whenever button2 is pressed:
startTime = (startTime + (sec + 300 * min)); //******LOOK @@@@@ THIS LINE ******************
lastDebounceTime = millis(); //set the current time
}
else if ( buttonState == LOW) {
digitalWrite(ledPin, LOW);
lastDebounceTime = millis();//set the current time
}
}
if (millis() - startTime >= oneSecond) {
// set the cursor to column 0, line 1
lcd.setCursor(0, 1);
sec--;
startTime += oneSecond;
if (sec < 0) lcd.print("NOOO");
int displayMin = sec/60;
if (displayMin < 10) lcd.print("0");
lcd.print(displayMin);
lcd.print(":");
int displaySec = sec % 60;
if (displaySec > 10) lcd.print("0");
lcd.println(sec % 60);
}
if (startTime = 0) {
// when ever the timer reaches 0
lcd.print("Flavor Town");
digitalWrite (relay, HIGH);
delay (2);
digitalWrite (relay, LOW);
}
}