string and char...? - FIXED

ok, so I was trying to build a serial command system when I started getting the error:

Arduino: 1.7.6 (Windows 8.1), Board: "Arduino Uno"

sketch_aug21a.ino: In function 'void loop()':

sketch_aug21a.ino:37:11: error: cannot convert 'String' to 'char' in assignment

Error compiling.

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

I have the two following lines of code in my program:

  cmd[15] = (Serial.readStringUntil('.'));
const int lgcmd = 15;
char cmd[lgcmd];

Can someone help me with this problem? I'm really confused with it. :confused:

Please help us by posting the whole program, not just the snippet that you think is causing the problem.

The .readStringUntil() command returns a sequence of characters and you are strying to put them all in one location - cmd[15].

If you want to fill the whole cmd array, you would use
cmd = Serial.readUntil('.');

But you might have other problems. If you declare cmd as char cmd[15], index 15 does not exist.

but it does not matter since char cmd[lgcmd] attempts to create a whole new array, anyway.

You are trying to treat a C++ String object as if it is a C string array.

The C++ String is a C string with packaging for people too lazy to learn and use C strings. You're not supposed to need to get at the actual data in a String.

C++ Strings are wasteful of both memory and cycles. Microcontrollers have less of both than PC's.
Best solution is to ditch using C++ Strings in such small environments and code closer to the metal.

UPDATE:
I did some editing based off of KeithRB 's reply. However, I still am getting a similar error.
I also posted (below) the full code, and enabled "verbose output".
Code:

#include <Servo.h>

const int prop = 3;
const int steer = 2;

const int boad = 38400;

char cmd[6];
int incmd = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(boad);
  Serial.println("Serial communications have been opened.");
  Serial.println("---------------------------------------");
  Serial.println("Serial connection established. Boad: ");
  Serial.print(boad);
  pinMode(prop, OUTPUT);
  //confirmation of setup:
  Serial.println("-------------Now Looping...------------");
  Serial.println("---------------------------------------");
  Serial.println("Ready for command. Send /help. for help");
}

void loop() {
  // put your main code here, to run repeatedly:
  cmd = Serial.readStringUntil('.');
}

Error:

Arduino: 1.7.6 (Windows 8.1), Board: "Arduino Uno"

Using library Servo in folder: C:\Program Files (x86)\Arduino\libraries\Servo 



C:\Program Files (x86)\Arduino/hardware/tools/avr/bin/avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10706 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino -IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard -IC:\Program Files (x86)\Arduino\libraries\Servo\src C:\Users\Jacob\AppData\Local\Temp\build1086056094383448326.tmp\sketch_aug21a.cpp -o C:\Users\Jacob\AppData\Local\Temp\build1086056094383448326.tmp\sketch_aug21a.cpp.o 

sketch_aug21a.ino: In function 'void loop()':

sketch_aug21a.ino:27:7: error: incompatible types in assignment of 'String' to 'char [6]'

Error compiling.

Not sure what you are trying to do, but below is some delimited servo test code.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

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

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}
error: incompatible types in assignment of 'String' to 'char [6]'

The error seems quite explicit to me.

  cmd = Serial.readStringUntil('.');

cmd is an array of chars and you are trying to assign a String to it.

I suggest that you read each character as it becomes available and save it in the cmd array until you receive a full stop, but make sure that you only receive 6 characters at most or you will be in trouble. If you want to use cmd as C style string with a null terminator then you will need to add that yourself and ensure that you only receive 5 characters before the full stop.

UPDATE: THE ISSUE HAS BEEN RESOLVED!

The new code compiled successfully. I had tried 'string cmd;' but, it seems I wasted the time of ~100 people over the difference between 's' and 'S'.

Thank you, zoomkat, for the example.

New code

#include <Servo.h>

const int prop = 3;
const int steer = 2;

const int boad = 9600;

String cmd;
int incmd = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(boad);
  Serial.println("Serial communications have been opened.");
  Serial.println("---------------------------------------");
  Serial.println("Serial connection established. Boad: ");
  Serial.print(boad);
  pinMode(prop, OUTPUT);
  //confirmation of setup:
  Serial.println("-------------Now Looping...------------");
  Serial.println("---------------------------------------");
  Serial.println("Ready for command. Send /help. for help");
}

void loop() {
  // put your main code here, to run repeatedly:
  cmd = Serial.readStringUntil('.');
}

SweetEpicDude1:
I had tried 'string cmd;' but, it seems I wasted the time of ~100 people over the difference between 's' and 'S'.

That's because learning about C strings doesn't mean "spell it with a small s".

char myText[] = "This is a C string. It is a char array with ASCII text ending in a NULL (ie 0) char.";

myText[ 0 ] == 'T'
myText[ 1 ] == 'h'

Waste your time with String and when you want to do something non-trivial it will be unexplainable crash time.