Having issues with rc car control script?

I changed the recarcontrol program a bit to my current setup. Im using node to send commands but im always returning a -1?

/* A Spark function to parse the commands */

int rcCarControl(String command);

/* Globals -------------------------------------------------------------------*/

int leftmotor1 =D0;
int leftmotor2 =D1;
int leftmotor3 =D2;
int leftmotor4 =D3;
int rightmotor1 =D4;
int rightmotor2 =D5;
int rightmotor3 =D6;
int rightmotor4 =D7;

/* This function is called once at start up ----------------------------------*/
void setup()
{
//Register Spark function
Spark.function(“rccar”, rcCarControl);

pinMode(leftmotor1, OUTPUT);
pinMode(leftmotor2, OUTPUT);
pinMode(leftmotor3, OUTPUT);
pinMode(leftmotor4, OUTPUT);
pinMode(rightmotor1, OUTPUT);
pinMode(rightmotor2, OUTPUT);
pinMode(rightmotor3, OUTPUT);
pinMode(rightmotor4, OUTPUT);
}

/* This function loops forever --------------------------------------------*/
void loop()
{
// Nothing to do here
}

/*******************************************************************************

  • Function Name : rcCarControl

  • Description : Parses the incoming API commands and sets the motor control
    pins accordingly

  • Input : RC Car commands
    e.g.: rc,FORWARD
    rc,BACK

  • Output : Motor signals

  • Return : 1 on success and -1 on fail
    *******************************************************************************/
    int rcCarControl(String command)
    {
    if(command.substring(3,7) == “STOP”)
    {
    digitalWrite(leftmotor1,LOW);
    digitalWrite(leftmotor2,LOW);
    digitalWrite(leftmotor3,LOW);
    digitalWrite(leftmotor4,LOW);
    digitalWrite(rightmotor1,LOW);
    digitalWrite(rightmotor2,LOW);
    digitalWrite(rightmotor3,LOW);
    digitalWrite(rightmotor4,LOW);

    return 1;
    }

if(command.substring(3,7) == “BACK”)
{
digitalWrite(leftmotor1,HIGH);
digitalWrite(leftmotor2,LOW);
digitalWrite(leftmotor3,LOW);
digitalWrite(leftmotor4,HIGH);
digitalWrite(rightmotor1,LOW);
digitalWrite(rightmotor2,HIGH);
digitalWrite(rightmotor3,HIGH);
digitalWrite(rightmotor4,LOW);
return 1;
}

if(command.substring(3,10) == “FORWARD”)
{
digitalWrite(leftmotor1,LOW);
digitalWrite(leftmotor2,HIGH);
digitalWrite(leftmotor3,HIGH);
digitalWrite(leftmotor4,LOW);
digitalWrite(rightmotor1,HIGH);
digitalWrite(rightmotor2,LOW);
digitalWrite(rightmotor3,LOW);
digitalWrite(rightmotor4,HIGH);

return 1;

}

if(command.substring(3,8) == “RIGHT”)
{
digitalWrite(leftmotor1,HIGH);
digitalWrite(leftmotor2,LOW);
digitalWrite(leftmotor3,LOW);
digitalWrite(leftmotor4,HIGH);
digitalWrite(rightmotor1,HIGH);
digitalWrite(rightmotor2,LOW);
digitalWrite(rightmotor3,LOW);
digitalWrite(rightmotor4,HIGH);

return 1;

}

if(command.substring(3,7) == “LEFT”)
{
digitalWrite(leftmotor1,LOW);
digitalWrite(leftmotor2,HIGH);
digitalWrite(leftmotor3,HIGH);
digitalWrite(leftmotor4,LOW);
digitalWrite(rightmotor1,LOW);
digitalWrite(rightmotor2,HIGH);
digitalWrite(rightmotor3,HIGH);
digitalWrite(rightmotor4,LOW);
return 1;
}

if(command.substring(3,7)=="lol")
{return 2;}

// If none of the commands were executed, return false
return -1;
}

Will you be sending the “rc,” prefix to every command you are going to issue? If so, you could drop that part of the string. Then you could use if(command.equals("STOP")) { /* "stop" code here */ }. It is a little simpler, and nobody likes to count characters to use in a .substring() anyways. :wink:

Or, if you want the commands to be case _in_sensitive, you can use command.equalsIgnoreCase() instead.

Hey!

Just wanna say that @will has modified RC code to make it better and he will be willing to share with everyone :slight_smile:

Make sure you bug him mid-week next week cos we are all at Maker faire this weekend!

Thanks for the reminder, @kennethlimcp. Linked below are two files–firmware for the RC Car, and a javascript listener that passes on messages locally, rather than through the Cloud.

Just got back in town and saw this. Thanks, will try this tonight