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