Serial to SD

Hey,
So I’m trying to make it so everything that is typed into the serial console is saved to the SD card. I’m using an Arduino UNO and an Adafruit SD card shield. It sort of works, as it does take what I type and save it to the card. But it also saves a ton of blank lines and I tried to put the boolean stringComplete like in one of the Serial examples to help save only when it receives something instead of empty data this but that system isn’t working. Any suggestions?

#include <SPI.h>
#include <SD.h>

const int chipSelect = 10;

String inputString = "";
boolean stringComplete = true;

void setup() {
  
  Serial.begin(9600);
  
  inputString.reserve(200);

  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    
    return;
  }
  Serial.println("card initialized.");
}

void loop() {
  serialEvent();
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  if (stringComplete = true) {
    Serial.println(inputString);

    dataFile.println(inputString);
    dataFile.close();
    
    Serial.println(inputString);

    inputString = "";
    stringComplete = false;
  }
}

void serialEvent() {
  while (Serial.available()) {

    char inChar = (char)Serial.read();
    inputString += inChar;

    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

if (stringComplete = true)

Change to:

if (stringComplete == true) .

Thank you for the quick reply, but now it does not work at all. It says the card is read and it creates the DATALOG.txt file, but it does not seem to recognize what I typed into the console

SerialEvent occurs whenever a new data comes in the hardware serial RX. You are calling it in loop, don't do this!

Handel RX data in loop, don't use the function serialEvent()

Thank you! I got just the right push to figure out the program and put in the rest of the code to make it work the way its supposed to in conjunction with the other devices