Snagged a Parallax Joystick from Radioshack and a 7-segment LED display. Wrote a little piece of code to denote if the stick was centered, or if it was in one of the four quadrants. See the video for use - just wanted to upload this to the community.
/*
Joystick Directional Indicator
Using a:
Parallax 2 Axis Joystick
Radio Shack 7-Segment LED Digital Display
*/
int UD = 0; //read from U/D axis of Joystick
int LR = 0; //read from L/R axis of Joystick
void setup() {
// initialize the digital pins as outputs.
pinMode(13, OUTPUT); // pin 1 of LED display
pinMode(12, OUTPUT); // pin 2 of LED display
pinMode(8, OUTPUT); // pin 6 of LED display
pinMode(7, OUTPUT); // pin 9 of LED display, the dot
Serial.begin(9600);
}
void loop() {
UD = analogRead(A0); //read u/d from stick
LR = analogRead(A1); //read l/r from stick
Serial.print("UD = "); // reference displays
Serial.print(UD, DEC); // reference displays
Serial.print(", LR = "); // reference displays
Serial.println(LR, DEC); // reference displays
if(LR <= 509 && UD <= 509) // send ldown to display if stick is in lower left quadrant
{
ldown();
}
if(LR <= 509 && UD >= 514) // send ldown to display if stick is in upper left quadrant
{
lup();
}
if(LR >= 514 && UD <= 509) // send ldown to display if stick is in lower right quadrant
{
rdown();
}
if(LR >= 514 && UD >= 514) // send ldown to display if stick is in upper right quadrant
{
rup();
}
if(LR >= 509 && LR <= 514 && UD >= 509 && UD <= 514) // send blinking dot to display if stick is basically centered
{
cent();
}
}
// 7-SEGMENT DISPLAY FUNCTIONS
void lup()
{
digitalWrite(13, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
delay(100);
digitalWrite(13, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
delay(100);
}
void ldown()
{
digitalWrite(13, HIGH);
digitalWrite(8, HIGH);
delay(100);
digitalWrite(13, LOW);
digitalWrite(8, LOW);
delay(100);
}
void rup()
{
digitalWrite(12, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
delay(100);
digitalWrite(12, LOW);
digitalWrite(8, LOW);
digitalWrite(7, LOW);
delay(100);
}
void rdown()
{
digitalWrite(12, HIGH);
digitalWrite(8, HIGH);
delay(100);
digitalWrite(12, LOW);
digitalWrite(8, LOW);
delay(100);
}
void cent()
{
digitalWrite(7, HIGH);
delay(100);
digitalWrite(7, LOW);
delay(100);
}