I have made a part counter using an IR sensor, 4x4 keypad and an lcd screen. It works by incrementing "count" and displaying the value on the lcd. It works fine as it is but I would like a way to manually input numbers onto the screen that could then be incremented. Right now my "C" and "D" button will increase or decrease the count if I need it to, I just think it would be nice to have a feature where I can type it in on the number pad. When I tried doing this before my increment would ignore the value I typed in. Is there a way I can impliment this feature? I am pretty new to arduino. This is my code right now. Thanks to everyone for taking the time to read and have an amazing day.
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Keypad.h> //libraries
static int count = 0; //variable used as first number
unsigned long lastTime = millis();
unsigned long wait = 1000;
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
const byte ROWS = 4;
const byte COLS = 4;
bool blinking = false;
int buttonState=0;
int lastButtonState=0;
int buttonPushCounter =0;
int memory;
void partLoc(){
lcd.setCursor(7,1);
}
void fabLoc(){
lcd.setCursor(11,0);
}
void clearScreen(){
lcd.print(" ");
}
void countUp(){
count++;
partLoc();
clearScreen();
partLoc();
lcd.print(count);
for (int i = 0; i <= 16; i++) {
blinking=false;
lcd.setCursor(i,1);
}
tone(52,2200);
delay(100);
noTone(52);
}
void countDown(){
if(count!=0){
count--;
partLoc();
clearScreen();
partLoc();
lcd.print(count);
partLoc();
lcd.print(count);
for (int i = 0; i <= 16; i++) {
blinking=false;
lcd.setCursor(i,1);
}
tone(52,2100);
delay(100);
noTone(52);
}
}
char hexaKeys [ROWS][COLS] ={
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup()
{
lcd.init();
lcd.backlight();
pinMode (52,OUTPUT);
pinMode(51, OUTPUT);
Serial.begin(9600);
pinMode(13,INPUT);
lcd.begin(16, 2);
delay(500);
lcd.print("Fab Cell #");
lcd.setCursor(0,1);
delay(500);
lcd.print("Part #");
lcd.setCursor(11,0);
lcd.blink();
}
void loop(){
int sensor = digitalRead(53);
unsigned long timeNow = millis();
buttonState = digitalRead(53);
if (buttonState != lastButtonState) {
if (buttonState == 1) {
buttonPushCounter++;
} else {
//Serial.println("off");
if (timeNow - lastTime > wait){ // adds delay after the count up but allows for input during the delay. delay will not stop the rest of the program from running
countUp();
Serial.println(count);
lastTime=timeNow;
}
}
lastButtonState = buttonState;
}
char customKey = customKeypad.getKey();
if (blinking){
lcd.blink();
}
//Switchcase Statements
switch (customKey){
case 'C':
countUp();
Serial.println(count);
break;
case 'D':
countDown();
Serial.println(count);
break;
case 'B':
fabLoc();
clearScreen();
fabLoc();
blinking = true;
tone(52,2200);
delay(100);
noTone(52);
break;
}
// Allows button input from buttons 0 - 9
if ((customKey>='0') && (customKey <= '9')){
lcd.print(customKey);
for (int i = 0; i <= 16; i++) {
blinking=false;
lcd.setCursor(i,1);
}
tone(52,2000);
delay(100);
noTone(52);
}
//When A is pressed displays the most recently cleared number
if (customKey=='A'){
for (int i = 0; i <= 16; i++) {
blinking=false;
lcd.setCursor(i,0);
}
Serial.println(memory);
lcd.setCursor(13,1);
lcd.print(memory);
lcd.setCursor(18,0);
delay(3000);
lcd.setCursor(13,1);
lcd.print(" ");
}
//When # is pressed the count resets
if (customKey=='#'){
memory = count; //stores the cleared value
count=0;
Serial.println("reset");
Serial.println(count);
lcd.setCursor(6,1);
lcd.print(" ");
partLoc();
lcd.print(count);
for (int i = 0; i <= 16; i++) {
blinking=false;
lcd.setCursor(i,1);
}
tone(52,1200);
delay(100);
noTone(52);
}
if (customKey=='*'){
memory = count; //stores the cleared value
fabLoc;
lcd.print(" ");
fabLoc();
blinking = true;
}
}