Hi I'm new to this programming lark and I'm struggling my way through. I'm more looking for tips than a direct answer.
I have a variable on an LCD screen which i want to increment with a push button. my variable starts at 0 But when i press the button the LCD screen shows 1111 then goes up in what seems like random thousands.
If anybody has any tips it would be much appreciated.
What it does is displays 3 variables that make up a time.
Then i can select each hour minute or seconds individually and use the up and down buttons to change (increment or decrease) the time.
Also on the minutes and seconds i want it to get to 60 then go to 0. But i think i can cope with that bit myself.
I'm sure the code can be written a lot more efficiently than i have done it but i have only been learning for a 4 days.
Ive only posted part of the code with the problem but there is enough to load on an arduino to see what i mean,
#include <LiquidCrystal.h>
#include <Menu.h>
#include <LCDMenu2.h>
#define _LCD_rows 2
#define _LCD_cols 16
#define _BUTTON_up 6
#define _BUTTON_down 4
#define _BUTTON_enter 5
#define _BUTTON_back 3
#define _BUTTON_prestat 0
#define _BUTTON_press_time 250
int restTimeH = 0;
int restTimeM = 0;
int restTimeS = 5;
int lapTimeH = 0;
int lapTimeM = 0;
int lapTimeS = 10;
int sets = 4;
///////////////////////////
int debouncetime = 80;
int tests = 1;
int hmtsel = 1;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void test()
{
lcd.setCursor(4, 1);
lcd.print(lapTimeH);
printDigits(lapTimeM);
printDigits(lapTimeS);
lcd.print(" ");
while (tests == 1)
{if (digitalRead(_BUTTON_up) == 1) {
delay(debouncetime);
if (digitalRead(_BUTTON_up) == 1){
if (hmtsel = 1) {
lapTimeH++;
delay(100);
lcd.setCursor(4, 1);
lcd.print(lapTimeH++);
}
if (hmtsel = 2) {
lapTimeM++;
lcd.print(lapTimeM);
}
if (hmtsel = 3) {lapTimeS++;}
lcd.print(lapTimeS);}
}
if (digitalRead(_BUTTON_down) == 1) {
delay(debouncetime);
if (digitalRead(_BUTTON_down) == 1){
}
}
if (digitalRead(_BUTTON_enter) == 1) {
delay(debouncetime);
if (digitalRead(_BUTTON_enter) == 1){
}
}
if (digitalRead(_BUTTON_back) == 1) {
delay(debouncetime);
if (digitalRead(_BUTTON_back) == 1){
}
} }
}
void printDigits(int digits) {
lcd.print(":");
if(digits < 10)
lcd.print("0");
lcd.print(digits);
}
void setup()
{
lcd.begin(_LCD_rows,_LCD_cols);
}
void loop()
{
test();
}
Thank you for your reply, i cant believe i missed that. But unfortunatly it has not solved my problem.
On the first click is shows 1100 then 3100,5100,7100,9100,11100,13100 etc as i click the button.
i only want it to go from 0 to 1 then 2 etc.
Sorry that was just a test and i left it in there.
The second lapTimeH should not have ++.
But that still does not sort the problem out.
I have also tried lcd.clear(); and that clears my screen then displays the same incorrect numbers.
Thank you for the tip about the auto format tool.
The whole project is a timer that counts down and fires a relay.
At the moment i'm building the menu type of system so i can change the hour min and seconds with the lcd and buttons, then once it is set it counts down from the newly set time.
Here is the code auto formated, thank you
#include <LiquidCrystal.h>
#include <Menu.h>
#include <LCDMenu2.h>
#define _LCD_rows 2
#define _LCD_cols 16
#define _BUTTON_up 6
#define _BUTTON_down 4
#define _BUTTON_enter 5
#define _BUTTON_back 3
#define _BUTTON_prestat 0
#define _BUTTON_press_time 250
int restTimeH = 0;
int restTimeM = 0;
int restTimeS = 5;
int lapTimeH = 0;
int lapTimeM = 0;
int lapTimeS = 10;
int sets = 4;
///////////////////////////
int debouncetime = 80;
int tests = 1;
int hmtsel = 1;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // Neuste Version, RW wird nicht mehr gebraucht
void test()
{
lcd.setCursor(4, 1);
lcd.print(lapTimeH);
printDigits(lapTimeM);
printDigits(lapTimeS);
lcd.print(" ");
while (tests == 1)
{
if (digitalRead(_BUTTON_up) == 1) {
delay(debouncetime);
if (digitalRead(_BUTTON_up) == 1){
if (hmtsel == 1) {
lapTimeH ++;
delay(100);
lcd.setCursor(4, 1);
lcd.print(lapTimeH);
}
if (hmtsel == 2) {
lapTimeM++;
lcd.print(lapTimeM);
}
if (hmtsel == 3) {
lapTimeS++;
}
lcd.print(lapTimeS);
}
}
if (digitalRead(_BUTTON_down) == 1) {
delay(debouncetime);
if (digitalRead(_BUTTON_down) == 1){
}
}
if (digitalRead(_BUTTON_enter) == 1) {
delay(debouncetime);
if (digitalRead(_BUTTON_enter) == 1){
}
}
if (digitalRead(_BUTTON_back) == 1) {
delay(debouncetime);
if (digitalRead(_BUTTON_back) == 1){
}
}
}
}
void printDigits(int digits) {
lcd.print(":");
if(digits < 10)
lcd.print("0");
lcd.print(digits);
}
void setup()
{
lcd.begin(_LCD_rows,_LCD_cols);
}
void loop()
{
test();
}
I tried some serial debuging and the serial was saying the variable was 1 with one click and 2 with another and 3 with another but on the lcd it was displaying what i have said previously.
Im very stumped.
Here is the code with the debugging, try it for yourself its well wierd. My Serial output increments by one each time (lovely) but on the lcd its displaying the previous numbers said in the before post.
#include <LiquidCrystal.h>
#include <Menu.h>
#include <LCDMenu2.h>
#define _LCD_rows 2
#define _LCD_cols 16
#define _BUTTON_up 6
#define _BUTTON_down 4
#define _BUTTON_enter 5
#define _BUTTON_back 3
#define _BUTTON_prestat 0
#define _BUTTON_press_time 250
int restTimeH = 0;
int restTimeM = 0;
int restTimeS = 5;
int lapTimeH = 0;
int lapTimeM = 0;
int lapTimeS = 10;
int sets = 4;
///////////////////////////
int debouncetime = 80;
int tests = 1;
int hmtsel = 1;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // Neuste Version, RW wird nicht mehr gebraucht
void test()
{
lcd.setCursor(4, 1);
lcd.print(lapTimeH);
printDigits(lapTimeM);
printDigits(lapTimeS);
lcd.print(" ");
while (tests == 1)
{
if (digitalRead(_BUTTON_up) == 1) {
delay(debouncetime);
if (digitalRead(_BUTTON_up) == 1){
if (hmtsel == 1) {
lapTimeH ++;
delay(100);
lcd.setCursor(4, 1);
lcd.print(lapTimeH);
Serial.print(lapTimeH);
}
if (hmtsel == 2) {
lapTimeM++;
lcd.print(lapTimeM);
}
if (hmtsel == 3) {
lapTimeS++;
}
lcd.print(lapTimeS);
}
}
if (digitalRead(_BUTTON_down) == 1) {
delay(debouncetime);
if (digitalRead(_BUTTON_down) == 1){
}
}
if (digitalRead(_BUTTON_enter) == 1) {
delay(debouncetime);
if (digitalRead(_BUTTON_enter) == 1){
}
}
if (digitalRead(_BUTTON_back) == 1) {
delay(debouncetime);
if (digitalRead(_BUTTON_back) == 1){
}
}
}
}
void printDigits(int digits) {
lcd.print(":");
if(digits < 10)
lcd.print("0");
lcd.print(digits);
}
void setup()
{
lcd.begin(_LCD_rows,_LCD_cols);
Serial.begin(9600);
Serial.println("DEBUG:");
Serial.println("================");
}
void loop()
{
test();
}
Haven't studied the code much, but something just stood out in this section :
if (digitalRead(_BUTTON_up) == 1){
if (hmtsel == 1) {
lapTimeH ++;
delay(100);
lcd.setCursor(4, 1);
lcd.print(lapTimeH);
Serial.print(lapTimeH);
}
if (hmtsel == 2) {
lapTimeM++;
lcd.print(lapTimeM);
}
if (hmtsel == 3) {
lapTimeS++;
}
lcd.print(lapTimeS);
}
Why is the lcd.print(lapTimeS); outside the if (hmtsel == 3) { loop, but the lcd.print(lapTimeM);and the lcd.print(lapTimeH); are inside the 'IF' loops ?
I have lost count of the number of times over the last 15 years that I have stared at a piece of code for hours and failed to see a really simple and obvious mistake.
An author is the last one to see his own errors. Years ago in the print industry we learnt (usually the hard way) never to proof read our own work.