How can I place quote marks around a variable?

I have an I2C OLED connected to my Arduino Uno and I have used it to display integers in past projects, but I would like to know how to place quotes around a variable (in this case Serial.read() ). My current solution displays strange characters around the text on the OLED, but if I use a constant non-changing variable the OLED doesn't spit out a garbage character. Here's my code:

#include <Arduino.h>;
#include <U8x8lib.h>;
int breakout = 0; // Set to 1 if you have broken out the OLED Module.
 U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);
 
// U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // OLEDs without Reset of the Display

String serialData; 
void setup() {
  Serial.begin(9600);
  if(breakout = 1){
    u8x8.setBusClock(100000);  // Enabled when breakout is set to 1
  }
  
  u8x8.begin();
  u8x8.setFlipMode(1);
}
 
void loop(void) {
  u8x8.setFont(u8x8_font_amstrad_cpc_extended_f);
  u8x8.setCursor(0, 0);
  if(Serial.available() > 0){
    serialData = Serial.readString();
    String Data;
    Data = "\"" + serialData + "\""; // CURRENT SOLUTION

    u8x8.clear();
    u8x8.setFont(u8x8_font_amstrad_cpc_extended_f);
    u8x8.setCursor(0, 0);
    u8x8.print(Data);
  }
   
}

Random garbage before Test
looks like up-side-down quote marks

Arduino : How to put quotation marks in a string ? | Circuits4you.com

might need converted to String(serialData).

This still causes the garbage at the start

Oops

Are you seeing
"Test
"

That's what it looks like to me - not garbage, simply the newline that the serial monitor tacked on to your string.

1 Like

Ah yes the 2nd quote is on another line, possibly?

The OP might want to check if serialData is filled with spaces or the LF is being sent to the LCD as part of the serial data.

yes it appears that is what is happening.

Great - can you mark the topic "solved" then, please?

I am trying to figure out how to place quotes around the serial result, and currently it’s still broken, so I don’t completely consider this solved yet.

serialData has a line ending. You need to configure your serial monitor not to send a line ending or use Serial.readStringUntil() to read until the line ending. That is why the ending quote appears on the second line.

Did you fix this line as AWOL indicated in reply #4?

if(breakout = 1){

just curious, what does that line have to do with serialData?

Nothing. But it means this line is executed no matter what you initialize breakout to:

u8x8.setBusClock(100000);

In fact, it actually sets breakout to 1.

added == instead of = in between serialData and 1.

What does that mean?

Try these 2 steps:

  1. Don't use String: use c-strings.
  2. If you have trouble with escaping the double quotes, use the ASCII value for the double quote (34 or 0x22).

You mean between breakout and 1?

What is wrong with the if(breakout = 1){} line?

A lot.

Nothing is wrong if your intention is to make the variable breakout equal 1.


If you want to test if breakout is 1 then that syntax is wrong.

Study the reference documentation:

https://www.arduino.cc/reference/en/

I have changed = to ==.

Then why the above question :thinking: