ChatGPT made me this code, where it associates isbns with pins, this one works perfectly, but i want to add input from a 4x4 keypad, and output to an i2c lcd, gpt just messes everything up, could i get some help?
#include <SD.h>
#include <SPI.h>
const int chipSelectPin = 53; // CS pin for SD card module
const int ledPin = 2; // LED pin for indicating activity
void setup() {
Serial.begin(9600);
pinMode(chipSelectPin, OUTPUT);
pinMode(ledPin, OUTPUT);
// SD Card Initialization
if (SD.begin(chipSelectPin)) {
Serial.println("SD card is ready to use.");
} else {
Serial.println("SD card initialization failed");
return;
}
// Create a directory named "ISBNS" to store ISBN files
if (!SD.exists("ISBNS")) {
if (SD.mkdir("ISBNS")) {
Serial.println("Directory 'ISBNS' created.");
} else {
Serial.println("Failed to create directory 'ISBNS'.");
return;
}
}
// Print instructions
Serial.println("Instructions:");
Serial.println("- To define an association: Type 'define <ISBN> <PIN>'");
Serial.println("- To delete an association: Type 'delete <ISBN>'");
Serial.println("- To power up a pin for 30 seconds: Type the ISBN");
}
void loop() {
if (Serial.available()) {
String command = Serial.readStringUntil('\n');
command.trim();
if (command.startsWith("define ")) {
defineAssociation(command);
} else if (command.startsWith("delete ")) {
deleteAssociation(command);
} else {
powerUpPin(command);
}
}
}
void defineAssociation(String command) {
command.remove(0, 7); // Remove "define "
int separatorIndex = command.indexOf(' ');
if (separatorIndex != -1) {
String isbn = command.substring(0, separatorIndex);
int pin = command.substring(separatorIndex + 1).toInt();
String baseFilename = "/ISBNS/" + String(hashISBN(isbn));
String filename = baseFilename + ".txt";
// Handle collisions
int counter = 0;
while (SD.exists(filename)) {
counter++;
filename = baseFilename + "_" + String(counter) + ".txt";
}
Serial.println("Trying to open file: " + filename);
File associationFile = SD.open(filename, FILE_WRITE);
if (associationFile) {
Serial.println("File opened successfully.");
associationFile.println(isbn);
associationFile.println(pin);
associationFile.close();
Serial.println("Defined ISBN " + isbn + " to pin " + String(pin));
} else {
Serial.println("Error opening file for writing: " + filename);
}
} else {
Serial.println("Invalid command format. Use: define ISBN pin");
}
}
void deleteAssociation(String command) {
command.remove(0, 7); // Remove "delete "
String baseFilename = "/ISBNS/" + String(hashISBN(command));
String filename = baseFilename + ".txt";
// Handle collisions
int counter = 0;
while (SD.exists(filename)) {
if (SD.remove(filename)) {
Serial.println("Deleted association for ISBN " + command);
return;
}
counter++;
filename = baseFilename + "_" + String(counter) + ".txt";
}
Serial.println("No association found for ISBN " + command);
}
void powerUpPin(String isbn) {
String baseFilename = "/ISBNS/" + String(hashISBN(isbn));
String filename = baseFilename + ".txt";
boolean found = false;
int pin = -1;
// Handle collisions
int counter = 0;
while (SD.exists(filename)) {
File associationFile = SD.open(filename, FILE_READ);
if (associationFile) {
String storedISBN = associationFile.readStringUntil('\n');
String pinStr = associationFile.readStringUntil('\n');
associationFile.close();
storedISBN.trim(); // Trim any whitespace, newlines, etc.
Serial.println("Checking file: " + filename);
Serial.println("Stored ISBN: " + storedISBN);
Serial.println("Input ISBN: " + isbn);
Serial.println("Stored ISBN (HEX): ");
for (int i = 0; i < storedISBN.length(); i++) {
Serial.print(storedISBN[i], HEX);
Serial.print(" ");
}
Serial.println();
Serial.println("Input ISBN (HEX): ");
for (int i = 0; i < isbn.length(); i++) {
Serial.print(isbn[i], HEX);
Serial.print(" ");
}
Serial.println();
if (storedISBN.equals(isbn)) { // Corrected comparison
pin = pinStr.toInt();
found = true;
break;
}
}
counter++;
filename = baseFilename + "_" + String(counter) + ".txt";
}
if (found && pin != -1) {
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
Serial.println("Powered up pin " + String(pin) + " for ISBN " + isbn + " for 30 seconds.");
delay(30000);
digitalWrite(pin, LOW);
} else {
Serial.println("No association found for ISBN " + isbn);
}
}
unsigned long hashISBN(String isbn) {
unsigned long hash = 5381;
for (int i = 0; i < isbn.length(); i++) {
hash = ((hash << 5) + hash) + isbn[i];
}
return hash % 1000000; // Reduce hash to 6 digits
}