×
Arduino Joystick Shield Progamming Script

Cara menggunakan Arduino Shield Joystick

Pada tutorial kali ini KlinikRobot akan mengulas cara merakit Arduino Shield Joystick (ASJ) dengan cara melakukan solder. Produk yang sedang diulas dapat dilihat pada halaman berikut ini :

Adapun peralatan kerja yang diperlukan adalah alat solder, timah solder, tang jepit/ tang kombinasi untuk memegang pcb.

Berikut langkah-langkah melakukan perakitan ASJ :

Cara merakit Arduino Shield Joystick dengan cara Menyolder

Setelah kit berhasil kita rakit, langkah selanjutnya adalah melakukan test program bersama Arduino. Adapun contoh pemrograman ASJ ini yang kami himpun sebagai berikut :

Cara penggunaan bersama Arduino dapat dilihat pada script Arduino berikut ini :
1. Pembacaan Joystick arah X, arah Y, push Z switch, Up, Down, Left, Right

const int PIN_BUTTON_SELECT = 2; // Select button is triggered when joystick is pressed
const int PIN_BUTTON_RIGHT = 3;
const int PIN_BUTTON_UP = 4;
const int PIN_BUTTON_DOWN = 5;
const int PIN_BUTTON_LEFT = 6;
const int PIN_ANALOG_X = 0;
const int PIN_ANALOG_Y = 1;
void setup() {
 Serial.begin(9600);
 // Specify each pin connected to a pushbutton as an input.
 // Also enable the Arduino's internal "pull-up" resistors
 // for each pushbutton we want to read--this means the shield
 // doesn't need to have resistors on it.
 // Note that when a pull-up resistor is used on a pin the
 // meaning of the values read are reversed compared to their
 // usual meanings:
 //    * HIGH = the button is not pressed
 //    * LOW = the button is pressed
 /* old method to turn on the pull-up resistor for the SEL line
     before Arduino IDE v1.01 (see http://arduino.cc/en/Tutorial/DigitalPins)
    /*pinMode(PIN_BUTTON_RIGHT, INPUT);
    digitalWrite(PIN_BUTTON_RIGHT, HIGH);

    pinMode(PIN_BUTTON_LEFT, INPUT);
    digitalWrite(PIN_BUTTON_LEFT, HIGH);

    pinMode(PIN_BUTTON_UP, INPUT);
    digitalWrite(PIN_BUTTON_UP, HIGH);

    pinMode(PIN_BUTTON_DOWN, INPUT);
    digitalWrite(PIN_BUTTON_DOWN, HIGH);

    pinMode(PIN_BUTTON_SELECT, INPUT);
    digitalWrite(PIN_BUTTON_SELECT, HIGH);*/
 pinMode(PIN_BUTTON_RIGHT, INPUT_PULLUP);
 pinMode(PIN_BUTTON_LEFT, INPUT_PULLUP);
 pinMode(PIN_BUTTON_UP, INPUT_PULLUP);
 pinMode(PIN_BUTTON_DOWN, INPUT_PULLUP);
 pinMode(PIN_BUTTON_SELECT, INPUT_PULLUP);
}
void loop() {
 // Print the current values of the inputs (joystick and
 // buttons) to the console.
 Serial.print("l:");
 Serial.print(digitalRead(PIN_BUTTON_LEFT));
 Serial.print(" ");
 Serial.print("r:");
 Serial.print(digitalRead(PIN_BUTTON_RIGHT));
 Serial.print(" ");
 Serial.print("u:");
 Serial.print(digitalRead(PIN_BUTTON_UP));
 Serial.print(" ");
 Serial.print("d:");
 Serial.print(digitalRead(PIN_BUTTON_DOWN));
 Serial.print(" ");
 Serial.print("x:");
 Serial.print(analogRead(PIN_ANALOG_X));
 Serial.print(" ");
 Serial.print("y:");
 Serial.print(analogRead(PIN_ANALOG_Y));
 Serial.print(" ");
 Serial.print("s:");
 Serial.print(digitalRead(PIN_BUTTON_SELECT));
 Serial.print(" ");
 Serial.println();
}

2. Pembacaan Joystick arah X, arah Y, push Z switch, Up, Down, Left, Right (langsung)

char button0=3, button1=4, button2=5, button3=6;
char sel=2;
void setup(void)
{
 pinMode(sel, INPUT);      //Set the Joystick 'Select'button as an input
 digitalWrite(sel, HIGH);  //Enable the pull-up resistor on the select button
 
 pinMode(button0, INPUT);      //Set the Joystick button 0 as an input
 digitalWrite(button0, HIGH);  //Enable the pull-up resistor on button 0
 pinMode(button1, INPUT);      //Set the Joystick button 1 as an input
 digitalWrite(button1, HIGH);  //Enable the pull-up resistor on button 1
 
 pinMode(button2, INPUT);      //Set the Joystick button 2 as an input
 digitalWrite(button2, HIGH);  //Enable the pull-up resistor on button 2
 pinMode(button3, INPUT);      //Set the Joystick button 3 as an input
 digitalWrite(button3, HIGH);  //Enable the pull-up resistor on button 3
 
 Serial.begin(9600);           //Turn on the Serial Port at 9600 bps
}
void loop(void)
{
 Serial.print(analogRead(0));          //Read the position of the joysticks X axis and print it on the serial port.
 Serial.print(",");
 Serial.print(analogRead(1));          //Read the position of the joysticks Y axis and print it on the serial port.
 Serial.print(",");
 Serial.print(digitalRead(sel));       //Read the value of the select button and print it on the serial port.
 Serial.print(digitalRead(button0));   //Read the value of the button 0 and print it on the serial port.
 Serial.print(digitalRead(button1));   //Read the value of the button 1 and print it on the serial port.
 Serial.print(digitalRead(button2));   //Read the value of the button 2 and print it on the serial port.
 Serial.println(digitalRead(button3)); //Read the value of the button 3 and print it on the serial port.
 
 //Wait for 100 ms, then go back to the beginning of 'loop' and repeat.
 delay(100);
}

3. Penggunaan Joystick bersamaan dengan pengaturan melodi suara sederhana

const int PIN_ANALOG_X = 0;
const int PIN_ANALOG_Y = 1;
const int speakerPin = 9;
int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;
void playTone(int tone, int duration) {
 for (long i = 0; i < duration * 1000L; i += tone * 2) {
   digitalWrite(speakerPin, HIGH);
   delayMicroseconds(tone);
   digitalWrite(speakerPin, LOW);
   delayMicroseconds(tone);
 }
}
void playNote(char note, int duration) {
 char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
 int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
 // play the tone corresponding to the note name
 for (int i = 0; i < 8; i++) {
   if (names[i] == note) {
     // We modify the pitch of the note to be played based on the vertical position
     // of the joystick:
     playTone(tones[i] + map(analogRead(PIN_ANALOG_Y), 0, 1023, 959, -959), duration);
   }
 }
}
void setup() {
 pinMode(speakerPin, OUTPUT);
}
void loop() {
 for (int i = 0; i < length; i++) {
   // We set the tempo based on the horizontal position of the joystick:
   tempo = map(analogRead(PIN_ANALOG_X), 0, 1023, 500, 100);
   if (notes[i] == ' ') {
     delay(beats[i] * tempo); // rest
   } else {
     playNote(notes[i], beats[i] * tempo);
   }
   // pause between notes
   delay(tempo / 2);
 }
}

Berikut ini video demonstari pemrogaman nya dengan Arduino :