I am using the Arduino Uno and the EasyVR shield for motor and servo control. The setup works well, because when tested I can communicate and motors/servos follows my commands. There is a concern I have been trying to debug and I figured I would reach out to those more experienced. As mentioned, I have a motor and servo connected to the arduino through the shield (pins 9 and 11). When I run the program (included below), it will initially wait for a start command - in my case that word is “Arduino”, that command will move the code into another group which will recognize my following commands. When waiting for “Arduino”, the serial monitor keeps printing to say a command from group 0 and timing out if I do not say “Arduino”. This is to be expected, but every time the loop times out, the motors and servos both twitch. The motor moves in a backwards motion very briefly and the servo is not enough to even recognize. From researching, I think I have narrowed this problem to be an interrupt issue in one of the libraries, but am not sure how I could fix it. Is this the case? Does anyone have any recommendations?
Thanks in advance,
Code Attached:
#if defined(ARDUINO) && ARDUINO >= 100
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <Servo.h> //Reference the Servo Library
#include <Wire.h> // Reference the I2C Library
#include <HMC5883L.h> // Reference the HMC5883L Compass Library
SoftwareSerial port(12,13);
#else // Arduino 0022 - use modified NewSoftSerial
#include <WProgram.h>
// #include <NewSoftSerial.h>
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <Servo.h> //Reference the Servo Library
#include <Wire.h> // Reference the I2C Library
#include <HMC5883L.h> // Reference the HMC5883L Compass Library
SoftwareSerial port(12,13);
//NewSoftSerial port(12,13);
#endif
#include <EasyVR.h>
EasyVR easyvr(port);
//Groups and Commands
enum Groups
{
GROUP_0 = 0,
GROUP_4 = 4,
};
enum Group0
{
G0_ARDUINO = 0,
};
enum Group4
{
G4_RIGHT = 0,
G4_LEFT = 1,
G4_FORWARD = 2,
G4_BACKWARD = 3,
G4_HALT = 4,
};
EasyVRBridge bridge;
int8_t group, idx;
///////My declarations
Servo myservo; // create servo object to control the steering servo
Servo mymotor; // create servo object to control the driving motor
SoftwareSerial mySerial = SoftwareSerial(2, 3);
int pos = 0; // variable to store the servo position
int val; // variable to hold motor speed/orientation
void setup()
{
// bridge mode?
if (bridge.check())
{
cli();
bridge.loop(0, 1, 12, 13);
}
// run normally
Serial.begin(9600);
mySerial.begin(9600); //Datarate for softwareserial port
mySerial.println("X-CTU Terminal Awake!");
port.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
mymotor.attach(11); // attaches the motor on pin 11 to the servo object
if (!easyvr.detect())
{
mySerial.println("EasyVR not detected!");
for (;;);
}
easyvr.setPinOutput(EasyVR::IO1, LOW);
mySerial.println("EasyVR detected!");
easyvr.setTimeout(5);
easyvr.setLanguage(0);
group = EasyVR::TRIGGER; //<-- start group (customize)
}
void action();
void loop()
{
easyvr.setPinOutput(EasyVR::IO1, HIGH); // LED on (listening)
mySerial.print("Say a command in Group ");
mySerial.println(group);
easyvr.recognizeCommand(group);
do
{
//processing while waiting to finish
}
while (!easyvr.hasFinished());
easyvr.setPinOutput(EasyVR::IO1, LOW); // LED off
idx = easyvr.getWord();
if (idx >= 0)
{
// built-in trigger (ROBOT)
// group = GROUP_X; <-- jump to another group X
return;
}
idx = easyvr.getCommand();
if (idx >= 0)
{
// print debug message
uint8_t train = 0;
char name[32];
mySerial.print("Command: ");
mySerial.print(idx);
if (easyvr.dumpCommand(group, idx, name, train))
{
mySerial.print(" = ");
mySerial.println(name);
}
else
mySerial.println();
easyvr.playSound(0, EasyVR::VOL_FULL);
// perform some action
action();
}
else // errors or timeout
{
if (easyvr.isTimeout())
mySerial.println("Timed out, try again...");
int16_t err = easyvr.getError();
if (err >= 0)
{
mySerial.print("Error ");
mySerial.println(err, HEX);
}
}
}
void action()
{
switch (group)
{
case GROUP_0:
switch (idx)
{
case G0_ARDUINO:
// write your action code here
easyvr.playSound(1,31);
group = GROUP_4; //jump to another group X for composite commands
break;
}
break;
case GROUP_4:
switch (idx)
{
case G4_RIGHT:
easyvr.playSound(1,31);
myservo.write(178);
delay(15);
mymotor.write(84);
delay(1000);
group = GROUP_4;
break;
case G4_LEFT:
easyvr.playSound(1,31);
myservo.write(1);
delay(15);
mymotor.write(84);
delay(1000);
group = GROUP_4;
break;
case G4_FORWARD:
easyvr.playSound(1,31);
myservo.write(87);
mymotor.write(84);
delay(1000);
group = GROUP_4;
break;
case G4_BACKWARD:
easyvr.playSound(1,31);
myservo.write(93);
mymotor.write(102);
delay(1000);
group = GROUP_4;
break;
case G4_HALT:
myservo.write(90);
mymotor.write(90);
easyvr.playSound(1,31);
group = GROUP_4;
break;
}
break;
}
}