Redbot Ultrasonic servo code

I am having trouble with this code. Thoughts on how i can fix it to run?

/***********************************************************************
UNB ECE RedBot Servo Pan Ultrasonic HC-SR04 Demo
Written by Troy T. Lavigne (UNB ECE)- Feb 11, 2015
Visit our website at http://www.unb.ca/fredericton/engineering/depts/ece/outreach/redbots/

Thanks to: 30 Jul 2013 by Mike Hord @ SparkFun Electronics.

The HC-SR04 can be purchased from DealExtreme at http://www.dx.com/p/hc-sr04-ultrasonic-sensor-distance-measuring-module-133696

This demo uses a "panning" HC-SR04 Ultrasonic Sensor Distance Measuring Module
to randomly drive around a room and hopefully avoid objects! Turning to the 
optimal angle is performed by using a SparkFun RedBot Sensor - Wheel Encoder.

Right and Left motor encoder inputs must be connected to:
=========================================================
3 - Right wheel encoder
A2 - Left wheel encoder

If your connection is to different pins, simply alter the RedBotEncoder
pins to match the pins that your encoder is connected to.

Servo connection:
=================
A0 - white wire
5V - red wire
GND - black wire

HC-SR04 Ultrasonic connection:
=============================
RedBot  HC-SR04
A4      Trig
A5      Echo
5V      VCC
GND     GND

***********************************************************************/
// Include the libraries. We make a provision for using the Xbee header
//  via software serial to report values, but that's not really used in
//  the code anywhere.
#include <RedBot.h>
#include <Servo.h> 

// Pin connections for the HC-SR04 module
#define trigPin A4
#define echoPin A5

// Instantiate the motor control class. This only needs to be done once
//  and indeed SHOULD only be done once!
RedBotMotor motor;

// Instantiate our encoder. We will use the encoder to perform direction changes.
RedBotEncoder encoder = RedBotEncoder(10, A2); // right, left

Servo panservo;  // create servo object to control a servo


// No two motors/wheels are identical, so alter these numbers to drive a straight line
#define fwdSpdL 125 // Tune for robot/battery condition.
#define fwdSpdR 110 // Tune for robot/battery condition.

#define mindistance 25         // stop when mindistance cm away from an object dead ahead
#define numsamples 3           // Take multiple Ping sensor samples to average
#define DegreesPerSample 5     // Define the angle change per sample
//#define wheel_circumference 20.3   // wheel circumference is ~20.3cm
//#define wheel_distance 10.2      // wheel to wheel distance is ~10.2cm
#define pi 3.14159              // Mmmm. Pie.
#define circumference 2*pi*10.2 // Circumference of the robot driving in a circle  is = 2*pi*radius
#define encoderL 1.27           // 1 encoder tick = wheel circumference/16 = 20.3cm/16 = 1.27cm/tick approximately

int pos = 90;        // variable to store the servo position 
int pannull = 90;    // variable to store the servo position
int panangle = 0;
int tiltnull = 50;   // variable to store the servo position 
int idealangle = 0;  // Direction of most distant object
float angle = 0;
float count = 0;
float alpha;
float length;
long cm, averagecm;
long duration, centimeters; // for HC-SR04
long idealcm = 0;


void setup()
{
  Serial.begin(57600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);  
  Serial.println("Starting RedBot Servo Pan Ultrasonic Demo Program!");  
  //Serial.println();
  
  panservo.attach(A0); // Attaches the servo on pin 3 to the servo object
  panservo.write(100);  // Tell servo to turn the ultrasonic sensor straight ahead.
  delay(1000);  
    
}


void loop()
{
  
  idealcm = 0;
  idealangle = 0;   

  // Find the best path (farthest obstacle)
  for (panangle = 10; panangle < 170; panangle=panangle+DegreesPerSample) {
    panservo.write(panangle);              // tell servo to go to position in variable 'pos' 
    averagecm = 0;
    for (int x = 1; x < numsamples; x++) {
      cm = hcsr04();
      //Serial.println("Sample Read");  
      //Serial.println();      
      averagecm = averagecm + cm;
      delay (10);      // wait 10ms
    }
    cm = averagecm / numsamples;
   
    if (cm > idealcm) {
        idealcm = cm;
        idealangle = panangle;
    }
  }  
  
  // if the entire path is blocked, turn 180 Degrees
  if (idealcm < mindistance)
  {
    //Serial.println("Blocked path, turning around");
    length = circumference/4;
    count = length/encoderL; // Convert distance to encoder ticks.
    while (encoder.getTicks(LEFT) < count)
    {   
       motor.leftDrive(fwdSpdL);
       motor.rightDrive(-fwdSpdR);
       delay(100);       
    }  
    
  } else {    
    motor.leftDrive(0);
    motor.rightDrive(0);
       
    //Serial.print("Ideal panangle is ");
    //Serial.println(idealangle);
    
    // Positive number means turn to the left.
    // Negative number means turn to the right.
    //Serial.print("Distance from null and count: ");
    angle = pannull-idealangle;
    
    length = float((angle*circumference)/360);
    count = length/encoderL; // Convert distance to encoder ticks.
    
//    Serial.print(angle);  
//    Serial.print(" ");  
//    Serial.print(count);
//    Serial.print(" ");  
//    Serial.println(length);   
    delay(250);
    panservo.write(idealangle);
    delay(250); // Wait after pointing the servo in the directin we want to go
    
    encoder.clearEnc(BOTH);
    if (count > 0) // turn the right wheel forward until count = encoder.getTicks(RIGHT)
    {
      while (encoder.getTicks(RIGHT) < count)
      {   
         motor.leftDrive(0);
         motor.rightDrive(fwdSpdR);
      }
    } else {
      count = abs(count);
      while (encoder.getTicks(LEFT) < count)
      {   
         motor.leftDrive(fwdSpdL);
         motor.rightDrive(0);
         delay(100);       
      }
    }
  
  // stop both wheels, wait a short period of time then drive forward until an object is detected less than the minimum distance away.
    motor.leftDrive(0);
    motor.rightDrive(0);
    panservo.write(pannull);  // tell servo to point straight ahead 
    delay(100);
    
     averagecm = 0;
     for (int x = 1; x < numsamples; x++) {
        cm = hcsr04();
        averagecm = averagecm + cm;
        delay (10);      
     }
     cm = averagecm / numsamples;
  
//    Serial.print("Distance to object is (cm):");
//    Serial.println(cm);
    
    while (cm > mindistance) 
    {
      motor.leftDrive(fwdSpdL);
      motor.rightDrive(fwdSpdR);
      
      averagecm = 0;
      for (int x = 1; x < numsamples; x++) {
        cm = hcsr04();
        averagecm = averagecm + cm;
        delay (10);     
      }
     cm = averagecm / numsamples;
     
//      Serial.print("Distance to object is (cm):");
//      Serial.println(cm);
      //delay(50);
    }
    
    motor.leftDrive(0);
    motor.rightDrive(0); 
  }    
 
}

long hcsr04()
{
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  centimeters = (duration/2) / 29.1;
  Serial.print(centimeters);
  Serial.println(" cm");

  return centimeters;
}

this is the error i keep getting. All other codes that i upload to the redbot that reference "RedBotMotor motor;" work fine so its not a library issue. I am trying to avoid having to write my own similar code.

Arduino: 1.6.7 (Windows 10), Board: "Arduino/Genuino Uno"

ServoPanHCSR04Ultra:49: error: 'RedBotMotor' does not name a type

 RedBotMotor motor;

 ^

C:\Users\Home\Dropbox\ServoPanHCSR04Ultra\ServoPanHCSR04Ultra.ino: In function 'void loop()':

ServoPanHCSR04Ultra:132: error: 'motor' was not declared in this scope

        motor.leftDrive(fwdSpdL);

        ^

ServoPanHCSR04Ultra:138: error: 'motor' was not declared in this scope

     motor.leftDrive(0);

     ^

exit status 1
'RedBotMotor' does not name a type

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

Ok I finally got this to work. Apparently Sparkfun changed their library and it mess the code up. Basically
everywhere it says motor.leftDrive () and motor.rightDrive() it actually needs to say motors.leftDrive()and motors.rightDrive(). All of the “s” are added in the new code

Also the “RedBotMotor motor;” needs to be “RedBotMotors motors;”

now it works :slight_smile: