Ok I've cut down a version of the code which still fails. I've also tracked it down to a single line that causes it to fail.. marked with XXXX. If I comment this line out the program works... uncomment it and I get the behavior described.
I'll try post a schematic of the circuit.. but in a nut shell a 20x4 LCD 44870 controller, and a 4x4 matrix keypad.
Now attached...
// Include the LCD library
#include <LiquidCrystal.h>
// Define the pin that turns the LCD backlight on/off.
#define LCDBacklightPin 13
// Define the pins for the keypad connections.
int rowPin[4] = {9,10,11,12};
int colPin[4] = {5,6,7,8};
// Definition of global variables used throughout the program.
int GBL_userID = 0; // Currently logged in user.
boolean GBL_backLightIsOn = true; // Whether the backlight is on/off.
unsigned long GBL_timePrevious = 0; // Last time we checked this user's time.
int i = 0;
// Create a serial instance representing the LCD, used globally.
// SoftwareSerial LCD = SoftwareSerial(0, LCDPin);
// LiquidCrystal lcd(RS, EN, D4, D5, D6, D7)
// Initialize the library with the numbers of the LCD interface pins
LiquidCrystal myLCD (1, 2, 3, 4, A1, A0);
// One time setup & initialisation.
void setup()
{
// Serial.begin(9600);
// Initialise the pin used for the LCD backlight.
pinMode(LCDBacklightPin, OUTPUT);
// set up the LCD's number of columns and rows:
myLCD.begin(20, 4);
// Initialise the key pad.
for (int i = 0; i <=3; i++)
{
pinMode(colPin[i], OUTPUT);
digitalWrite(colPin[i], LOW);
pinMode(rowPin[i], INPUT);
}
lcdBacklight(true);
lcdClear();
lcdPosition(0,0);
lcdWriteString("setup done");
delay(1000);
}
// Main program loop.
void loop()
{
lcdClear();
lcdPosition(0,0);
lcdWriteString("loop start");
delay(1000);
// Prompt the user to log in.
T0_userLogin();
lcdClear();
lcdPosition(0,0);
lcdWriteString("loop end");
delay(1000);
}
// The first time after powering up we force the Admin to log in. This is to prevent someone disconnecting the power without being noticed.
// Routine to get a user from the login screen. Will not return to caller until a user is found.
void T0_userLogin()
{
char pin[5];
// Display the login prompt
lcdClear();
lcdPosition(0,0);
lcdWriteString("input screen");
delay(500);
for (i = 0; i <= 5; i++)
pin[i] = ' ';
// Get the PIN number from the user.
int result = getInput(2, 1, pin, 5, 5, true, 0, false);
// char keyPressed = getKey(5000,false);
}
// Routine to get write a value to the LCD, and allow it to be edited via the keypad.
int getInput(int row, int col, char before[], int minChar, int maxChar, boolean hidden, int alpha, boolean CRReq)
{
lcdCursor(true);
char keyPressed = getKey(5000, false);
return 0;
}
// Get a key from the keppad.
// If more than timeout milliseconds pass without a key being pressed then an error 'X' will be returned.
char getKey(int timeout, boolean switchBacklight)
{
// return 'X';
// Define the key values to be returned.
char key [4][4] = {{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}};
// Keep track of the start time.
long startTime = millis();
// Difference between the start time and the current time.
long duration;
while (true)
{
// Check to ensure the timeout value has not been reached. If it has we stop and return 'X'.
duration = millis() - startTime;
if (duration > timeout)
{
if(switchBacklight)
{
GBL_backLightIsOn = false;
lcdBacklight(false);
}
return 'X';
}
// Scan the keypad looking for a key press.
for (int i = 0; i <=3; i++)
{
digitalWrite(colPin[i], HIGH);
for (int j = 0; j <=3; j++)
{
if (digitalRead(rowPin[j]) == HIGH) // A key is being pressed.
{
unsigned long before = millis();
while (digitalRead(rowPin[j]) == HIGH); // Wait until the key is released.
delay(50);
if (!GBL_backLightIsOn)
{
GBL_backLightIsOn = true;
lcdBacklight(true);
}
digitalWrite(colPin[i], LOW);
return key[j][i];
}
}
digitalWrite(colPin[i], LOW);
}
}
}
// ------------------- LCD control routines ----------------------------------------------------------
// Routine to position the cursor within the display.
void lcdPosition(int row, int col)
{
myLCD.setCursor(col, row);
}
// Routine to turn the cursor on/off. Replaced with debug code
void lcdCursor (boolean on)
{
// The following line causes the sketch to fail.
lcdWriteString(" fails"); // XXXX
}
// Routine to write something to the LCD.
void lcdWriteString(char string[])
{
myLCD.write(string);
}
// Routine to write a single ASCII character.
void lcdWriteByte(byte string)
{
char myChar = string;
myLCD.write(myChar);
}
// Routine to clear the LCD screen.
void lcdClear()
{
myLCD.clear();
}
// Routine to turn the LCD backlight on/off.
void lcdBacklight(boolean on)
{
if (on)
{
GBL_backLightIsOn = true;
digitalWrite(LCDBacklightPin, HIGH);
}
else
{
GBL_backLightIsOn = false;
digitalWrite(LCDBacklightPin, LOW);
}
}
schematic.tiff (42.4 KB)