Shop OBEX P1 Docs P2 Docs Learn Events
Boe-Bot with Arduino and Joystick: Works with USB, but Left wheel spins when USB unplugged. — Parallax Forums

Boe-Bot with Arduino and Joystick: Works with USB, but Left wheel spins when USB unplugged.

Hi I am working with a student and we have a behavior that has stumped us. I will try and get a video and a code snippet to share.

We have a Boe-Bot with the Arduino Uno and Shield. We placed the Arduino Joystick on a breadboard and hardwired the breadboard to the arduino with a 5 foot piece of Ethernet Wire.
The Code works when the USB cable to the computer is plugged in. But the moment the USB cable is unplugged the Left Wheel starts spinning full speed forward. The joystick still works; controls the right wheel and can be used to reverse the left wheel. Plugging the Arduino Uno back into the USB cable and the left wheel stops immediately and the Joystick Control returns normally.

Next time the student is in class I wll get a video of the behavior and post the code.

Comments

  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2016-05-17 23:56
    When the USB cable is plugged in, the Arduino gets its juice from the computer. When it unplugs, it looks for its supply from either the 5V line (not typically used for this purpose) or from its onboard regulator. When unplugged from the USB, you need to be sure the Arduino is powered from an adequate source. With servos, this requires at a minimum 7 volts from AA or AAA cells. The Arduino does not use a low drop out regulator, and if you supply it with only 6V (or worse, a 9V transistor battery, which lacks the current for running motors), the chip on the Arduino will likely repeatedly go into brown out. When this happens, its sketch keeps restarting.

    Whether or not this is the problem, here's a good chance to teach your student good troubleshooting skills. Here are some of the most common reasons for this kind of issue. Work out a method for isolating and testing each one.

    1. Poor (non-USB) power supply. Either the power isn't enough volts (should be 7V to 12V), not enough current, or both.

    2. If you are using the servo headers on the Arduino Shield, be sure they are jumpered properly. Options are 5V (their power comes from the Shield's onboard regulator), or Vin. Be careful of this setting if you are using >6V supply, and your servos are not rated for over 6V. Otherwise, the servos may be damaged, or could behave erratically.

    3. Check the switch position on the Shield. It should be in the "2" position when running servos. This is necessary for operation, and also to avoid damaging things. If you try to pulse a servo that is not powered, the servo could try to pull its current from the signal line. The Arduino cannot provide enough current for this. I recall, but can't be certain, that the shield includes current limiting resistors for the servo headers, but it's best not to tempt fate.

    4. Add a "sanity check" to the top of the Arduino code that lets you immediately identify if the board is going into brownout. One good method is to have the LED on pin 13 flash a few times upon startup, but not thereafter. If the LED keeps flashing, then you know the board is resetting.
  • adwoodsadwoods Posts: 3
    edited 2016-05-23 16:07
    Here is the code the student came up with:
    #include <Servo.h> // Include servo library

    Servo servoLeft; // Declare left and right servos
    Servo servoRight;

    int spLeftRight = A0; // select the input pin for the potentiometer
    int spUpDown = A1;

    int vLeftRight;
    int vUpDown;
    int clr;
    int cedUp;

    void setup() {
    // declare the ledPin as an OUTPUT:
    servoLeft.attach(12); // Attach left signal to pin 13
    servoRight.attach(13); // Attach right signal to pin 12
    tone(4, 3000, 1000); // Play tone for 1 second
    delay(1000);
    Serial.begin(9600);
    Serial.print("Starting");
    Serial.print("\n");
    }


    void loop() {
    // read the value from the sensor:
    vLeftRight= analogRead(spLeftRight)*(400/970.0)-200+14;
    vUpDown = analogRead(spUpDown)*(400/970.0)-200+21;

    clr=(1500+ (-1*(vUpDown-vLeftRight)));
    cedUp=(1500+ (vUpDown+vLeftRight));

    Serial.print("LeftRight: ");
    Serial.print( vLeftRight);
    Serial.print(" UpDown: ");
    Serial.print( vUpDown);
    Serial.print(" clr ");
    Serial.print(clr);
    Serial.print(" cedUp");
    Serial.print(cedUp);
    Serial.print("\n");

    if ((abs(vUpDown)+abs(vLeftRight))>40)
    {
    servoLeft.writeMicroseconds(clr); // Left wheel counterclockwise
    servoRight.writeMicroseconds(cedUp); // Right wheel clockwise

    }
    else
    {
    servoLeft.writeMicroseconds(1500); // Left wheel counterclockwise
    servoRight.writeMicroseconds(1500);
    }
    //delay(0); // ...for 0 seconds

    }
  • GordonMcCombGordonMcComb Posts: 3,366
    edited 2016-05-23 17:27
    As the code has a run-once setup routine that would indicate if the Arduino is being reset, knowing the behavior of that would be helpful in going forward.

    In addition or in replacement of the tone, I'd flash the LED a few times at startup. The tone is okay, but the LED gets things going much faster. A couple of rapid flashes is enough, and there's no reason to pause for a second before starting with the rest of the sketch. The more obvious it is that the Arduino might be browning out the better.
  • adwoodsadwoods Posts: 3
    edited 2016-05-23 19:00
Sign In or Register to comment.