3pi trouble

Hi, I just bought a 3pi Robot and I’ve haven’t been able to program it very effectively. I program using the Arduino enviorment and have downloaded the libraries, but only a few of them worked. I’m now using the Arduino LCD library and I got the sensor library working, but now I can’t get the buttons to work. When I uploaded the example code that came with the library it ran once, but if i pressed a button after that it wouldn’t register. Could someone provide me with a code that would work with the buttons without using the library. I tried writing my own but it didn’t work.

Here’s the example code

#include <OrangutanLCD.h>
#include <OrangutanPushbuttons.h>

 * OrangutanPushbuttonExample for the 3pi robot or Orangutan LV-168
 * This example program is indended for use on the 3pi or Orangutan LV-168.
 * It uses the OrangutanPushbuttons library to detect user input from the
 * pushbuttons, and it uses the OrangutanLCD library to display feedback
 * on the LCD.
 * https://www.pololu.com/docs/0J17/5.f
 * https://www.pololu.com
 * https://forum.pololu.com

OrangutanPushbuttons buttons;
OrangutanLCD lcd;

void setup()                    // run once, when the sketch starts

void loop()                     // run over and over again
  // wait for either the top or bottom buttons to be pressed
  // store the value of the pressed button in the variable 'button'
  unsigned char button = buttons.waitForPress(TOP_BUTTON | BOTTOM_BUTTON);
  if (button == TOP_BUTTON)     // display the button that was pressed
    lcd.print("top down");
    lcd.print("bot down");
  buttons.waitForRelease(button);  // wait for that button to be released
  lcd.print("released");      // display that the button was released


The buttons are on the same I/O lines as the LCD data pins, which I’m assuming is why they stop working when you use the Arduino LCD routines. The Pololu LCD library changes the state of the LCD data pins to send data, but then it restores them to whatever state they had previously so they can be used for secondary purposes like controlling LEDs or reading buttons. The Arduino LCD library most likely doesn’t bother recording and restoring the previous states of these lines. This would conflict with the Pololu button library because it only initializes the button I/O lines as inputs once and then assumes they will remain inputs with pull-ups enabled ever after.

You could easily write your own code to read the buttons; it just requires looking at a digital input to see if it’s high or low. Just make sure to set the pin as a digital input with the internal pull-up enabled and give the voltage on the line at least a few hundred nanoseconds to stablize (a microsecond should be safe) before you check the input value. You should also use some delays to debounce the buttons to avoid seeing a single button press as multiple presses over a few milliseconds.

Please note that the Arduino 3pi libraries are out of date compared to the Pololu AVR library and were written while Arduino IDE 0012 was the latest release, so there could be compatibility issues with the current version of the Arduino IDE. Do the libraries work better if you use an older version of the Arduino IDE (0011 or 0012)? Is there a reason why you’re using the Arduino IDE rather than AVR Studio with the Pololu AVR library? I think you’ll get much better results using the current Pololu AVR library in AVR Studio than you will using the older Arduino 3pi libraries with the Arduino IDE, and unless you’re on a mac or want to incorporate another Arduino library into your program, there really isn’t anything to be gained from programming the 3pi in the Arduino environment now that the Pololu AVR library has added support for things like sending serial data and using the digital I/O lines.

- Ben