...
#define DEBUGSERIALTXPIN 8
#define DEBUGSERIALRXPIN -1
#define SDCHIPSELECT 9
#define FILENAME "Passwords1.txt"
#define NUMBEROFPASSWORDS 100
#define PASSWORDLENGTH 8
/* GLOBALS */
unsigned long LineNumber = 0;
char password [NUMBEROFPASSWORDS][PASSWORDLENGTH] = {};
int nPassword = 0;
// I2C LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set LCD address to 0x27 and 20 chars wide by 2 rows tall
void setup()
{
// Disable the Ethernet SPI
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
// Serial ports
Serial.begin(19200);
Serial1.begin(19200);
Serial2.begin(19200);
Serial3.begin(19200);
// Software Serial for Debug messages
SoftwareSerial debugSerial(DEBUGSERIALRXPIN, DEBUGSERIALTXPIN); // RX, TX
debugSerial.begin(9600);
debugSerial.print("Software Serial TX ready on pin = ");
debugSerial.println(DEBUGSERIALTXPIN);
// LCD Init
lcd.init();
lcd.backlight();
lcd.setCursor(1, 0);
lcd.print("LCD Initialized");
lcd.setCursor(1, 1);
// Initialize SD Card
if (!SD.begin(SDCHIPSELECT))
{
lcd.clear();
lcd.print("SD Card Error!");
debugSerial.println("initialization failed. Things to check:");
debugSerial.println("1. is a card inserted?");
debugSerial.println("2. is your wiring correct?");
debugSerial.println("3. did you change the chipSelect pin to match your shield or module?");
debugSerial.println("Note: press reset or reopen this serial monitor after fixing your issue!");
while (true);
} // End of IF
}
void loop() {
readPasswords ();
dispPasswords ();
}
int readLine (File myFile, char *s, int size )
{
int n = 0;
while (myFile.available () > 0)
{
char c = myFile.read ();
if (c != '\r')
s [n++] = c;
if (size <= n || '\n' == c)
{
s [n-1] = 0;
break;
}
}
return n;
}
// -------------------------------------
void dispPasswords (void)
{
for (unsigned n = 0; n < nPassword; n++)
{
char s [20];
sprintf (s, " %2d %s", n, & password [n][0]);
lcd.clear ();
lcd.setCursor (0,1);
lcd.print (s);
delay (100);
}
}
// -------------------------------------
void readPasswords (void)
{
File myFile = SD.open (FILENAME);
char str [80];
int currentLoop = 0;
nPassword = 0;
while (readLine (myFile, str, sizeof (str)))
{
char *p = & password [nPassword++][0];
strncpy (p, str, PASSWORDLENGTH); // destination, source
currentLoop++;
if (currentLoop == NUMBEROFPASSWORDS)
{
currentLoop = 0;
break;
}
}
}