Voice Recognition Module V2 Arduino Obstacle Avoidance Code Error- Please Help!

So far I’ve been working on the setup for my robot with his three commands, Stop, Wander, and Look Around.

My Idea of a stop code is just delay(1000000000)
which is like 29 years.

Anyways back to the problem:

#include <Servo.h>

com = 0;
const int RForward = 0; 
const int RBackward = 180; 
const int LForward = RBackward; 
const int LBackward = RForward; 
const int RNeutral = 90; 
const int LNeutral = 90; 
const int pingPin = 7;
const int irPin = 0;  
const int dangerThresh = 10; 
int leftDistance, rightDistance; 
Servo panMotor;  
Servo leftMotor;
Servo rightMotor;
long duration;

void setup (){
  Serial.begin(9600);
  Serial.write(0xAA);
  Serial.write(0x37);
  Serial.write(0xAA);
  Serial.write(0x21);
  rightMotor.attach(11);
  leftMotor.attach(10);
  panMotor.attach(6);
  panMotor.write(90);
}

void loop (){
  while(Serial.available())
{
  com = Serial.read();
  switch(com)
{
  case 0x12:
  int distanceFwd = ping();
  if (distanceFwd>dangerThresh) //if path is clear
  {
    leftMotor.write(LForward); 
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);
    rightMotor.write(RNeutral); 
    panMotor.write(0); 
    delay(500);
    rightDistance = ping(); //scan to the right
    delay(500);
    panMotor.write(180);
    delay(700);
    leftDistance = ping(); //scan to the left
    delay(500);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();
  }
  void compareDistance()
  {
    if (leftDistance>rightDistance)
    {
      leftMotor.write(LBackward)
      rightMotor.write(RForward)
      delay(500);
    }
    else if (rightDistance>leftDistance)
    {
      leftMotor.write(LForward)
      rightMotor.write(RBackward)
      delay(500);
    }
    else
    {
      leftMotor.write(LForward)
      rightMotor.write(RBackward);
      delay(1000);
    }
  long ping()
  {
    pinMode(pingPin, OUTPUT);
    digitalWrite(pingPin, LOW);
    delayMicroseconds(2);
    digitalWrite(pingPin, HIGH);
    delayMicroseconds(5);
    digitalWrite(pingPin, LOW);
    pinMode(pingPin, INPUT);
    duration = pulseIn(pingPin, HIGH);
    return duration / 29 / 2;
  }
  }
}

and here is the error for my code:

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.
Arduino: 1.0.6 (Windows 7), Board: "Arduino Uno"
Voice_Command_2:3: error: expected constructor, destructor, or type conversion before '=' token
Voice_Command_2.ino: In function 'void loop()':
Voice_Command_2:34: error: 'com' was not declared in this scope
Voice_Command_2:61: error: a function-definition is not allowed here before '{' token
Voice_Command_2:93: error: expected `}' at end of input
Voice_Command_2:93: error: expected `}' at end of input
Voice_Command_2:93: error: expected `}' at end of input

If someone could help me with fixing this code, that’d be great. I used a tutorial to learn the voice recognition, but all the tutorial gave me was a code for LEDs and I really don’t know how to apply that code to obstacle avoidance: http://www.instructables.com/id/Arduino-voice-control/

:slight_smile:

You did not say what type of variable you want "com" to be. Try int.

You are missing THREE close-brackets before the function compareDistance(). You have to finish loop() before you can start declaring compareDistance().

You are missing five semicolons at the ends of statements in compareDistance().

You are missing another close-bracket before the function ping().

You have two extra close-brackets at the end of the sketch.

My Idea of a stop code is just delay(1000000000)

So do you know how big a number an int will store?
Also look up long and long long data types.

A real stop is:-

while(1);

Which like you delay is useless because you need a reset to recover from it.

So thank you two for replying! :slight_smile:
I have fixed the code, at least that’s what Arduino says:

#include <Servo.h>

byte com = 0;
const int RForward = 0; 
const int RBackward = 180; 
const int LForward = RBackward; 
const int LBackward = RForward; 
const int RNeutral = 90; 
const int LNeutral = 90; 
const int pingPin = 7;
const int irPin = 0;  
const int dangerThresh = 10; 
int leftDistance, rightDistance; 
int distanceFwd = ping();
Servo panMotor;  
Servo leftMotor;
Servo rightMotor;
long duration;

void setup (){
  Serial.begin(9600);
  Serial.write(0xAA);
  Serial.write(0x37);
  Serial.write(0xAA);
  Serial.write(0x21);
  rightMotor.attach(11);
  leftMotor.attach(10);
  panMotor.attach(6);
  panMotor.write(90);
}

void loop ()
{
  while(Serial.available())
{
  com = Serial.read();
  switch(com)
{
  case 0x12:
  if (distanceFwd>dangerThresh) //if path is clear
  {
    leftMotor.write(LForward); 
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);
    rightMotor.write(RNeutral); 
    panMotor.write(0); 
    delay(500);
    rightDistance = ping(); //scan to the right
    delay(500);
    panMotor.write(180);
    delay(700);
    leftDistance = ping(); //scan to the left
    delay(500);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();
  }
}
}
}
void compareDistance()
{
  if (leftDistance>rightDistance)
  {
    leftMotor.write(LBackward);
    rightMotor.write(RForward);
    delay(500);
  }
  else if (rightDistance>leftDistance)
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward);
    delay(500);
  }
  else
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward);
    delay(1000);
  }
}
long ping()
{
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  return duration / 29 / 2;
}

Now the previous reply did bring up a point. Since I am new to this, the only time stopping code integer that I’ve “used” is delay. Is there anyway to loop this delay when I use voice command and unloop when I change commands?

And here is my look around code, which uses the servo sweep library code:

/* Sweep
 by BARRAGAN <http://barraganstudio.com> 
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://arduino.cc/en/Tutorial/Sweep
*/ 

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
 myservo.attach(6);  // attaches the servo on pin 9 to the servo object
 myservo.write(90);
} 
 
void loop() 
{ 
  for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(1000);                       // waits 1s for the servo to reach the position 
  } 
  for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(1000);                       // waits 1s for the servo to reach the position 
  } 
}

Also with this obstacle avoidance code, does the robot do it for however long I say the code, forever, or until the voice command sends out another signal? Or should I just put all voice commands in one code so all the codes can be monitored at once by the Arduino? I think the second one is a good idea.

Thanks for the replies peoples! :slight_smile:

Ok so I plugged in and tried the code, but my VR just stays on idle mode, not recognition mode. The VR doesn't seem to try to test for me saying codes. Why is this and how do I fix this?
Ty! :slight_smile:

You don't want to stop the code you want to stop the robot. That is just turn all the motors off.

There is little point posting example code you need to post the code you are having trouble with.

Another wild problem has appeared.
Here is the code:

#include <Servo.h>

const int RForward = 0; 
const int RBackward = 180; 
const int LForward = RBackward; 
const int LBackward = RForward; 
const int RNeutral = 90; 
const int LNeutral = 90; 
const int pingPin = 7;
const int irPin = 0;  
const int dangerThresh = 10; 
int leftDistance, rightDistance; 
int distanceFwd = ping();
int pos = 0;
Servo panMotor;  
Servo leftMotor;
Servo rightMotor;
long duration;
byte com = 0;

void setup (){
  Serial.begin(9600);
  delay(2000);
  Serial.write(0xAA);
  Serial.write(0x37);
  delay(1000);
  Serial.write(0xAA);
  Serial.write(0x21);
  rightMotor.attach(11);
  leftMotor.attach(10);
  panMotor.attach(6);
  panMotor.write(90);
}
void loop ()
{
  while(Serial.available())
{
  com = Serial.read();
  switch(com)
{
  case 0x12:
  if (distanceFwd>dangerThresh) //if path is clear
  {
    leftMotor.write(LForward); 
    rightMotor.write(RForward); //move forward
  }
  else //if path is blocked
  {
    leftMotor.write(LNeutral);
    rightMotor.write(RNeutral); 
    panMotor.write(0); 
    delay(500);
    rightDistance = ping(); //scan to the right
    delay(500);
    panMotor.write(180);
    delay(700);
    leftDistance = ping(); //scan to the left
    delay(500);
    panMotor.write(90); //return to center
    delay(100);
    compareDistance();
  }
  break;
  case 0x13:
  void loop(){
    panMotor.write(180);              
    delay(1000);                                               
    panMotor.write(0);              
    delay(1000);                        
    panMotor.write(90);
    delay(1000);
    exit(0);
  }
  break;
  case 0x11:
  delay(10000);
  delay(10000);
  delay(10000);
  delay(10000);
  delay(10000);
}
}
}

void compareDistance()
{
  if (leftDistance>rightDistance)
  {
    leftMotor.write(LBackward);
    rightMotor.write(RForward);
    delay(500);
  }
  else if (rightDistance>leftDistance)
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward);
    delay(500);
  }
  else
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward);
    delay(1000);
  }
}

long ping()
{
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  return duration / 29 / 2;
}

Following it, an error appeared about the void loop in the void loop.

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
Arduino: 1.0.6 (Windows 7), Board: "Arduino Uno"
Voice_Command_Code.ino: In function 'void loop()':
Voice_Command_Code:65: error: a function-definition is not allowed here before '{' token
  case 0x13:
  void loop(){

Right there.

So how do I fix it?

an error appeared about the void loop in the void loop.

You do know you are not supposed to do that don't you?
You can not define a function inside another function. Why do you think you want to do this?

You fix it by not doing it. What do you want to do here?

What I am trying to do is make the servo motor that is called pan motor spin from 180 to 0 once. Would I just enter that, because if I do, it loops and that is not what I want.

Thank you! :slight_smile:
I'm new to this BTW

So why is case 13 a function definition? What has the loop function have to do with case 13?

And what is case 11 supposed to do?

  case 0x11:
    delay(10000);
    delay(10000);
    delay(10000);
    delay(10000);
    delay(10000);

It will just jam up the whole machine for 50 seconds. It will not stop the robot, it is just you will loose control for a time. Why?

You seem to be using servo motors. Are these continuous rotation motors? If they are then you can not control how far they turn only how fast. If they are not then you are only going to get less than one revolution from your robot’s motors.

This compiles, now say what is wrong with it’s function.

#include <Servo.h>

const int RForward = 0; 
const int RBackward = 180; 
const int LForward = RBackward; 
const int LBackward = RForward; 
const int RNeutral = 90; 
const int LNeutral = 90; 
const int pingPin = 7;
const int irPin = 0;  
const int dangerThresh = 10; 
int leftDistance, rightDistance; 
int distanceFwd = ping();
int pos = 0;
Servo panMotor;  
Servo leftMotor;
Servo rightMotor;
long duration;
byte com = 0;

void setup (){
  Serial.begin(9600);
  delay(2000);
  Serial.write(0xAA);
  Serial.write(0x37);
  delay(1000);
  Serial.write(0xAA);
  Serial.write(0x21);
  rightMotor.attach(11);
  leftMotor.attach(10);
  panMotor.attach(6);
  panMotor.write(90);
}
void loop ()
{
  while(Serial.available())
  {
    com = Serial.read();
    switch(com)
    {
    case 0x12:
      if (distanceFwd>dangerThresh) //if path is clear
      {
        leftMotor.write(LForward); 
        rightMotor.write(RForward); //move forward
      }
      else //if path is blocked
      {
        leftMotor.write(LNeutral);
        rightMotor.write(RNeutral); 
        panMotor.write(0); 
        delay(500);
        rightDistance = ping(); //scan to the right
        delay(500);
        panMotor.write(180);
        delay(700);
        leftDistance = ping(); //scan to the left
        delay(500);
        panMotor.write(90); //return to center
        delay(100);
        compareDistance();
      }
      break;
    case 0x13:
      panMotor.write(180);              
      delay(1000);                                               
      panMotor.write(0);              
      delay(1000);                        
      panMotor.write(90);
      delay(1000);
      // exit(0); why?
      break;
    case 0x11:
      delay(10000);
      delay(10000); // don't be silly
      delay(10000);
      delay(10000);
      delay(10000);
    }
  }
}

void compareDistance()
{
  if (leftDistance>rightDistance)
  {
    leftMotor.write(LBackward);
    rightMotor.write(RForward);
    delay(500);
  }
  else if (rightDistance>leftDistance)
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward);
    delay(500);
  }
  else
  {
    leftMotor.write(LForward);
    rightMotor.write(RBackward);
    delay(1000);
  }
}

long ping()
{
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  return duration / 29 / 2;
}

case 13 is when the 3rd voice command is uttered and a signal is sent from the Geeetech Voice Recognition Module. How else would I go about moving the servo from 180 to 0 when saying the Voice Command Look Around?

Also I don't really know how to make any other command like that one. case 11 is the voice command Stop.

case 11 is the voice command Stop.

But it stops the program not the robot. To stop the robot you have to turn off all the motors.

So how would I go about doing that? Would I just make the motors keep turning to the same spot so they stay still? If I did do that, would that allow me to exit that mode?

And how would I go about performing the look around voice command?

And how would I go about performing the look around voice command?

Have you seen the code I posted three back?

Would I just make the motors keep turning to the same spot so they stay still?

No you stop the motors, what is wrong with that concept? Stopping means make them not move.

f I did do that, would that allow me to exit that mode?

That is the problem you have at the moment with what you have written. Once the motors are stopped then you are free to look at commands that make them move again.

Ok so I did notice the code, but how would I go about stopping the servo? It is not a continuous rotation servo btw, it is a Tower Pro 9g Servo Motor.

but how would I go about stopping the servo?

Servos stop when they have reached the desired pointing angle.

It is not a continuous rotation servo btw, it is a Tower Pro 9g Servo Motor.

But if your motors are servos then how is the robot moving? What do you want to stop when you say stop?

I think you are way over your head with the whole concept of this project and you have a very limited ability to describe what is going on. You should not get so far in writing code without testing it.

I would forget motors to begin with and just light up various LEDs with the spoken commands. Get that working and that will be a framework for future development.

Only then replace the LEDs with motors, normal DC motors not servo motors for the wheels, get the robot to go forward, back and stop on command.

Well my continuous motors for my project use three values for .write, 90 to stop moving, 180 to move counter clockwise, and 0 to move clockwise. Should I just say leftMotor.write(90); and rightMotor.write(90); and the center for the head of the robot, which is where it should be when I say stop, is 90, so panMotor(90);

Would that work?