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;
}