2

I am trying to make a random number generator that when a button is pressed will display a random number on a 16x2 LCD screen, but the button output always equals 1 even when the button is not being pressed.

Image of the setup not running:

enter image description here

Image of the setup running (NOTE: the button seems to do nothing when pressed):

enter image description here

The code:

#include <Adafruit_LiquidCrystal.h>
long randNumber;
int seconds = 0;
int buttonState = 2;
const int ledPin = 13;

Adafruit_LiquidCrystal lcd_1(0);

void setup() {
  pinMode(buttonState,INPUT_PULLUP);
  pinMode(ledPin,OUTPUT);
  lcd_1.begin(16, 2);
  lcd_1.print("hello world");
  randomSeed(analogRead(0));
}

void loop() {
  delay(500);
  buttonState = digitalRead(0);
  digitalWrite(ledPin, !digitalRead(buttonState));
  delay(250);
  if (buttonState == 1) {
    randNumber = random(0,9 + 1);
    lcd_1.setCursor(0, 1);
    lcd_1.print(randNumber);
    lcd_1.print(buttonState);
    lcd_1.setBacklight(1);
    delay(500);
  } else {
    lcd_1.setBacklight(0);
    lcd_1.setCursor(0, 1);
    lcd_1.print(buttonState);
    delay(500);
  }
}
ocrdu
  • 2,172
  • 6
  • 15
  • 22

0 Answers0