I am making a vending machine for a school project and the main components I am using are a coin acceptor, keypad, 16x2 LCD, and dc motors. So basically the code goes like this: a coin is detected from coin acceptor, when the target amount is reached then it would allow the person to pick the snack. Then the motor dispenses the snack. My problem is the LCD and when the target amount is hit I want it to show what item the person chose(A1, A2, B1, B2), but instead it goes to that screen and the second row(where the keypad input should show up) the first column does not show up. So when the person hits A1 the LCD would show " 1", there is a space before the 1.
The purpose of the clearSecondRow function is to clear the second row from the previous message presented there. I do believe this plays part of my problem but I have no idea how to do the clearing of the second row another way.
Here is the full code for my vending machine(to clearly show the part of the code I needed help with I had to post the whole entire code):
//Version 3.0
#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_NeoPixel.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1,0, 4, 5, 6, 7, 3, POSITIVE);
// Start of Keypad and Motor Constants and Variables
#define pLength 3
char demo[pLength];
char aOne[pLength];
char aTwo[pLength];
char bOne[pLength];
char bTwo[pLength];
char aOneMaster[pLength] = "A1";
char aTwoMaster[pLength] = "A2";
char bOneMaster[pLength] = "B1";
char bTwoMaster[pLength] = "B2";
byte data_count = 0, master_count = 0;
int aOneMotor = 10;
int aTwoMotor = 11;
int bOneMotor = 12;
int bTwoMotor = 13;
int greenLED, yellowLED; //Test LEDs
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {50, 51, 52, 53}; //Pinouts for the row of the Keypad
byte colPins[COLS] = {36, 37, 38, 39}; //Pinouts for the column of the Keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// End of Keypad and Motor
//Start of Coin Acceptor Constants and Variables
const int coinpin = 2;
const int targetcents = 10;
const float lcdTargetCents = 0.10;
volatile int cents = 0;
volatile float lcdCents = 0.00;
int credits = 0;
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
lcd.backlight();
//Motor Pins and Test LED
pinMode(aOneMotor, OUTPUT);
pinMode(aTwoMotor, OUTPUT);
pinMode(bOneMotor, OUTPUT);
pinMode(bTwoMotor, OUTPUT);
greenLED = aOneMotor; //Test LED
yellowLED = aTwoMotor; //Test LED
//Coin Acceptor Pins and Initial Interrupt
pinMode(coinpin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
}
void loop() {
if (cents >= targetcents) {
credits = credits + 1;
cents = cents - targetcents;
}
else{
//Keep Waiting...
}
//Debugging Zone
Serial.println(cents);
Serial.println(credits);
lcd.setCursor(0,0);
lcd.print("Insert Money");
lcd.setCursor(0, 1);
lcd.print(lcdCents);
delay(15);
//Keypad and Motor Code
while (credits > 0){
lcd.setCursor(0,0);
lcd.print("Choose Snack:");
char keyPressed = keypad.getKey();
clearSecondRow();
if (keyPressed){
demo[data_count] = keyPressed;
aOne[data_count] = keyPressed;
aTwo[data_count] = keyPressed;
bOne[data_count] = keyPressed;
bTwo[data_count] = keyPressed;
Serial.println(demo[data_count]);
lcd.setCursor(data_count,1);
lcd.print(demo[data_count]);
data_count++;
}
if(data_count == pLength-1){
delay(500);
lcd.clear();
if(!strcmp(aOne, aOneMaster)){
Serial.println("A1 Triggered");
lcd.setCursor(0,0);
lcd.print("Vending...");
digitalWrite(aOneMotor, HIGH);
delay(1000);
digitalWrite(aOneMotor, LOW);
}
else if(!strcmp(aTwo, aTwoMaster)){
Serial.println("A2 Triggered");
digitalWrite(aTwoMotor, HIGH);
delay(5000);
digitalWrite(aTwoMotor, LOW);
}
else if(!strcmp(bOne, bOneMaster)){
Serial.println("B1 Triggered");
digitalWrite(bOneMotor, HIGH);
delay(5000);
digitalWrite(bOneMotor, LOW);
}
else if(!strcmp(bTwo, bTwoMaster)){
Serial.println("B2 Triggered");
digitalWrite(bTwoMotor, HIGH);
delay(5000);
digitalWrite(bTwoMotor, LOW);
}
else{
Serial.println("ERROR NOT DEFINED");
lcd.print("OUT OF STOCK");
delay(2500);
}
lcd.clear();
clearAllData();
}
}
}
//Interrupt Function: when coin is enter this funtion is triggered
void coinInterrupt(){
// Each time a pulse is sent from the coin acceptor, interrupt main loop to add 1 cent and flip on the LED
cents = cents + 1;
lcdCents = lcdCents + 0.01;
}
//Function to clear the input data from keypad
void clearAllData(){
while(data_count !=0){
credits = 0;
lcdCents = 0.00;
aOne[data_count--] = 0;
aTwo[data_count--] = 0;
Serial.println("All Data Cleared");
}
return;
}
void clearSecondRow(){
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(1,1);
lcd.print(" ");
lcd.setCursor(2,1);
lcd.print(" ");
lcd.setCursor(3,1);
lcd.print(" ");
}
P.S. - I did not include any wiring diagrams or pictures because I truly believe it is just a programming issue.