I've made a code in which I set a time in minutes, with a potentiometer, in the void loop() as it allows me to print it in the Serial Monitor as well as in the LCD display. My question is, how can I use the variable time, set in the void loop, in the void setup so the timer can start with that value?
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Countimer.h>
Countimer timer;
LiquidCrystal_I2C lcd(0x27,16,2);
int time;
int pot = 0;
//int buttonState = 0;
int start = 2;
//const int relay = 2;
void setup() {
//pinMode(4, INPUT);
pinMode(2, INPUT);
Serial.begin(9600);
lcd.init();
lcd.backlight();
time == time;
// Set up count down timer with 10s and call method onComplete() when timer is complete.
// 00h:00m:10s
timer.setCounter(0, time, 0, timer.COUNT_DOWN, onComplete);
// Print current time every 1s on serial port by calling method refreshClock().
timer.setInterval(refreshClock, 1000);
//lcd.setCursor(0,0);
//lcd.print("PUMP CONTROLLER");
//lcd.setCursor(0,1);
//lcd.print(" THES");
//delay(3000);
//lcd.clear();
//Serial.println(time);
}
void refreshClock() {
Serial.print("Current count time is: ");
Serial.println(timer.getCurrentTime());
lcd.setCursor(0,0);
lcd.print("Time:");
lcd.setCursor(6,0);
lcd.print(timer.getCurrentTime());
Serial.println(time);
}
void onComplete() {
Serial.println("Complete!!!");
lcd.setCursor(6,0);
lcd.print("COMPLETE");
delay(2000);
refreshClock();
}
void loop() {
// Run timer
time = analogRead(pot);
time = map(time, 0, 1023, 60, 0);
lcd.setCursor(0,1);
lcd.print("TIME:");
lcd.setCursor(6,1);
//lcd.print(" ");
lcd.setCursor(6,1);
lcd.print(time);
//timer.setCounter(0, time, 0, timer.COUNT_DOWN, onComplete);
if (digitalRead(start) == HIGH){
timer.run();
if(!timer.isCounterCompleted()) {
timer.start();
}
}
//timer.run();
// Now timer is running and listening for actions.
// If you want to start the timer, you have to call start() method.
//if(!timer.isCounterCompleted()) {
// timer.start();
//}
if (digitalRead(start) == LOW){
refreshClock();
}
digitalRead(2);
delay(1000);
Serial.println(digitalRead(2));
Serial.println(time);
}