Smart car arduino(robot car)

Hi, I have tried to google and youtube everything about this little problem. I have tried to declare the motordriver outside the void setup and inside the void setup and i have tried to change it to "MotorDriver" and every suggetions it has come up with. it was a code i once got from the seller that i bought it from. It worked great back then, but now after i have used the arduino for something else, and just wanted to upload it and give it to my 4 year old nephew, it won't work. can you guys please help me out, so I can give it to my nephew :pray:

the problem is that it says motordriver isn't declared and i have tried everything to declare it and nothing has worked and the seller doesn't answer me, so i really hope you can help me out?

`//test 1:2015.08.15
#include <car_bluetooth.h>
#include <SoftwareSerial.h>   //Software Serial Port
#include <String.h>
#include "MotorDriver.h"

#define RxD 2
#define TxD 4
CarBluetooth bluetooth(RxD, TxD);
#define CMD_INVALID     0XFF
#define CMD_FORWARD     'F'
#define CMD_RIGHT_FRONT 'R'

#define CMD_BACKWARD    'B'

#define CMD_LEFT_FRONT  'L'
#define CMD_STOP        'S'

#define SPEED_STEPS 20
uint8_t speed0 = 100;
void setup(){
	Serial.begin(9600);
  motordriver.init();
  motordriver.setSpeed(100,MOTORA);
  motordriver.setSpeed(100,MOTORB);
  bluetooth.waitPairable();
  bluetooth.waitConnected();
}
uint8_t bt_command;

#define CAR_STOP 0
#define CAR_FORWARD 1
#define CAR_BACK 2
uint8_t car_status = CAR_STOP;
uint8_t new_status = car_status;

void loop(){
  bt_command = bluetooth.readByte();
  if(bt_command != CMD_INVALID){
  controlCar(bt_command);
  }
  if(bluetooth.getStatus() == PAIRABLE) {
  	motordriver.stop();
  	bluetooth.waitConnected();
  	}
}


void controlCar(uint8_t cmd){
   switch(cmd)
  {
    case CMD_FORWARD:     motordriver.goForward();break;
  case CMD_RIGHT_FRONT: 
  //  if(car_status != CAR_STOP)new_status = CAR_FORWARD;
    motordriver.goRight();
   // delay(200); 
    break;
  case CMD_BACKWARD:    motordriver.goBackward(); break;
  case CMD_LEFT_FRONT:  
    motordriver.goLeft();
   // delay(200);
    break;
  case CMD_STOP:        motordriver.stop();break;
  default: break;
  }
  if((cmd>='0')&&(cmd<='9'))
  {
  	speed0 = cmd-0x30;
	Serial.print(speed0);
	Serial.print(">");
	speed0 = map(speed0, 0, 9, 0, 255);
	Serial.println(speed0);
	motordriver.setSpeed(speed0,MOTORA);
    motordriver.setSpeed(speed0,MOTORB);
  }
}

void speedUp(){
  if(speed0 < 236)speed0 += SPEED_STEPS;
  else speed0 = 255;
  motordriver.setSpeed(speed0,MOTORA);
  motordriver.setSpeed(speed0,MOTORB);
}

void speedDown(){
  if(speed0 > 70)speed0 -= SPEED_STEPS;
  else speed0 = 50;
   motordriver.setSpeed(speed0,MOTORA);
  motordriver.setSpeed(speed0,MOTORB);
}

bluebee:64:5: error: 'motordriver' was not declared in this scope

 motordriver.stop();

 ^~~~~~~~~~~

C:\Users\krist\OneDrive\Documents\Arduino\Arduino\bluebee\bluebee.ino:64:5: note: suggested alternative: 'MotorDriver'

 motordriver.stop();

 ^~~~~~~~~~~

 MotorDriver

C:\Users\krist\OneDrive\Documents\Arduino\Arduino\bluebee\bluebee.ino: In function 'void controlCar(uint8_t)':

bluebee:147:7: error: 'motordriver' was not declared in this scope

   motordriver.goForward();

   ^~~~~~~~~~~

C:\Users\krist\OneDrive\Documents\Arduino\Arduino\bluebee\bluebee.ino:147:7: note: suggested alternative: 'MotorDriver'

   motordriver.goForward();

   ^~~~~~~~~~~

   MotorDriver

bluebee:177:5: error: 'motordriver' was not declared in this scope

 motordriver.setSpeed(speed0, MOTORA);

 ^~~~~~~~~~~

C:\Users\krist\OneDrive\Documents\Arduino\Arduino\bluebee\bluebee.ino:177:5: note: suggested alternative: 'MotorDriver'

 motordriver.setSpeed(speed0, MOTORA);

 ^~~~~~~~~~~

 MotorDriver

bluebee:177:34: error: 'MOTORA' was not declared in this scope

 motordriver.setSpeed(speed0, MOTORA);

                              ^~~~~~

C:\Users\krist\OneDrive\Documents\Arduino\Arduino\bluebee\bluebee.ino:177:34: note: suggested alternative: 'MOTOR2_A'

 motordriver.setSpeed(speed0, MOTORA);

                              ^~~~~~

                              MOTOR2_A

bluebee:178:34: error: 'MOTORB' was not declared in this scope

 motordriver.setSpeed(speed0, MOTORB);

                              ^~~~~~

C:\Users\krist\OneDrive\Documents\Arduino\Arduino\bluebee\bluebee.ino:178:34: note: suggested alternative: 'MOTOR2_B'

 motordriver.setSpeed(speed0, MOTORB);

                              ^~~~~~

                              MOTOR2_B

C:\Users\krist\OneDrive\Documents\Arduino\Arduino\bluebee\bluebee.ino: In function 'void speedUp()':

bluebee:185:3: error: 'motordriver' was not declared in this scope

motordriver.setSpeed(speed0, MOTORA);

^~~~~~~~~~~

C:\Users\krist\OneDrive\Documents\Arduino\Arduino\bluebee\bluebee.ino:185:3: note: suggested alternative: 'MotorDriver'

motordriver.setSpeed(speed0, MOTORA);

^~~~~~~~~~~

MotorDriver

bluebee:185:32: error: 'MOTORA' was not declared in this scope

motordriver.setSpeed(speed0, MOTORA);

                            ^~~~~~

C:\Users\krist\OneDrive\Documents\Arduino\Arduino\bluebee\bluebee.ino:185:32: note: suggested alternative: 'MOTOR2_A'

motordriver.setSpeed(speed0, MOTORA);

                            ^~~~~~

                            MOTOR2_A

bluebee:186:32: error: 'MOTORB' was not declared in this scope

motordriver.setSpeed(speed0, MOTORB);

                            ^~~~~~

C:\Users\krist\OneDrive\Documents\Arduino\Arduino\bluebee\bluebee.ino:186:32: note: suggested alternative: 'MOTOR2_B'

motordriver.setSpeed(speed0, MOTORB);

                            ^~~~~~

                            MOTOR2_B

C:\Users\krist\OneDrive\Documents\Arduino\Arduino\bluebee\bluebee.ino: In function 'void speedDown()':

bluebee:192:3: error: 'motordriver' was not declared in this scope

motordriver.setSpeed(speed0, MOTORA);

^~~~~~~~~~~

C:\Users\krist\OneDrive\Documents\Arduino\Arduino\bluebee\bluebee.ino:192:3: note: suggested alternative: 'MotorDriver'

motordriver.setSpeed(speed0, MOTORA);

^~~~~~~~~~~

MotorDriver

bluebee:192:32: error: 'MOTORA' was not declared in this scope

motordriver.setSpeed(speed0, MOTORA);

                            ^~~~~~

C:\Users\krist\OneDrive\Documents\Arduino\Arduino\bluebee\bluebee.ino:192:32: note: suggested alternative: 'MOTOR2_A'

motordriver.setSpeed(speed0, MOTORA);

                            ^~~~~~

                            MOTOR2_A

bluebee:193:32: error: 'MOTORB' was not declared in this scope

motordriver.setSpeed(speed0, MOTORB);

                            ^~~~~~

C:\Users\krist\OneDrive\Documents\Arduino\Arduino\bluebee\bluebee.ino:193:32: note: suggested alternative: 'MOTOR2_B'

motordriver.setSpeed(speed0, MOTORB);

                            ^~~~~~

                            MOTOR2_B

Using library CarBluetooth in folder: C:\Users\krist\OneDrive\Documents\Arduino\libraries\CarBluetooth (legacy)

Using library SoftwareSerial at version 1.0 in folder: C:\Users\krist\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\SoftwareSerial

Using library Motor_Driver_Library-1.0.0 at version 1.0.0 in folder: C:\Users\krist\OneDrive\Documents\Arduino\libraries\Motor_Driver_Library-1.0.0

Using library Wire at version 1.0 in folder: C:\Users\krist\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire

Using library Robot_Control at version 1.0.4 in folder: C:\Program Files (x86)\Arduino\libraries\Robot_Control

Using library SPI at version 1.0 in folder: C:\Users\krist\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\SPI

exit status 1

'motordriver' was not declared in this scope

You have not declared/defined any object named "motordriver", which would likely be associated with the MotorDriver.h library you included.

Look at the library examples to see how it is declared and used. You may need a motordriver.begin() call as well. Better, find the original code. It is probably on the web somewhere.

I did find this library from MotorDriver.h
Can you tell me anything that might help me out? I have already added motor.begin(); and thank you for the help. i have searched the entire internet and contacted the seller who has the original code. but i have the original code too and it says the same :frowning:
and i really wanna give the car to my nephew, since he really wanna play with it and have some fun with it :slight_smile: so i appreciate every help very much :pray:

#include "Arduino.h"

#ifndef MotorDriver_h

#define MotorDriver_h
#define MOTORLATCH 12
#define MOTORCLK 4
#define MOTORENABLE 7
#define MOTORDATA 8

// 8-bit bus after the 74HC595 shift register
// (not Arduino pins)
// These are used to set the direction of the bridge driver.
#define MOTOR1_A 2
#define MOTOR1_B 3
#define MOTOR2_A 1
#define MOTOR2_B 4
#define MOTOR3_A 5
#define MOTOR3_B 7
#define MOTOR4_A 0
#define MOTOR4_B 6

// Arduino pins for the PWM signals.
#define MOTOR1_PWM 11
#define MOTOR2_PWM 3
#define MOTOR3_PWM 6
#define MOTOR4_PWM 5
#define SERVO1_PWM 10
#define SERVO2_PWM 9

// Codes for the motor function.
#define FORWARD 1
#define BACKWARD 2
#define BRAKE 3
#define RELEASE 4

Please post MotorDriver.cpp, the executable part of the library.

but i have the original code too and it says the same

Unlikely. Post all of that code. It can't possibly work unless you define the motordriver object.

no back then at that time i bought it and just uploaded the code onto arduini, it worked just fine. but now that i tried to upload it again, it doesn't work anymore neither does the original code anymore. but i have the cap here

#include "Arduino.h"
#include "MotorDriver.h"

MotorDriver::MotorDriver()
{
  
}

void MotorDriver::motor(int nMotor, int command, int speed)
{
  int motorA, motorB;

  if (nMotor >= 1 && nMotor <= 4)
  {  
    switch (nMotor)
    {
    case 1:
      motorA   = MOTOR1_A;
      motorB   = MOTOR1_B;
      break;
    case 2:
      motorA   = MOTOR2_A;
      motorB   = MOTOR2_B;
      break;
    case 3:
      motorA   = MOTOR3_A;
      motorB   = MOTOR3_B;
      break;
    case 4:
      motorA   = MOTOR4_A;
      motorB   = MOTOR4_B;
      break;
    default:
      break;
    }

    switch (command)
    {
    case FORWARD:
      motor_output (motorA, HIGH, speed);
      motor_output (motorB, LOW, -1);     // -1: no PWM set
      break;
    case BACKWARD:
      motor_output (motorA, LOW, speed);
      motor_output (motorB, HIGH, -1);    // -1: no PWM set
      break;
    case BRAKE:
      // The AdaFruit library didn't implement a brake.
      // The L293D motor driver ic doesn't have a good
      // brake anyway.
      // It uses transistors inside, and not mosfets.
      // Some use a software break, by using a short
      // reverse voltage.
      // This brake will try to brake, by enabling 
      // the output and by pulling both outputs to ground.
      // But it isn't a good break.
      motor_output (motorA, LOW, 255); // 255: fully on.
      motor_output (motorB, LOW, -1);  // -1: no PWM set
      break;
    case RELEASE:
      motor_output (motorA, LOW, 0);  // 0: output floating.
      motor_output (motorB, LOW, -1); // -1: no PWM set
      break;
    default:
      break;
    }
  }
}


// ---------------------------------
// motor_output
//
// The function motor_ouput uses the motor driver to
// drive normal outputs like lights, relays, solenoids, 
// DC motors (but not in reverse).
//
// It is also used as an internal helper function 
// for the motor() function.
//
// The high_low variable should be set 'HIGH' 
// to drive lights, etc.
// It can be set 'LOW', to switch it off, 
// but also a 'speed' of 0 will switch it off.
//
// The 'speed' sets the PWM for 0...255, and is for 
// both pins of the motor output.
//   For example, if motor 3 side 'A' is used to for a
//   dimmed light at 50% (speed is 128), also the 
//   motor 3 side 'B' output will be dimmed for 50%.
// Set to 0 for completelty off (high impedance).
// Set to 255 for fully on.
// Special settings for the PWM speed:
//    Set to -1 for not setting the PWM at all.
//
void MotorDriver :: motor_output (int output, int high_low, int speed)
{
  int motorPWM;

  switch (output)
  {
  case MOTOR1_A:
  case MOTOR1_B:
    motorPWM = MOTOR1_PWM;
    break;
  case MOTOR2_A:
  case MOTOR2_B:
    motorPWM = MOTOR2_PWM;
    break;
  case MOTOR3_A:
  case MOTOR3_B:
    motorPWM = MOTOR3_PWM;
    break;
  case MOTOR4_A:
  case MOTOR4_B:
    motorPWM = MOTOR4_PWM;
    break;
  default:
    // Use speed as error flag, -3333 = invalid output.
    speed = -3333;
    break;
  }

  if (speed != -3333)
  {
    // Set the direction with the shift register 
    // on the MotorShield, even if the speed = -1.
    // In that case the direction will be set, but
    // not the PWM.
    shiftWrite(output, high_low);

    // set PWM only if it is valid
    if (speed >= 0 && speed <= 255)    
    {
      analogWrite(motorPWM, speed);
    }
  }
}


// ---------------------------------
// shiftWrite
//
// The parameters are just like digitalWrite().
//
// The output is the pin 0...7 (the pin behind 
// the shift register).
// The second parameter is HIGH or LOW.
//
// There is no initialization function.
// Initialization is automatically done at the first
// time it is used.
//
void MotorDriver:: shiftWrite(int output, int high_low)
{
  static int latch_copy;
  static int shift_register_initialized = false;

  // Do the initialization on the fly, 
  // at the first time it is used.
  if (!shift_register_initialized)
  {
    // Set pins for shift register to output
    pinMode(MOTORLATCH, OUTPUT);
    pinMode(MOTORENABLE, OUTPUT);
    pinMode(MOTORDATA, OUTPUT);
    pinMode(MOTORCLK, OUTPUT);

    // Set pins for shift register to default value (low);
    digitalWrite(MOTORDATA, LOW);
    digitalWrite(MOTORLATCH, LOW);
    digitalWrite(MOTORCLK, LOW);
    // Enable the shift register, set Enable pin Low.
    digitalWrite(MOTORENABLE, LOW);

    // start with all outputs (of the shift register) low
    latch_copy = 0;

    shift_register_initialized = true;
  }

  // The defines HIGH and LOW are 1 and 0.
  // So this is valid.
  bitWrite(latch_copy, output, high_low);

  // Use the default Arduino 'shiftOut()' function to
  // shift the bits with the MOTORCLK as clock pulse.
  // The 74HC595 shiftregister wants the MSB first.
  // After that, generate a latch pulse with MOTORLATCH.
  shiftOut(MOTORDATA, MOTORCLK, MSBFIRST, latch_copy);
  delayMicroseconds(5);    // For safety, not really needed.
  digitalWrite(MOTORLATCH, HIGH);
  delayMicroseconds(5);    // For safety, not really needed.
  digitalWrite(MOTORLATCH, LOW);
}

This is what instantiates the object, and it is named MotorDriver, not motordriver. Character case is vitally important!

i have tried to replace every "motordrive" with "MotorDrive" and then it said this. i did the search function and replaced all "motordriver" with "MotorDriver"

bluebee:186:14: error: expected unqualified-id before '.' token
   MotorDriver.setSpeed(speed0, MOTORB);
              ^
C:\Users\krist\OneDrive\Documents\Arduino\Arduino\bluebee\bluebee.ino: In function 'void speedDown()':
bluebee:192:14: error: expected unqualified-id before '.' token
   MotorDriver.setSpeed(speed0, MOTORA);
              ^
bluebee:193:14: error: expected unqualified-id before '.' token
   MotorDriver.setSpeed(speed0, MOTORB);
              ^
Using library CarBluetooth in folder: C:\Users\krist\OneDrive\Documents\Arduino\libraries\CarBluetooth (legacy)
Using library SoftwareSerial at version 1.0 in folder: C:\Users\krist\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\SoftwareSerial 
Using library Motor_Driver_Library-1.0.0 at version 1.0.0 in folder: C:\Users\krist\OneDrive\Documents\Arduino\libraries\Motor_Driver_Library-1.0.0 
Using library Wire at version 1.0 in folder: C:\Users\krist\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\Wire 
Using library Robot_Control at version 1.0.4 in folder: C:\Program Files (x86)\Arduino\libraries\Robot_Control 
Using library SPI at version 1.0 in folder: C:\Users\krist\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.5\libraries\SPI 
exit status 1
expected unqualified-id before '.' token

You must be missing some other file. Nothing defines/declares the class MotorDriver.

The code you posted for MotorDriver.h is not complete and does not compile without errors.

have you looked at the example

link posted by @gcjr above has the missing part of MotorDriver.h, which has what you need. Try downloading that library and run the minimal example.

#include "Arduino.h"

#ifndef MotorDriver_h

#define MotorDriver_h
#define MOTORLATCH 12
#define MOTORCLK 4
#define MOTORENABLE 7
#define MOTORDATA 8

// 8-bit bus after the 74HC595 shift register
// (not Arduino pins)
// These are used to set the direction of the bridge driver.
#define MOTOR1_A 2
#define MOTOR1_B 3
#define MOTOR2_A 1
#define MOTOR2_B 4
#define MOTOR3_A 5
#define MOTOR3_B 7
#define MOTOR4_A 0
#define MOTOR4_B 6

// Arduino pins for the PWM signals.
#define MOTOR1_PWM 11
#define MOTOR2_PWM 3
#define MOTOR3_PWM 6
#define MOTOR4_PWM 5
#define SERVO1_PWM 10
#define SERVO2_PWM 9

// Codes for the motor function.
#define FORWARD 1
#define BACKWARD 2
#define BRAKE 3
#define RELEASE 4






class MotorDriver
{

public:
	MotorDriver();
	void motor(int nMotor, int command, int speed);
private:
	void shiftWrite(int output, int high_low);
	void motor_output (int output, int high_low, int speed);
  
};

#endif

EDIT: not the same library. Closely related though. Look for others.

Hello @KRIST3R - Try un-installing (or removing) the library you used, and then delete the sketch folder. Then, download and install the original library and original sketch.

Please, post the picture of the Motor Driver you are using.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.