Go Down

Topic: Little error: cannot convert 'String' to 'const char*' for ... (Read 12011 times) previous topic - next topic

elglanto

Hello there. First of all, here is my code:

Code: [Select]
const int led = 13;
String statu = "OFF";

void setup() {
  Serial.begin(9600);
      pinMode(led, OUTPUT);
      digitalWrite(led, LOW);
}

void loop() {

  if (Serial.available() > 0) {
    int inByte = Serial.read();

    switch (inByte) {
    case '0':
      statu = "OFF";
      break;
    case '1':
      statu = "ON";
      break;
    }
  }
  if (strcmp (statu,"OFF") == 1) {
    digitalWrite(led, LOW);
  }
  else {
    digitalWrite(led, HIGH);
  }
}


Please note that the
Code: [Select]
digitalWrite(led, LOW/LOW); could be in the switch case part but I need it here to add more code in the
Code: [Select]
if laser.

So, the error I got is:
Quote
test:31: error: cannot convert 'String' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'

As I am a beginner, I am still having an hard time with the string/char thing. First, I try to change the second line to:
Code: [Select]
String statu = "OFF"; but it doesn't work either.

I guess the type of the variable "statu" is not correct but what would be correct then?

gmcd2200

Hi

Ignoring your code for other things..
Just comparing a string with a string is a good thing...

ie:  if (strcmp (statu,"OFF") == 1) {                  
to
if (strcmp (statu,"OFF") == char("1")) {

just a guess.

lar3ry

First, something to remember.
String != string.
A String is an object that is actually a wrapper for a string, that allows you to do a lot of fairly intuitive things with the object.
A string, on the other hand (note the lower case s), is the usual way to refer to an array of char.

You cannot use strcmp() on a String object, because strcmp expects to compare two char arrays.

Your if statement will work if you change it to

Code: [Select]
  if (statu == "OFF" ) {


That being said, it is well worth your while to learn C strings. Yes, it's a little less intuitive for beginners, but the major advantage for Arduino programming is that they do not fragment your memory, which is in limited supply. Strings eat memory, in various sized chunks, leading to fragmentation, and ultimately overrunning the capacity, leading to some very hard-to-find bugs.

Save the String class for when you are programming a PC with Megabytes or Gigabytes of RAM.

Your program, using strings, looks like this:

Code: [Select]

const int led = 13;
char statu[4];  // 3 chars and a NULL terminator

void setup() {
  Serial.begin(115200);
      pinMode(led, OUTPUT);
      digitalWrite(led, LOW);
}

void loop() {

  if (Serial.available() > 0) {
    int inByte = Serial.read();

    switch (inByte) {
    case '0':
      strcpy(statu, "OFF");
      break;
    case '1':
      strcpy(statu, "ON");
      break;
    }
  }
  if (strcmp (statu,"OFF") == 0) {  // assuming you want to know if [b]statu and "OFF" are equal[/b]
    digitalWrite(led, LOW);
  }
  else {
    digitalWrite(led, HIGH);
  }
}

pYro_65

#3
May 21, 2014, 07:28 am Last Edit: May 21, 2014, 07:30 am by pYro_65 Reason: 1
The string has its constructors marked 'explicit', which means they cannot be implicitly called using code like this:
Code: [Select]
String statu = "OFF";

To explicitly call it, use the constructor call method.
Code: [Select]
String statu( "OFF" );

And the if statement comparison is covered in the posts above.
Forum Mod anyone?
https://arduino.land/Moduino/

elglanto

Hello, thanks for your help, it works now. But one last thing is bugging me. I want to have the initial state of statu being OFF. Thus, I use
Code: [Select]
strcpy(statu, "OFF");. But why does it have to be in there:

Code: [Select]
void setup() {
  Serial.begin(9600);
      pinMode(led, OUTPUT);
      digitalWrite(led, LOW);
      strcpy(statu, "OFF");
}


and not there:

Code: [Select]
const int led = 13;
char statu[4];
strcpy(statu, "OFF");


?

PaulS

Quote
But why does it have to be in there:

Because it is executable code, and executable code needs to be in a function.

You could do:
Code: [Select]
char statu[] ="OFF";
The art of getting good answers lies in asking good questions.

Go Up