I'm a beginner and I would like to do a project that it uses 5 buttons to type ABC, numbers, change col, change ln, and reset. Im here asking why I cant Serial.print out the digitalRead(5) and why theres nothing on my lcd.
Im using LCD 1602, UNO, and 5 6pins buttons.
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// define pins for buttons
#define BUTTON_1 5
#define BUTTON_2 7
#define BUTTON_COL 9
#define BUTTON_LN 11
#define BUTTON_RESET 13
LiquidCrystal_I2C lcd(0x27, 16, 2);
// define variables for tracking cursor position
int col = 0;
int ln = 0;
void setup() {
// set up input pins for buttons
lcd.init();
lcd.backlight();
pinMode(BUTTON_1, INPUT_PULLUP);
pinMode(BUTTON_2, INPUT_PULLUP);
pinMode(BUTTON_COL, INPUT_PULLUP);
pinMode(BUTTON_LN, INPUT_PULLUP);
pinMode(BUTTON_RESET, INPUT_PULLUP);
lcd.clear();
Serial.begin(9600);
}
void loop() {
int i=1;
int A=65;
int N=48;
char ABC = i*A;
char NUM = i*N;
if (A > 90) {
A = 65;
}
if (N > 57) {
N = 48;
}
// check for button presses and perform actions accordingly
if (digitalRead(BUTTON_1) == LOW) {
// send ASCII code for 'A' with Serial.println() function
lcd.write(ABC);
A++;
}
if (digitalRead(BUTTON_2) == LOW) {
// send ASCII code for '0' with Serial.println() function
lcd.write(NUM);
N++;
}
if (digitalRead(BUTTON_COL) == LOW) {
ln++;
}
if (digitalRead(BUTTON_LN) == LOW) {
ln++;
}
if (digitalRead(BUTTON_RESET) == LOW) {
col = 0;
ln = 0;
lcd.clear();
}
lcd.setCursor(col, ln);
// wait for 100 milliseconds before checking button states again
delay(100);
Serial.println(digitalRead(5));
}
Those if() statements can never be true. Every time loop() starts, A and N are reset since they are local variables. Did you mean to maybe make them global variables so they retain their value from call to call of loop()?
When you have variables inside a function they are only available for that function. In this case, you're also declaring those variables which resets them every loop. Put them near your BUTTON definitions at the top, outside of any functions to make them globally available and not resetting every loop cycle.
by making them global, like I suggested? Put those declarations, up near the top of your code, outside of any functions, just like your col and ln variables.