Missing terminating " character - Serial Monitor

Hello, im new to coding and arduino. I am trying to add a command ("RESET") to the serial monitor so the saved progress from EEPROM will be reset and the counting will start over.

what i am working with: Arduino UNO R3, 1.3" 128x64 OLED display, SPI, white and one Button Switch

here is the code (the highlighted error is on line 44, 45, 46)

#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <EEPROM.h> // For persistent storage

// Constructor for SH1106 SPI OLED
U8G2_SH1106_128X64_NONAME_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);

// Button pin
const int buttonPin = 7; // Button connected to pin 2
bool lastButtonState = LOW; // Previous button state

// Variables for cigarette counts and health
int dnesCigarettes = 0; // Cigarettes for the day
int totalCigarettes = 0; // Total cigarettes
int healthBarPixels = 0; // Pixels filled in the health bar (persistent)

// Rectangle dimensions and position
const int rectX = 25;       // Start 25 pixels from the left
const int rectY = 50;       // Y position
const int rectWidth = 98;   // Width of the rectangle
const int rectHeight = 10;  // Height of the rectangle

// Timing for screen switching
unsigned long lastSwitchTime = 0; // Last time the screen switched
int currentScreen = 0;           // Tracks the current screen (0: Main, 1: Secondary, 2: New Screen)

void setup(void) {
  Serial.begin(9600); // Initialize Serial communication
  u8g2.begin(); // Initialize the display
  pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with pull-up resistor

  // Load total cigarettes and health bar pixels from EEPROM
  EEPROM.get(0, totalCigarettes);
  EEPROM.get(sizeof(totalCigarettes), healthBarPixels);
  if (totalCigarettes < 0) {
    totalCigarettes = 0; // Ensure no negative value is loaded
  }
}

void loop(void) {
  // Handle serial commands
  if (Serial.available()) {
    String command = Serial.readStringUntil('
'); // Correctly handles newline character
    command.trim(); // Remove whitespace
    if (command == "RESET") {
      totalCigarettes = 0;
      healthBarPixels = 0;
      EEPROM.put(0, totalCigarettes);
      EEPROM.put(sizeof(totalCigarettes), healthBarPixels);
      Serial.println("EEPROM progress reset to 0.");
    }
  }
  // Handle button press
  handleButtonPress();

  // Check if 5 seconds have passed
  if (millis() - lastSwitchTime > 10000) {
    currentScreen = (currentScreen + 1) % 3; // Cycle between screens 0, 1, and 2
    lastSwitchTime = millis();               // Update the last switch time
  }

  // Draw the appropriate screen
  if (currentScreen == 0) {
    drawMainScreen();
  } else if (currentScreen == 1) {
    drawSecondaryScreen();
  } else if (currentScreen == 2) {
    drawNewScreen();
  }

  delay(100); // Small delay to avoid flickering
}

void handleButtonPress() {
  bool currentButtonState = digitalRead(buttonPin) == LOW; // Read button state

  if (currentButtonState && !lastButtonState) { // Detect button press
    currentScreen = 0; // Switch to main screen
    lastSwitchTime = millis(); // Reset the timer
    dnesCigarettes++; // Increment daily count
    totalCigarettes++; // Increment total count

    // Save total cigarettes and health bar pixels to EEPROM
    EEPROM.put(0, totalCigarettes);
    EEPROM.put(sizeof(totalCigarettes), healthBarPixels);

    // Update health bar
    if (totalCigarettes % 20 == 0 && healthBarPixels < rectWidth - 2) {
      healthBarPixels += 1; // Add one pixel to the health bar for every 20 presses
    }
  }

  lastButtonState = currentButtonState; // Update last button state
}

void drawMainScreen() {
  u8g2.clearBuffer(); // Clear the internal memory

  // Draw "DNES" (Centered Text)
  u8g2.setFont(u8g2_font_ncenB14_tr); // Medium font
  int dnesX = (128 - u8g2.getStrWidth("DNES: 00")) / 2; // Calculate centered X position
  u8g2.setCursor(dnesX, 40);
  u8g2.print("DNES: ");
  u8g2.print(dnesCigarettes);

  // Draw "CELKEM" (Top Left)
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.setCursor(0, 10);
  u8g2.print("CELKEM: ");
  u8g2.print(totalCigarettes);

  // Draw Health Bar
  u8g2.setFont(u8g2_font_ncenB08_tr); // Font for HP
  u8g2.setCursor(0, 58);             // Adjusted to align with the rectangle
  u8g2.print("HP");

  // Draw transparent rectangle (outline only)
  u8g2.drawFrame(rectX, rectY, rectWidth, rectHeight);

  // Draw filled part of the rectangle
  if (healthBarPixels > 0) {
    u8g2.drawBox(rectX + 1, rectY + 1, healthBarPixels, rectHeight - 2);
  }

  u8g2.sendBuffer(); // Transfer internal memory to display
}

void drawSecondaryScreen() {
  u8g2.clearBuffer(); // Clear the internal memory

  // Adjusted Y positions for equal spacing
  int topTextY = 10; // Align top of "DENNE" with "CELKEM"
  int bottomLineY = 60; // Bottom of the last line (aligned with the rectangle's bottom)
  int lineSpacing = (bottomLineY - topTextY) / 3; // Calculate uniform spacing

  // Draw "PRŮMĚR DENNĚ:"
  u8g2.setFont(u8g2_font_ncenB08_tr); // Medium font
  u8g2.setCursor(0, topTextY);
  u8g2.print("DENNE:");

  // Draw "PRŮMĚR TÝDNĚ:"
  u8g2.setCursor(0, topTextY + lineSpacing); // Next line
  u8g2.print("TYDNE:");

  // Draw "PRŮMĚR MĚSÍČNĚ:"
  u8g2.setCursor(0, topTextY + 2 * lineSpacing); // Next line
  u8g2.print("MESICNE:");

  // Draw "PRŮMĚR ROČNĚ:"
  u8g2.setCursor(0, topTextY + 3 * lineSpacing); // Next line
  u8g2.print("ROCNE:");

  u8g2.sendBuffer(); // Transfer internal memory to display
}

void drawNewScreen() {
  u8g2.clearBuffer(); // Clear the internal memory

  // Adjusted Y positions for equal spacing
  int topTextY = 10; // Align top of "DNES" with "CELKEM"
  int bottomLineY = 60; // Bottom of the last line (aligned with the rectangle's bottom)
  int lineSpacing = (bottomLineY - topTextY) / 3; // Calculate uniform spacing

  // Draw "DNES:"
  u8g2.setFont(u8g2_font_ncenB08_tr); // Medium font
  u8g2.setCursor(0, topTextY);
  u8g2.print("DNES:");

  // Draw "% NIKOTINU"
  u8g2.setCursor(0, topTextY + lineSpacing); // Next line
  u8g2.print("% NIKOTINU");

  // Draw "DEHETU"
  u8g2.setCursor(0, topTextY + 2 * lineSpacing); // Next line
  u8g2.print("DEHETU");

  // Draw "OXIDU UHELNATEHO"
  u8g2.setCursor(0, topTextY + 3 * lineSpacing); // Next line
  u8g2.print("OXIDU UHELNATEHO");

  u8g2.sendBuffer(); // Transfer internal memory to display
}

I fail to see why you would not simply use

    String command = Serial.readStringUntil('\n'); // Correctly handles newline character

i changed it but the error says the same, "Compilation error: missing terminating ' character". am i missing something??

It compiled cleanly for me after that change.

Looks like you tried to type in the actual newline character between single quotes, instead of the escape sequence '\n'. This resulted in the line of code being split into two lines, the first line ending with a single quote, and the next line starting with a single quote, with nothing in between.

    String command = Serial.readStringUntil('
'); // Correctly handles newline character

thank you so so much this fixed it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.