#include <Wire.h>
#include <String.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *myMotorA = AFMS.getMotor(1);
Adafruit_DCMotor *myMotorB = AFMS.getMotor(2);
Adafruit_DCMotor *myMotorC = AFMS.getMotor(3);
Adafruit_DCMotor *myMotorD = AFMS.getMotor(4);
String cmdline = "";
bool newcommand = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // set up Serial library at 9600 bps
//cmdline.reserve(200);
//Serial.println("9");
//Serial.println("Motor test!");
AFMS.begin();
myMotorA->setSpeed(0);
/*myMotorB->setSpeed(0);
myMotorC->setSpeed(0);
myMotorC->setSpeed(0);*/
}
void serialEvent()
{
String newline;
newcommand=false;
char inchar;
while(Serial.available())
{
inchar = Serial.read();
if(inchar=='\n')
{
newcommand=true;
}else
{
newline+=inchar;
}
}
}
void loop() {
// put your main code here, to run repeatedly:
char data;
String result;
if(newcommand==true)
{
Serial.println(cmdline);
newcommand =false;
if(cmdline=="start")
{
myMotorA->setSpeed(10);
Serial.println("go");
}
if(cmdline =="stop")
{
Serial.println("end");
myMotorA->setSpeed(0);
}
cmdline = "";
}
}
an Adafriut Motor Shield v2.3 on a Arduino Mega. The Motor Shield component works fine. The Serial Interrupt receives the message fine. Serial.println(cmdline); shows the right message.
Are you simply printing the 'whole' string, but not seeing it.
Serial.print(F("<")); // a prefix to show the 'real' start of the string
Serial.print(cmdLine); // the command string - warts and all
Serial.println(F(">")); // a suffix to show the 'real' end of the string
Will bracket the cmdLine - which could (undesirably) appear like... <command >
You are failing to copy newline to cmdline at the relevant point I think. This is either when you
see a newline in serialEvent, or when you recognize that newcommand has become true.