Using the displayed number on an LCD I2C counter as an input variable in my code

Hi all,

First time poster but will attempt to be clear and concise.
I am trying to create an increment/decrement counter, the output of which will be used to run through a loop a set number of times (corresponding to the number on the lcd display) i.e. if the lcd display/counter displays 3, the loop will run 3 times.

I have done a bunch of reading on how to accomplish this, and am currently looking at the wire.h library commands. What i've gathered so far has to happen is that the arduino must request a set number of bytes from the I2C and convert these to an int to be used as a variable in the code.

My kit consists of a Mega with the I2C LCD connected to gnd, vcc and pins 20, 21 (sda and scl), along with a push button circuit (ie pulldown resistor layout) with a button that increases the count, one that decreases, and one to be used as a select button when the counter reads the desired value (though I have not yet programmed this button).

Basically when the select button is pressed I would like the number on the display to be used in the code as an input variable. I'm not massively experienced with the I2c bus and its relevant commands but I would be very grateful for any pointers both with the code and with anything that would be educational regarding my problem.

My code is:

#include <Wire.h>

#include <LiquidCrystal_I2C.h>


const int upButton = 2;
const int downButton = 3;
const int selectButton; //to be included


// Variables will change:


int buttonPushCounter = 0;   // counter for the number of button presses
int upButtonState = 0;         // current state of the button
int downButtonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button


void setup() {

  // initialize the button pin as a input:
   
   pinMode(upButton, INPUT);
   
   pinMode(downButton, INPUT);
   
   lcd.begin();
   
   lcd.setCursor(2,0);
   
   lcd.print("Layer Number");
   
   Serial.begin(9600);
}


void loop() {


   // read the pushbutton up input pin:
   
   upButtonState = digitalRead(upButton);
   delay(100);
  // compare the buttonState to its previous state

   if (upButtonState != lastButtonState) {
     
     // if the state has changed, increment the counter
     
     if (upButtonState == HIGH)
     
     {
       
      buttonPushCounter++;
      lcd.setCursor(7,1);
           
      lcd.print(buttonPushCounter);
     }
     
     

   }
   // save the current state as the last state,
  //for next time through the loop
   lastButtonState = upButtonState;
   
     // read the pushbutton down input pin:
   
   downButtonState = digitalRead(downButton);
    delay(100);
  // compare the buttonState to its previous state

   if (downButtonState != lastButtonState) {
     
     // if the state has changed, decrement the counter
     
     if (downButtonState == HIGH)
     
     {
       
      buttonPushCounter--;
      lcd.setCursor(7,1);
         
      lcd.print(buttonPushCounter);
     }
     
             
   }
   // save the current state as the last state,
  //for next time through the loop
   lastButtonState = downButtonState;
   
  if (buttonPushCounter <= 1){

      buttonPushCounter = 1;
      }
}

NervousGiraffe98:
I have done a bunch of reading on how to accomplish this, and am currently looking at the wire.h library commands. What i've gathered so far has to happen is that the arduino must request a set number of bytes from the I2C and convert these to an int to be used as a variable in the code.

No need to go to all that trouble: your code has to tell the LCD what number to display, so just keep track of what you told it to do.

In fact, it looks as though buttonPushCounter is already doing that very thing.

Good god, the variable is just 'buttonPushCounter' isn't it? Whichever number is displayed on the LCD is the value that that variable takes, right? Ah i'm afraid the lack of numerical feedback had thrown me, i'm still getting used to thinking in Arduino!

Thanks for the help :slight_smile:

Of course - you tell the display what to show, so you can use the same information to do other things.