Triple Counter for Personal Project

Good morning.

I am extremely new to coding with Arduino and have run into a problem.

I am attempting to use a DFRobot LCD Keypad Shield with an Arduino Mega 2560.
The ultimate goal is to have three counters on one screen. I would like to be able to use the “LEFT” and “RIGHT” buttons on the shield to move the cursor between the three different counters and use the “UP” and “DOWN” buttons to increment/decrease the displayed value of the selected counters.

Currently, the issue I have run into is trying to get the cursor to move only to selected positions on the line in which the counters are located. It seems that every time I think I have solved the issue, the code gives me a new error code. Any help would be greatly appreciated.

The code that I have is as follows:


// The goal of this project is to utilize the LCD shield to display three counters. The “LEFT” and
// “RIGHT” buttons should allow user to scroll between counters while the “UP” and “DOWN” buttons
// increase or decrease the displayed number.

// include the Liquid Crystal library code
#include <LiquidCrystal.h>

// Initialize the library with interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// Variables that change for counters
int CPCounter = 0; // Counter for Command Points
int VPCounter = 0; // Counter for Victory Points
int TNCounter = 0; // Counter for Turns

//keypad button variables
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;

#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5

int read_LCD_buttons() // read the buttons
{
adc_key_in = analogRead(0); // read the value from the sensor

// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
// We make this the 1st option for speed reasons since it will be the most likely result

if (adc_key_in > 1000) return btnNONE;

// For V1.1 us this threshold
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
return btnNONE; // when all others fail, return this.
}

void setup()
{
// Set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
lcd.clear();

// Print first line of static words
lcd.setCursor(1, 0);
lcd.print(“WARHAMMER 40K”);

// Print second line of static words
lcd.setCursor(1,1);
lcd.print(“CP”);
lcd.setCursor(7,1);
lcd.print(“VP”);
lcd.setCursor(13,1);
lcd.print(“T”);

Serial.begin(9600);

lcd.setCursor(4,1);
} // End Void Setup

void loop()
{
lcd_key = read_LCD_buttons();
switch (lcd_key)
{
case btnRIGHT:
{
lcd.setCursor(10,1);
if (lcd.cursor()==10,1)
{
lcd.setCursor(14,1);
}
}
}
} // End Void Loop


Again, I greatly appreciate any and all help as I am extremely new to this.

Please edit your post to have code tags, see "How to use the forum".

Next, have a look at the state change example in the IDE. It does apply here. You want to move the cursor when the button becomes pressed, not when it is pressed. Not you do the same action every time you call read_LCD_buttons() and the Arduino is fast and will call it many many times.

Linx_Constantine:

          lcd.setCursor(10,1);

if (lcd.cursor()==10,1)

What is that second line suppose to do? Aka, don't make up your own functions :wink:

Next, try and actually print three different variables on the screen to be later changed. Now I only see some labels.

Linx_Constantine:

int read_LCD_buttons() // read the buttons
 // End Void Setup
 // End Void Loop

Those comments are basically useless, and useless comments make the code more - not less - difficult to read. You didn’t after all, put a similar comment on the end of read_LCD_buttons(). :roll_eyes:

Also, " Void Loop" is not the name. The name is simply “loop()”. “void” (no capitals) is a descriptor indication that the function returns no information as a function.

If you have used “Auto Indent” - Ctrl-T in the IDE - and you have posted the code using code tags as we explain (below) then it is quite easy to see functions begin and end.

Standard advice follows:


You need to go and read the forum instructions so that you can go back and modify your original posts (not re-post them) - using the “More → Modify” option below the right hand corner of your post - to mark up your code as such using the “</>” icon in the posting window. Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.

In fact, the IDE has a “copy for forum” link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don’t forget to use the “Auto-Format” (Ctrl-T) option first to make it easy to read. If you do not post it as “code” it can as you now see, be quite garbled and is always more difficult to read.

It is inappropriate to attach it as a “.ino” file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only between complete functional blocks.