I've been using Arduino and learning how to code with it for 1 weeks now. I have some problem with my code and I don't know how to solve them, I've done a lot of research but I don't know how to fix my error. What I want to do is making a Running Timer on a layer that start when the arduino is ON. The problem is when we are on the layer the timer start/continue and when we are on another layer, the timer is stopped.
I've markes each zone that I suppose it's causes a problem in my code with ; ///// problem : ... /////.
#include <LiquidCrystal.h>
#include <LCDKeypad.h>
#define Sec(s) (s*1000ul)
#define Hour(h) (h*60ul*60ul*1000ul)
LCDKeypad lcd;
unsigned int days = 0;
unsigned int hours = 0;
unsigned int minutes = 0;
unsigned int seconds = 0;
unsigned int setting = 0;
const uint8_t ledPin = 1;
const uint32_t
timeON = Sec(1),
timeOFF = Sec(2);
bool ledState = false;
uint32_t
time = timeOFF,
curTime = 0,
prevTime = 0;
void setup() {
pinMode(1, OUTPUT);
lcd.begin(16,2);
lcd.setCursor(0,0);
analogWrite(10, 50);
lcd.print("Home");
}
void loop() {
btnRead();
curTime = millis();
if ( curTime - prevTime >= time )
{
prevTime = curTime;
ledState = !ledState;
time = ledState ? timeON : timeOFF;
digitalWrite(ledPin, ledState);
}
}
void btnRead(){
for (int i = 0; i < 1; i++) {
int button = lcd.button();
switch (button) {
case KEYPAD_RIGHT:
setting++;
break;
case KEYPAD_LEFT:
setting--;
break;
case KEYPAD_UP:
switch (setting) {
case 0:
break;
case 1:
break;
case 2:
break;
}
case KEYPAD_DOWN:
switch (setting) {
case 0:
break;
case 1:
break;
case 2:
break;
}
}
setting %= 4;
lcdPrint();
/////Create a delay between each Layer when changing from Layer 1 to Layer 2 and vise versa/////
while(millis() % 200 != 0);
///// Problem : the button is sync to make change each 200 millisecs set with the board so... ///
///// ... it is not responding all the time/////
}
}
void lcdPrint() {
lcd.setCursor(0,0);
switch (setting) {
case 0:
lcd.print("Home ");
/////Reset the LCD when changing layer////////
lcd.setCursor(0,1);
lcd.print(" ");
//////////////////////////////////////////////
/////////problem: make the LCD flickering/////
//////////////////////////////////////////////
if (ledState == LOW) {
lcd.setCursor(9,1);
lcd.print("LED OFF");
} else {
(ledState == HIGH);
lcd.setCursor(9,1);
lcd.print("LED ON ");
digitalWrite(ledPin, ledState);
}
lcd.setCursor(0, 1);
lcd.print(millis()/1000);
break;
case 1:
lcd.print(" Running Time ");
days %= 100;
hours %= 24;
minutes %= 60;
seconds %= 60;
seconds++;
if (seconds == 60) {
seconds = 0;
minutes++;
if (minutes == 60) {
minutes = 0;
hours++;
if (hours == 24) {
hours = 0;
days++;
}
}
}
/////////////Make the LCD Running Timer Layer on a printed patern : ////////
///////////// printed patern : (" 00 00:00:00 ")////////
lcd.setCursor(0,1);
char time[17];
sprintf(time, " %02i %02i:%02i:%02i ", days, hours, minutes, seconds);
lcd.print(time);
/////////////////Make the Timer on time////////////////////////
while(millis() % 1000 != 0);
/////////////////////////////////////////////////////////////////
/////Problem: KeyPad aren't responding well /////////////////////
/////Problem: When we're not on the Layer, the timer stop... ////
///// ...when it's suppose to be running all the time////
/////////////////////////////////////////////////////////////////
break;
case 2:
lcd.print("Setting ");
lcd.setCursor(0,1);
lcd.print(" ");
break;
}
}
I've also tried to organise my code with void command(){ ...};
//[...]//
void lcdPrint() {
lcd.setCursor(0,0);
switch (setting) {
case 0:
SecCount();
break;
case 1:
//[...]//
break;
//[...]//
}
}
//I try to assign SecCount() to this (v down here v) : //
void SecCount() {
lcd.setCursor(0, 1);
lcd.print(millis()/1000);
}
but didn't seem to work...
So if you want to give me tips and tricks on how to organisation of my code and other stuff, I will really appreciate!
Best regard.