Hi, I am trying to construct a sort of keyboard with a standard lcd and an Arduino nano. Using a joystick to move the cursor, I want to push the button, and on a specific cursor location it will print a letter. I could not figure out how to program the Arduino, but went out on a limb and tried a program that went like this,
int ROW = 2;
int COL = 7;
if(lcd.setCursor(1,1) & buttonstate == HIGH){
COL = COL +1;
lcd.setCursor(COL, ROW);
lcd.print(“a”);
}
Obviously this did not work, and I can't figure out how to achieve what I want to program, and if it is even possible. So how do I do this?
In the future, please do not post code fragments, it should be a complete program so others can compile and work with it. You also need to surround your code with [code][/code] tags. Use the </> icon or type them in.
Hi, I am trying to make a keyboard on an LCD by attaching a joystick that changes the location of the cursor. If I press a button, the character the cursor is on will be printed on the screen. My program is not completed, but when testing it my program had an error. How will I write this sketch?
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 a= 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int numRows = 2;
const int numCols = 16;
int ROW = 2;
int COL = 7;
int col = 0;
int row = 0;
int xPin = A1;
int yPin = A0;
int buttonPin = 7;
int xPosition = 0;
int yPosition = 0;
int buttonState = 0;
void setup() {
// put your setup code here, to run once:
lcd.begin(numCols, numRows);
Serial.begin(9600);
lcd.print("abcdefghijklmnop");
lcd.setCursor(0,2);
lcd.print("qrstuvwxyz");
lcd.setCursor(col,row);
lcd.blink();
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
xPosition = analogRead(xPin);
yPosition = analogRead(yPin);
buttonState = digitalRead(buttonPin);
if((yPosition) > 1000){
int row = row + 1;
lcd.setCursor(col, row);
lcd.blink();
}
if((yPosition) < 20) {
int row = row - 1;
lcd.setCursor(col,row);
lcd.blink();
}
if(lcd.setCursor(1,1) & buttonstate == HIGH){
COL = COL +1;
lcd.setCursor(COL, ROW);
lcd.print(“a”);
}
Serial.print("X: ");
Serial.print(xPosition);
Serial.print(" | Y: ");
Serial.print(yPosition);
Serial.print(" | Button: ");
Serial.println(buttonState);
delay(100);
}
Moderator edit: </mark> <mark>[code]</mark> <mark>
const int rs = 12, en = 11, d4 = 5, d5 a = 4, d6 = 3, d7 = 2;
C/C++ is case sensitive so buttonState is not the same as buttonstate
Further lcd.setCursor() does not return anything so having it as part of a condition in e.g. an if statement (as below from your code) does not make sense.
if (lcd.setCursor(1, 1) & buttonstate == HIGH) {
Lastly, a single & is a binary AND; in conditions, you want to use a logical AND which is a double & (&&).
Thank you for the information, I did not think that the full code was necessary, but you are right, it is good practice. I changed the operator from bitwise to boolean, and found it was a problem, but not the one addressed by the compiler. I tried a different code that only acted if the cursor was in position. This also did not work, and I still cannot find answers to how to write an if statement that concerns cursor placement.
I am working on a project that involves using a joystick to move the cursor to different locations, and when a button is pressed, the character the cursor is over will be printed on the lcd. I have written most of the code, but have hit a fundamental roadblock. I am trying to write an if statement that states if lcd.setCursor(1,1), print something. I have not been able to find info on this, and want to know if my idea is remotely feasible, or if I need to scratch my entire design.