Thursday, August 25, 2011

Measuring Feedback: Analog Joystick and LCD

If you remember from my last update, I accidentally fried the ATmega USB chip on my Arduino. Since this chip controls the COM port communications, I can no longer use the serial monitor for easy feedback. From now on it looks like I'm going to be stuck using an LCD (Liquid Crystalline Display) which I've been meaning to set up for some time now. Thankfully it's just as easy to control due to all of the built in program libraries.

Your Arduino isn't going to always be attached to a computer so its a good idea to have some other form of visual feedback besides the serial monitor. This is where your LCD comes in handy. It makes your project look cooler and allows you to debug and get some feedback from your program while disconnected from your computer.

I really should look into getting a back lit LCD, cooler looking AND easier to read
Even though a robot should be fully autonomous, its always helpful to have some form of control during the building and testing stages. During my last Sparkfun splurge, I picked up a thumb joystick and it's breakout board. http://www.sparkfun.com/products/9032

Thumb joystick plus breakout board
If you've ever played a vidya game post year 2000, you've probably come into contact with one of these before. This joystick is two-dimensional and has a breakout pin for each of the horizontal and vertical (X and Y) axis. Being an analog joystick, movements are measured using two potentiometers, one for up/down the other for left/right. Each directional axis is mapped to a 10K potentiometer so we're going to need to use the analogRead function to get our values.

 Wow that microphone is sensitive... my nose is stuffy, big whoop wanna fight about it?

Here's the code I used from the video:

//  Noah Stahl
//  http://arduinomega.blogspot.com
//  Arduino Mega 2560
//This sketch continuously measures the vertical and horizontal
//positions of a thumb joystick and displays the results on an LCD.
//This code assumes that you are using a 20x2 LCD with 4-bit data
// LCD Vss(1) to GND
// LCD Vdd(2) to +5V
// LCD Vo (3) to +5V (contrast control)
// LCD RS (4) to digital pin 53
// LCD R/W(5) to GND
// LCD EN (6) to digital pin 52
// LCD D4 (7) to digital pin 50
// LCD D5 (8) to digital pin 49
// LCD D6 (9) to digital pin 48
// LCD D7 (10)to digital pin 47


#include <LiquidCrystal.h>
// Specify the numbers of the LCD pins on the Arduino
LiquidCrystal lcd(53, 52, 50, 49, 48, 47); // RS, EN, D4, D5, D6, D7
 
void setup()
{
  lcd.begin(20, 2);         // Number of columns and rows on the LCD
  lcd.clear();
  lcd.println("ArduinoMega Blagspot");
  lcd.setCursor(0,1);
  lcd.println("Thumb Joystick Test ");
  delay(5000);
  lcd.clear();
}
 
void loop()
{
  lcd.setCursor(0,0);
  lcd.print("Horizontal: ");
  lcd.print(analogRead(1));  // HORZ pin connected to A1
  lcd.setCursor(0,1);
  lcd.print("  Vertical: ");
  lcd.print(analogRead(0));  // VERT pin connected to A0
  delay(100);                // Prevents LCD from updating too quickly
  lcd.clear();
}

You might have noticed that the horizontal centers at 525 instead of 512. Since it is consistent you could easily adapt your code to accommodate this.

The analog values will also max/minimize out before the joystick actually reaches its full range of motion. These dead spots are close enough to the edges that it shouldn't be a problem for your project. There is also an extra pin on the breakout board for the built in tactile switch which I didn't utilize in this demo. For less than $4, can you really complain? (Don't answer that...) Once I get around to setting up my Xbees with the Arduino, this would be perfect for a remote control. For another update though!

2 comments:

  1. yo man im sitting here doing the same thing right now haha....im trying to figure out if there is any real simple games i could code...got any ideas?

    ReplyDelete
  2. btw im using a serial lcd...so much simpler bro

    ReplyDelete