Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Joystick.h>
- Joystick joy; // Objek Joystick untuk membaca input dari Joypad
- // -------------------------------------------
- // Kode sederhana Game Interaktif dengan Arduino dan Joypad
- // www.ardukode.blogspot.com
- // -------------------------------------------
- int x = 0; // Nilai sumbu X Joypad
- int y = 0; // Nilai sumbu Y Joypad
- int button1 = 0; // Status tombol 1 Joypad
- int button2 = 0; // Status tombol 2 Joypad
- int button3 = 0; // Status tombol 3 Joypad
- int button4 = 0; // Status tombol 4 Joypad
- void setup() {
- joy.begin(); // Inisialisasi library Joystick
- Serial.begin(9600); // Inisialisasi komunikasi serial
- }
- void loop() {
- joy.readJoystick(); // Baca input dari Joypad
- x = joy.getAxis(0); // Dapatkan nilai sumbu X
- y = joy.getAxis(1); // Dapatkan nilai sumbu Y
- button1 = joy.getButton(1); // Dapatkan status tombol 1
- button2 = joy.getButton(2); // Dapatkan status tombol 2
- button3 = joy.getButton(3); // Dapatkan status tombol 3
- button4 = joy.getButton(4); // Dapatkan status tombol 4
- // Proses input dari Joypad untuk mengontrol game
- if (x > 0) {
- // Pemain bergerak ke kanan
- } else if (x < 0) {
- // Pemain bergerak ke kiri
- }
- if (y > 0) {
- // Pemain bergerak ke atas
- } else if (y < 0) {
- // Pemain bergerak ke bawah
- }
- if (button1) {
- // Tombol 1 ditekan
- }
- if (button2) {
- // Tombol 2 ditekan
- }
- if (button3) {
- // Tombol 3 ditekan
- }
- if (button4) {
- // Tombol 4 ditekan
- }
- // Tampilkan informasi game di layar LCD (opsional)
- if (lcd) {
- lcd.clear();
- lcd.print("X: ");
- lcd.print(x);
- lcd.print(", Y: ");
- lcd.print(y);
- lcd.println();
- lcd.print("Button 1: ");
- lcd.print(button1);
- lcd.print(", Button 2: ");
- lcd.print(button2);
- lcd.println();
- lcd.print("Button 3: ");
- lcd.print(button3);
- lcd.print(", Button 4: ");
- lcd.println(button4);
- }
- // Putar suara game di speaker (opsional)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement