Help with code tweak for button activated text on lcd

I have an lcd with button coded for sequential numbers when pressed. What I want, is for a press of the button to give me a number. Specifically like this: (press = "1"), (press = "1"), (press = "2"), (press = "3"), (press = "5"), (press = "8"), then reset to "1". This is my second ever build and I'l brand new to this. Any help is greatly appreciated!

This is the code I have working.

#include <LiquidCrystal.h>
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
int a = 0;
int e = 8;

void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Knock the number");
pinMode(e, INPUT);
}

void loop() {
int button = digitalRead(e);

lcd.setCursor(7, 1);
lcd.print(a);

if (button == HIGH) {
a ++;
lcd.setCursor(7, 1);
lcd.print(a);
delay(1000);
}
}

I'm using an Uno.

Using pin 1 is not good. Pin 0 and 1 are used when downloading code. They are also used for Serial Monitor, a very good tool for debugging.

Nice picture.

Have a look att "code tags", the symbol up to the left in the Quick Reply window. It makes Your code appear like this:

#include <LiquidCrystal.h>
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
int a = 0;
int e = 8;

void setup() {
 // set up the LCD's number of columns and rows:
 lcd.begin(16, 2);
 // Print a message to the LCD.
 lcd.print("Knock the number");
 pinMode(e, INPUT);
}

void loop() {
 int button = digitalRead(e);


 lcd.setCursor(7, 1);
 lcd.print(a);


 if (button == HIGH) {
   a ++;
   lcd.setCursor(7, 1);
   lcd.print(a);
   delay(1000);
 }
}

Maybe just compare logically if (NUMBER < 9 && NUMBER != 4 && NUMBER != 6 && NUMBER != 7)

Thank you both. Through some copying and pasting I figured it out. I have no idea how to code.

#include <LiquidCrystal.h>

const byte buttonPin = 8;

int buttonPushCounter = 0; // counter for the number of button presses
boolean buttonState = LOW; // current state of the button
boolean lastButtonState = LOW; // previous state of the button

// lcd constructor made global in scope so the whole program can sse it
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);

void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Knock the number");
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);

}

void loop()
{
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState)
{
if (buttonState == HIGH)
{
// if the current state is HIGH then the button
// went from off to on:
buttonPushCounter++; // add one to counter
lcd.clear();
if (buttonPushCounter > 7) // if couter over 3 reset the counter to 1 to show "Jon"
// and not "Hello All"
{
buttonPushCounter = 1;
}
Serial.println(buttonPushCounter);
switch (buttonPushCounter) // choose what to display based on buttonPushCounter value
{
case 0:
lcd.print("Hello All"); // show "Hello All until first button press
break;
case 1:
lcd.setCursor(7, 1);
lcd.print("1");
break;
case 2:
lcd.setCursor(3, 0);
lcd.print("1");
break;
case 3:
lcd.setCursor(7, 1);
lcd.print("2");
break;
case 4:
lcd.setCursor(10, 0);
lcd.print("3");
break;
case 5:
lcd.print("5");
break;
case 6:
lcd.setCursor(15, 1);
lcd.print("8");
break;
case 7:
lcd.print("Knock the number");
break;
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
}

You didn't notice the way to show code given in reply #2. Using the Autoformat function in the IDE before copying makes it even better.

Thank you. I'll try that next time.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.