Need help with code

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

}

can you explain how the current code works?

If you don't know what you're doing, stop using that f*cking ChatGPT and start studying a bit. ChatGPT can be useful to start from scratch, but it always needs to be double checked (and understood).
Just to make things clearer: "There ain't no such thing as a free lunch" or "no pain, no gain". Sorry.

I don't support ChatGPT. I have two reasons, the first is when I asked igt to create a real program that a professional programmer would normally do it didn't even try. The second reason is I have found it to make mistakes fairly frequently.
I do use ChatGPT to get me started if I don't have a good starting model, and to make my code less complex.
Good luck.

The psychology puzzles me. People can get free code from an AI, so they figure they can get unpaid work from a human too.

It's like winning a cake in the lottery, and then going to a neighbor and saying "I won a cake, but now I have eaten it, and to be honest it wasn't very good. I heard you can cook, can you bake me a cake for free?"

mute

Al = Assisted laziness

In German we say "betreutes denken" :slight_smile:

Never does it initialize or use an LCD.