Arduino joystick connection

In this session I take a basic joystick and hook it to my arduino. This is simple but fun and I feel I could come up with some neat projects as I go along.

Parts Needed

OR


const int VERT = 0; // analog
const int HORIZ = 1; // analog
const int SEL = 2; // digital


void setup()
{
 
  pinMode(SEL,INPUT); // this is for the button when you push down on the joystick
  digitalWrite(SEL,HIGH); // By doing this you are just letting code know you are not pressed

  // set up serial port for output
  Serial.begin(9600);
}

void loop() 
{
  int vertical, horizontal, select;
  
  // read all values from the joystick
  vertical = analogRead(VERT); // will be 0-1023
  horizontal = analogRead(HORIZ); // will be 0-1023
  select = digitalRead(SEL); // will be HIGH (1) if not pressed, and LOW (0) if pressed
  
  // print out the values
  if (vertical > 540)
  {
      Serial.println("moving down: ");//print words
      Serial.print(vertical,DEC);//print value as decimal
  }
 if (vertical < 525)
  {
      Serial.println("moving up: ");//print words
      Serial.print(vertical,DEC);//print value as decimal
  }
if (horizontal < 515)
{
    Serial.println(" moving right: ");//print words
    Serial.print(horizontal,DEC);//print value as decimal
}
if (horizontal > 525)
{
    Serial.println(" moving left: ");//print words
    Serial.print(horizontal,DEC);//print value as decimal
}
  if(select == LOW)
  {
    Serial.println("PRESSED!");
  }
}  

Leave a Reply

Your email address will not be published. Required fields are marked *