Arduino robot kit – Motor library

      8 Comments on Arduino robot kit – Motor library

Once the motors were connected to the motor controller (see previous post), I started working on writing the code for basic robot movement. The basic motor commands are:

  • onFwd/onRev: move each motor independently in the forward or reverse direction
  • off/stop: stop either motor gradually (float) or abruptly (break)
  • fwdRight/fwdLeft: turn the robot by running the two motors at different speeds in the forward direction
  • revRight/revLeft: turn the robot by running the two motors at different speeds in the reverse direction
  • turnRight/turnLeft: turn the robot by running the two motors in the opposite directions

The names of these functions are inspired by the motor functions in NQC and  NXC, which I use for the LEGO Mindstorms Robots.

Each of these functions manipulate the three control signals for each motor as shown in the this code snippet:

void Motor::onFwd(int motor, int speed) {
	int val = map(speed, 0, 100, 0, 255);
	switch (motor) {
	case Motor_R: // Right Motor
		analogWrite (enbPinA, val);
		digitalWrite (fwdPinA, HIGH); 
		digitalWrite (bkdPinA, LOW);
		break;
	case Motor_L: // Left Motor
                ...

Finally, the Motor control library can be downloaded from here.

8 thoughts on “Arduino robot kit – Motor library

  1. maen Post author

    Andrew,
    – make sure that the batteries are fully charged.
    – make sure that jumpers VR1-VR4 and 5V_EN in the controller card are OPEN and jumpers CSA and CSB are CLOSED.
    – upload the sketch below for direct control of the motors. The sketch will turn the motors for 5 sec intervals. if it is still slow, remove one of the motors on each side so only one motor is connected to the A and B sides in the controller

    // Robot Motor Test
    
    //Motor A 
    int dir1PinA = 7;
    int dir2PinA = 5; 
    int speedPinA = 6;
    
    //motor B 
    int dir1PinB = 4;
    int dir2PinB = 2; 
    int speedPinB = 3;
    
    int speed = 255; 
    int dir; 
    int motor;
    int time = 5000;
    
    void setup () 
    {
    pinMode (dir1PinA, OUTPUT); 
    pinMode (dir2PinA, OUTPUT); 
    pinMode (speedPinA, OUTPUT); 
    pinMode (dir1PinB, OUTPUT); 
    pinMode (dir2PinB, OUTPUT); 
    pinMode (speedPinB, OUTPUT); 
    
    } 
    
    void loop () {
    
    // all motors forward for 10 secounds
    analogWrite (speedPinA, speed); 
    analogWrite (speedPinB, speed);
    digitalWrite (dir1PinA , HIGH); 
    digitalWrite (dir2PinA, LOW); 
    digitalWrite (dir1PinB, HIGH); 
    digitalWrite (dir2PinB, LOW);  
    delay(time);
    // all motors backward for 10 secounds 
    analogWrite (speedPinA, speed); 
    analogWrite (speedPinB, speed);
    digitalWrite (dir1PinA, LOW); 
    digitalWrite (dir2PinA, HIGH); 
    digitalWrite (dir1PinB, LOW); 
    digitalWrite (dir2PinB, HIGH);
    delay(time);
    // all motors off for 10 secounds 
    digitalWrite (speedPinA, LOW); 
    digitalWrite (speedPinB, LOW);
    digitalWrite (dir1PinA , LOW); 
    digitalWrite (dir2PinA, LOW); 
    digitalWrite (dir1PinB, LOW); 
    digitalWrite (dir2PinB, LOW);
    delay(time);
    // right motors forward for 10 secounds
    analogWrite (speedPinA, speed); 
    analogWrite (speedPinB, LOW);
    digitalWrite (dir1PinA , HIGH); 
    digitalWrite (dir2PinA, LOW); 
    digitalWrite (dir1PinB, HIGH); 
    digitalWrite (dir2PinB, LOW);  
    delay(time);
    // left motors forward for 10 secounds
    analogWrite (speedPinA, LOW); 
    analogWrite (speedPinB, speed);
    digitalWrite (dir1PinA , HIGH); 
    digitalWrite (dir2PinA, LOW); 
    digitalWrite (dir1PinB, HIGH); 
    digitalWrite (dir2PinB, LOW);  
    delay(time);
    // all motors hard stop for 10 secounds 
    digitalWrite (speedPinA, 255); 
    digitalWrite (speedPinB, 255);
    digitalWrite (dir1PinA , LOW); 
    digitalWrite (dir2PinA, LOW); 
    digitalWrite (dir1PinB, LOW); 
    digitalWrite (dir2PinB, LOW);
    delay(time);
    }
    
    
  2. Andrew

    when i set speed >100 car dont runcompletly (in air)
    when i set speed <100 and use motor.onFwd(0,90) only left motors work and run very slow…. I completly check all cables and all contected by your scheme

  3. maen Post author

    The motor should run at any speed without friction (left the car in the air). Make sure the batteries are charged and the wiring is correct.

  4. maen Post author

    Andrew,
    The speed parameter in the motor commands takes a value from 0 to 100 and maps it to 0 to 255 (for PWM). So 100 means 100% power and 50 means 50% power and so on. Maybe I should have called the parameter power instead of speed to be accurate. I found that the robot needs to be on 100% power to turn properly on carpet. In general, I think the battery is not providing enough power for all four motors and the fraction caused by the imperfectly aligned wheels is too high.

  5. Andrew

    One morequestion about Motor library, how can i change the speed ofcar. itryto use motor.onFwd(0,50), motor.onFwd(0,100), motor.onFwd(0,150), motor.onFwd(0,200) but car dont work fine and dont change the speed. Can you say something about that?

  6. Andrew

    Hi, can you give full scheme of this car, i cant understend some things with motors. And pls make some more tutorials for this, i cant completly make it work. Thank you

Comments are closed.