Hey you all! First of all, thank you for this Forum, I have already found a great amount of help by reading other people's posts on here. I just got my Arduino a few days ago. I have very little programming experience, but I'm hooked already.
Here's what I'm having problems with.
My idea is to write a little program where I move an ASCII character (For now, the character "O") on an LCD Display using a Joystick.
I have hooked up a display (LCD 1602 Module) and a small Analog Joystick to my Board.
I know this probably isn't the most efficient or smartest way to do it, but my idea so far is to add and substract from the X and Y values of the cursor positions depending on the analog input from the joystick.
This is what the code looks like:
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = A0; // analog pin connected to X output
const int Y_pin = A1; // analog pin connected to Y output
int col = 0 ;
int row = 0 ;
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
Serial.begin(9600);
}
void loop()
{
lcd.setCursor(col, row); // Setting up the cursor
// Move the "0" from left to right
int YValue = analogRead(Y_pin); // Reading the Y Pin of the Joystick
// Move Right
if(YValue > 1000) // If the Y-Pin's output is larger than 1000
{
lcd.setCursor(col++, row); // Move cursor 1 column right
lcd.print("O"); // Print O at the new location
}
// Move Left
else if(YValue < 10) // If the Y-Pin's output is smaller than 10
{
lcd.setCursor(col--, row); // move cursor 1 column left
lcd.print("O"); // Print O at the new location
}
else // otherwise, do nothing, just print the O
{
lcd.setCursor(col, row);
lcd.print("O");
}
// Move Up and Down
int XValue = analogRead(X_pin);
// Unten
if(XValue > 1000)
{
lcd.setCursor(col, row++);
lcd.print("O");
}
// Oben
else if(XValue < 10)
{
lcd.setCursor(col, row--);
lcd.print("O");
}
else
{
lcd.setCursor(col, row);
lcd.print("O");
}
delay(100);
lcd.clear();
}
This kinda works, and I was super excited.
What happens now is that the cursor is able to leave the bounds of the screen because col and row aren't constrained, so if I move the joystick down when the "O" is already on the lower of the two rows, the program just mindlessly keeps trying to move it further down.
In that case, the LCD display keeps the cursor where it is, I assume this is something that is built into the LiquidCrystal.h library.
The first idea I had was to add another condition to moving the cursor, that is, by modifying the code like this:
int XValue = analogRead(X_pin);
// Move Down
if((XValue > 1000) && (row<=1))
{
lcd.setCursor(col, row++);
lcd.print("O");
}
// Move Up
else if((XValue < 10) && (row>=0))
{
lcd.setCursor(col, row--);
lcd.print("O");
}
else
{
lcd.setCursor(col, row);
lcd.print("O");
}
For some reason, this does not solve the problem. The cursor still goes into the negatives, and after the while resets to position 0,0.
Then, I tried googling a bit and found the constrain() function, which seemed perfectly suitable.
So, I added this to void (setup):
void setup() {
// Limit Range of col and row
constrain(col, 0, 15);
constrain(row, 0,1);
Unfourtunately, this also doesn't work, as the Serial Monitor shows that the values are still going beyond the range of the LCD display's two rows and 16 columns.
What would be the correct way to solve this issue?
Thanks a bunch in advance, can't wait to explore this stuff further!
Good Night
Matt