Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
sraykov08
Published

Joystick Shield

How to Use the Arduino Joystick Shield v2.4

IntermediateProtip7,121
Joystick Shield

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
USB-A to B Cable
USB-A to B Cable
×1
Joystick Shield
https://www.aliexpress.com/item/32902881411.html?spm=a2g0o.productlist.0.0.83b07f77DZWd21&algo_pvid=6db54e3c-3016-41fb-9d9a-c6156e97c3e7&algo_expid=6db54e3c-3016-41fb-9d9a-c6156e97c3e7-1&btsid=0ab6f8ad15895223736268320e4592&ws_ab_test=searchweb0_0,searchweb201602_,searchweb201603_
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

joystick

Arduino
#define BUTTON_UP 2

#define BUTTON_RIGHT 3

#define BUTTON_DOWN 4

#define BUTTON_LEFT 5

#define BUTTON_E 6

#define BUTTON_F 7

#define BUTTON_K 8

#define PIN_ANALOG_X 0

#define PIN_ANALOG_Y 1






#define DELAY 500



void setup() {

 Serial.begin(9600);



 // to enable pull up resistors first write pin mode

 // and then make that pin HIGH

 pinMode(BUTTON_UP, INPUT);

 digitalWrite(BUTTON_UP, HIGH);



 pinMode(BUTTON_RIGHT, INPUT);

 digitalWrite(BUTTON_RIGHT, HIGH);



 pinMode(BUTTON_DOWN, INPUT);

 digitalWrite(BUTTON_DOWN, HIGH);



 pinMode(BUTTON_LEFT, INPUT);

 digitalWrite(BUTTON_LEFT, HIGH);



 pinMode(BUTTON_E, INPUT);

 digitalWrite(BUTTON_E, HIGH);



 pinMode(BUTTON_F, INPUT);

 digitalWrite(BUTTON_F, HIGH);


  pinMode(BUTTON_K, INPUT);

 digitalWrite(BUTTON_K, HIGH);

}



void loop() {

 if(digitalRead(BUTTON_UP) == LOW) {

    Serial.println("Button pro is pressed");
   
   delay(DELAY);

 }

 else if(digitalRead(BUTTON_RIGHT) == LOW) {

   Serial.println("Button 0 is pressed");

   delay(DELAY);

 }

 else if(digitalRead(BUTTON_DOWN) == LOW) {

   Serial.println("Button X is pressed");

   delay(DELAY);

 }

 else if(digitalRead(BUTTON_LEFT) == LOW) {

   Serial.println("Button");
   Serial.println((char)254);
   Serial.println(" is pressed");

   delay(DELAY);

 }

 else if(digitalRead(BUTTON_E) == LOW) {

   Serial.println("Button E is pressed");

   delay(DELAY);

 }

 else if(digitalRead(BUTTON_F) == LOW) {

   Serial.println("Button F is pressed");

    Serial.print("x: ");

 Serial.println(analogRead(PIN_ANALOG_X));

 // Print y axis values

 Serial.print("y: ");

 Serial.println(analogRead(PIN_ANALOG_Y));



 // Some delay to clearly observe your values on serial monitor.

 delay(500);


   delay(DELAY);

 }

 if(digitalRead(BUTTON_K) == LOW) {

    Serial.println("Button k  is pressed");


   delay(DELAY);

 }
}

Credits

sraykov08
7 projects • 1 follower

Comments