Robojax

Arduino code and video Dual Axis Joystick

دروس آردوینو به فارسی

This is the Arduino code Dual Axis Joystick

This video is Introduction to Dual Axis Joystick for Arduino with example.

 /*
 * this is Arduino code to use Dual axis XY Joystick to read value of joystick position as X and Y and
 * also read the switch.
 * Other Arduino libarary and videos http://robojax.com/learn/arduino/
 
 * Watch the video for this code to learn it fully.
  * Watch the video here: https://youtu.be/6N8Iq353GM8
 * this code is offered "as is" without any warranty.
 * if you are sharing this code, you must keep this copyright note.
 */
 /*

 * Written for Robojax video on Jan 10, 2018
 * by Ahmad Nejrabi, in Ajax, Ontario, Canada
 * Copyright Robojax Inc.
 */
 
/*
 * This is a dual axis joystick skitch for Arduino.
 * The position of joystick is read and displayed on serial monitor
 */
#define sw 6
#define screenWidth 600
#define screenHeight 300
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(sw,INPUT_PULLUP);// setting pin sw as input
  pinMode(13,OUTPUT);
}
 
void loop() {
  // Robojax Dual Axis joystick project
  int x = analogRead(A0);// read analog
  int y = analogRead(A1);
  int xPos = map(x, 0, 1023,0,screenWidth);
  int yPos = map(y, 0, 1023,0,screenHeight); 
  int sStat = digitalRead(sw);//
  // Robojax project

  Serial.print("X: ");
  Serial.print(xPos);

  Serial.print(" Y: ");
  Serial.println(yPos);// Robojax prints y
  if(sStat ==LOW){
    Serial.println("Switch pressed");
    digitalWrite(13,HIGH);// Turn LED ON
  }else{
    digitalWrite(13,LOW);// Turn LED OFF
  }
  delay(500);
}