We are engineering students using an Arduino mega2560 that came with the complete starter kit for a class project. We are creating a prototype for an automated parking system using a keypad to act as a pressure plate so that when a button is pressed, our stepper motor will open a gate, and the value on the LCD displaying the number of spots available will go down. We are having an issue with our LCD where it goes completely white after a second of the code running. The counter seems to be working in the serial monitor, but the LCD is not. The LCD works properly when we remove the keypad from the code and displays the proper decreasing values. Any help would be very much appreciated!
Here's the code:
#include <Keypad.h>
#include <Stepper.h>
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
const char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
}; //connect to the row pinouts of the keypad , switched pins 8 and 5 to have the pins on the arduino closer together as they are the only ones connected for testing with the button '1' in row 1, col 1.
byte colPins[COLS] = {8, 4, 3, 2 }; //connect to the column pinouts of the keypad
byte rowPins[ROWS] = {9, 5, 7, 6}; //connect to the row pinouts of the keypad , switched pins 8 and 5 to have the pins on the arduino closer together
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
const int stepsPerRevolution = 500; // steps per revolution of the motor
// initialize the stepper library on pins 8 through 11
Stepper myStepper(stepsPerRevolution, 10, 12, 11, 13);// did this so that the wires are not crossed when plugging into the arduino
LiquidCrystal lcd(2,3,4,5,6,7);
int i=10;
void setup(){
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.print("Available Spots ");
// Print message to LCD.
Serial.begin(9600);
}
void loop(){
lcd.setCursor(0,1);
// set the cursor to column 0, line 1
// (note: line 1 is the second row, counting begins with 0):
char key= keypad.getKey();
if (key && i>0)//added this to show that for any button pressed, as they're all mapped to the value 1. The stepper will not turn if the button has been pressed 10 times. -BJ (15/03/22)
{
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
//opens the gate upwards to an angle of approximately 90 deg.
//delay(5000); got rid of this for testing;
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
//delay(5000); got rid of this for testing;
i--;
lcd.print(i);// print the value of the spots available
lcd.print(" ");//gets rid of the 0 that hangs around after the number drops down to 9 after 10 on the lCD.
Serial.println(i);
}
}



