I have little to no experience in programming and i'm very slowly and gradually getting the hang of it. My next project is that i'm making a clock, and wanted to use multiple functions for the hour:minute:seconds instead of just using one for everything. But after reading some i came to the conclusion that the arduino can only run one function at a time. So my question is how can i make the illusion of multiple functions running simultaneously on a LCD screen.
My code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 144
#define btnDOWN 329
#define btnLEFT 504
#define btnSELECT 741
#define btnNONE 5
int read_LCD_buttons()
{
adc_key_in = analogRead(0);
if (adc_key_in > 1000) return btnNONE;
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE;
}
unsigned long minInterval = 60000UL; // Interval for minutes in milliseconds
unsigned long secInterval = 1000UL; // Interval for seconds in milliseconds
unsigned long prevMinMillis = 0UL; // Holds the timestamp of the last time Minute() was called
unsigned long prevSecMillis = 0UL; // Holds the timestamp of the last time Second() was called
void setup()
{
lcd.begin(16, 2);
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - prevMinMillis > minInterval)
{
prevMinMillis = currentMillis;
Minute();
}
if (currentMillis - prevSecMillis > secInterval)
{
prevSecMillis = currentMillis;
Second();
}
}
void Second(){
int First_Digit = 0;
int Second_Digit = 0;
for(Second_Digit; Second_Digit<10; Second_Digit++){
if(Second_Digit==10){
First_Digit = First_Digit + 1;
}
lcd.setCursor(0, 0);
lcd.print(First_Digit);
lcd.setCursor(1, 0);
lcd.print(Second_Digit);
lcd.clear();
if(First_Digit==5 && Second_Digit==9){
First_Digit = First_Digit - 5;
Second_Digit = Second_Digit -10;
}
if(Second_Digit==9){
First_Digit = First_Digit + 1;
Second_Digit = Second_Digit - 10;
}
}
}
void Minute(){
int First_Digit = 0;
int Second_Digit = 0;
for(Second_Digit; Second_Digit<10; Second_Digit++){
if(Second_Digit==10){
First_Digit = First_Digit + 1;
lcd.print(First_Digit);
}
lcd.setCursor(3, 0);
lcd.print(First_Digit);
lcd.setCursor(4, 0);
lcd.print(Second_Digit);
lcd.clear();
if(First_Digit==5 && Second_Digit==9){
First_Digit = First_Digit - 5;
Second_Digit = Second_Digit -10;
lcd.print(First_Digit);
}
if(Second_Digit==9){
First_Digit = First_Digit + 1;
lcd.print(First_Digit);
Second_Digit = Second_Digit - 10;
}
}
}
setCursor()
as you have. Is this not working? If not, please describe what it displays, and what you want to see it display? – jose can u c Jan 24 '18 at 19:35delay()
as you guessed. You need to think about your program flow as thoughloop()
runs millions of times per second, but you only want to execute a particular function less frequently. Expand to running multiple functions at different frequencies. – jose can u c Jan 24 '18 at 19:57