-1
#include <Preferences.h>
Preferences preferences;


void setup() {
  Serial.begin(115200);

  char keyToAdd[15];

  String valueToAdd;

  keyToAdd = Serial.readString();
  valueToAdd = Serial.readString();

  preferences.begin("licence",false);

  preferences.putString(keyToAdd, valueToAdd);

  preferences.end();
 }

void loop() {
}

I want a key maybe "room" and want to write on my Serial monitor room test but ...

What I got is this error:

incompatible types in assignment of 'String' to 'char [15]'
Dharman
  • 30,962
  • 25
  • 85
  • 135
bull19
  • 53
  • 5

1 Answers1

2

Serial.readString() returns a String object, keyToAdd is an array of 15 characters. There is no automatic conversion from String to a char array.

I don't think you need a char array in this case anyway. Just make keyToAdd a String the same as valueToAdd then use c_str() to get a const char* to pass to putString:

void setup() {
  Serial.begin(115200);

  String keyToAdd;

  String valueToAdd;

  keyToAdd = Serial.readString();
  valueToAdd = Serial.readString();

  preferences.begin("licence",false);

  preferences.putString(keyToAdd.c_str(), valueToAdd);

  preferences.end();
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • readString times out quickly. Not too easy to handle, if you have two consecutive Serial.readString() calls – datafiddler Apr 21 '22 at 14:46
  • @datafiddler I'm not advocating this being the correct code (I've only occasionally dabbled in Arduino), I was just fixing the error in the question – Alan Birtles Apr 21 '22 at 14:49