I would like to ask whats wrong with this statement

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));
}

What makes you think that?

(It's best to read the pin once per iteration of the loop function, and use that value throughout)

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()?

To Arduino or programming in general?

Thanks for responding. Yes, that’s what Im thinking about! How should I fix it?

I’ll say I a beginner for all languages

I’m trying to use it to figure out if it’s working

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.

Thanks for the correction, I’ll put them in setup

No, up further.

#include <LiquidCrystal_I2C.h>
#include <Wire.h>

UP HERE ******************

// 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();

Perhaps you want to add i? Instead of multiply by i?

IDK, not my code. I was trying to be specific about where the global variables go and how they're different.

I want to ask what’s the difference?

Please reread post 8. If you still don't understand I would recommend you research variables and functions in C programming.

Regards.

Ok, thanks for your help. I appreciate it.

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.

Nothing on the display, checks the connections..
And yes, you can print the digitalRead, I commented it out as it was too fast..

Your project in a sim..

good luck.. ~q

2 Likes

I didn't know about that. Cool.

1 Like

Got it. Thanks

Thanks. I’ll check it.