Motor 2, question

So my project is a tad complex as previously mentioned on other posts, but i’m having a bit of an issue with the motor.

basically my program loops and depending on the value received from an arduino over serial, the 3pi will turn either left then straight, or right then straight.

The first time it loops through the call, it works perfectly fine, but the second time when the same command is sent, only motor 1 rotates and 2 does not move at all.

Looking at my code it all seems fine and there is no way it could be calling another function as there is nothing that would cause only one motor to move.

So I was wondering if you guys knew if it could possibly be that the pin motor 2 is connected to is busy with another process and therefore not responsive? kind of just baffles me.

below is just the code used in the switch case to determine orientation

  if (mySerial.available()) {
    someChar =;
    switch (someChar) {
    case 1: 
      motors.setSpeeds(60, -60);        // turn left
    case 2:
      motors.setSpeeds(-60, 60);        //turn right
    case 3:  
      motors.setSpeeds(0, 0);
      Serial.print(char(2));          //send 2 val to tell arduino that robot has stopped moving. 
      move = 0;
    case 4:
      for (int x = 0; x<100; x++){
        unsigned int position = robot.readLine(sensors, IR_EMITTERS_ON, 1);
        if (position < 1000)
          motors.setSpeeds(-200, 220);        // We are far to the right of the line: turn left.
        else if (position < 3000)
          motors.setSpeeds(40, 40);            // We are somewhat close to being centered on the line: drive left.
          motors.setSpeeds(200, -220);        // We are far to the left of the line: turn right.


Can you reduce your code to the simplest thing that demonstrates the problem and then post your complete program? It sounds like it only needs to be a few lines long to reproduce what you are describing (e.g. get rid of the switch statement and just have the 3pi do one action when it receives a serial command). Chances are that it will eventually start working properly as you simplify it, which will help you discover what the problem is.

- Ben

Hm, will try to cut it down.

Not so sure where to start. But I’ll be back to update.

Ok so it looks like the switch case and serial code is fine. And I have determined what was causing the issue. After the motor loop, I had a speaker loop which utilises the an Arduino tone library. Turns out this line of code which plays out a particular frequency through the speaker via pin 9, would mess up the motors for the next time it looped through.

  tone(19, NOTE_D6,noteDuration);

Any idea why it might have done that? is pin 19 attached to the motor?

from the Arduino website
"Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega)."

So i know Arduino isn’t really your area but any suggestions on how to then remove the tone() function after it’s called so the motor will work again?

You should avoid using Arduino libraries that are incompatible with the hardware we have on the 3pi. Our 3pi libraries already have functions for playing notes on the buzzer. Is there a reason you don’t want to just use those?

- Ben

yeh, i’m using the speaker because it provides slightly nicer tones and it works quite nicely with my current robot design.

In the end though, I managed to offload the speaker functionality onto the Arduino board itself. Thanks for all the replies :slight_smile: