To whom this may concern,
On line 178 of the code below, I am having trouble getting a live reading from my analogRead(0) "val0" port. I am not sure if I can get live readings in a switch/case statement, however I can't stop my robot if I can't read these potentiometers. I am using the Adafruit Motor Shield and the Tigal EasyVR Shield to control all of these actions. If someone could look at my code and tell me if I can read live readings or not it would much appreciated.
Background: I am building a voice activated robot for my senior capstone. This robot will be commanded to pour a drink in a different cup. I have a robot that has DC motors that are being controlled by potentiometers. Therefore, I need them to stop when they get to a certain point on the pot.
if you have any question or concerns please let me know.
regards,
James
#include <EasyVR.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *Motor1 = AFMS.getMotor(1);
int val0 = analogRead(0);
int val1 = analogRead(1);
int val2 = analogRead(2);
int val3 = analogRead(3);
int val4 = analogRead(4);
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#include "SoftwareSerial.h"
SoftwareSerial port(12,13);
#else // Arduino 0022 - use modified NewSoftSerial
#include "WProgram.h"
#include "NewSoftSerial.h"
NewSoftSerial port(12,13);
#endif
EasyVR easyvr(port);
//Groups and Commands
enum Groups
{
GROUP_0 = 0,
GROUP_1 = 1,
};
enum Group0
{
G0_ROBOT = 0,
};
enum Group1
{
G1_COKE = 0,
G1_PEPSI = 1,
G1_SPRITE = 2,
};
EasyVRBridge bridge;
int8_t group, idx;
void setup()
{
// bridge mode?
if (bridge.check())
{
cli();
bridge.loop(0, 1, 12, 13);
}
// run normally
Serial.begin(9600);
port.begin(9600);
if (!easyvr.detect())
{
Serial.println("EasyVR not detected!");
for (;;);
}
easyvr.setPinOutput(EasyVR::IO1, LOW);
Serial.println("EasyVR detected!");
easyvr.setTimeout(5);
easyvr.setLanguage(0);
group = EasyVR::TRIGGER; //<-- start group (customize)
Serial.println("Adafruit Motorshield v2 - DC Motor test!");
Serial.println("Starting up controller!");
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
Serial.println("Controller has succsessfully !");
// Set the speed to start, from 0 (off) to 255 (max speed)
Motor1->setSpeed(150);
}
void action();
void loop()
{
int val0 = analogRead(0);
int val1 = analogRead(1);
int val2 = analogRead(2);
int val3 = analogRead(3);
int val4 = analogRead(4);
easyvr.setPinOutput(EasyVR::IO1, HIGH); // LED on (listening)
Serial.print("Say a command in Group ");
Serial.println(group);
Serial.println(val0);
Serial.println(val1);
Serial.println(val2);
Serial.println(val3);
Serial.println(val4);
easyvr.recognizeCommand(group);
do
{
// can do some processing while waiting for a spoken command
}
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];
Serial.print("Command: ");
Serial.print(idx);
if (easyvr.dumpCommand(group, idx, name, train))
{
Serial.print(" = ");
Serial.println(name);
}
else
Serial.println();
easyvr.playSound(0, EasyVR::VOL_FULL);
// perform some action
action();
}
else // errors or timeout
{
if (easyvr.isTimeout())
Serial.println("Timed out, try again...");
int16_t err = easyvr.getError();
if (err >= 0)
{
Serial.print("Error ");
Serial.println(err, HEX);
}
}
}
void action()
{
switch (group)
{
case GROUP_0:
switch (idx)
{
case G0_ROBOT:
// write your action code here
group = GROUP_1;// group = GROUP_X; <-- or jump to another group X for composite commands
break;
}
break;
case GROUP_1:
switch (idx)
{
case G1_COKE:
Serial.println ("before");
val0 = analogRead(0);
if (val0 >500)
{
while (val0 >500)
{
Serial.print ("value = ");
Serial.println (val0);
Motor1->setSpeed(100);
Motor1->run(FORWARD);
delay(100);
}
Motor1->setSpeed(0);
}
Serial.println ("after");
group = GROUP_0;// group = GROUP_X; <-- or jump to another group X for composite commands
break;
case G1_PEPSI:
// write your action code here
group = GROUP_0;// group = GROUP_X; <-- or jump to another group X for composite commands
break;
case G1_SPRITE:
// write your action code here
group = GROUP_0;// group = GROUP_X; <-- or jump to another group X for composite commands
break;
}
break;
}
}