Membuat Permainan Google Chrome Dino Otomatis dengan Arduino
Komponen dan perlengkapan
Arduino UNO
×
1
LDR, 5 Mohm
×
1
Tentang proyek ini
Bosan banget, selama masa karantina ini? 🥴
Ayo mainkan permainan Google Chrome Dino… ;)
Tapi, apa yang harus dilakukan jika permainan Dino juga mulai membosankan.
Ayo otomatisasi permainan Dino😉.
Jadi, kita butuh Arduino dan LDR. Pengetahuan sedikit tentang python juga diperlukan.
Ayo siapkan Arduino kita terlebih dahulu.
Hubungkan Arduino UNO dengan LDR. (Jangan lupa tambahkan Resistor)
Selanjutnya, unggah kode untuk membaca nilai LDR secara analog.
// Arduino Dino Game ! :)
int sensorPin = A0; //Connect LDR on A0 int sensorValue = 0; // variable to store the value coming from LDR int thresholdValue = 45; //this one depend on your trial and error method. void setup() { Serial.begin(9600); //Serial begin on 9600 }
void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); //read sensor data //Serial.println(sensorValue); //uncomment this while first use and take your thresholdValue and set.
if(sensorValue <= thresholdValue){ //if condition to detect obstacle. Serial.println(1); //Serial printing if condition is true. } delay(40); //delay 50ms }
Pasang LDR pada monitor lalu baca analogValues ketika hambatan ada. Tetapkan nilai tersebut sebagai thresholdValue dan perbarui kondisi if Anda. Yesssss !!!!! ✌️ Bagian Arduino selesai.
Anda dapat menambahkan servo motor untuk menekan ‘Space bar’ agar Dino melompat. Tapi ini masa karantina. Jadi, saya tidak punaya cara untuk mendapatkan servo motor. Jadi, di sini saya menggunakan kode Python untuk melakukan tugas ini.
Jadi, ayo coding dengan Python ;)
Salin dan tempel kode Python, buat perubahan yang sesuai.
Dalam kode kita menggunakan modul pyserial untuk membaca data serial. Kemudian untuk menekan tombol atas menggunakan modul PyAutoGUI.
import subprocess import time import pyautogui import serial subprocess.call([r'C:\Program Files\Mozilla Firefox\Firefox.exe', '-new-tab', 'https://chromedino.com/']) #Add google chrome path for chrome users. #to open browser and open Dino game. #I'm a firefox user so, i don't have Dino game, #Here i'm using chromedino.com to play game. time.sleep(6) #give a short time to open and setup all. print("All sett :)")
ser = serial.Serial('COM4') #Update with your arduino [port] ser.baudrate = '9600' #set baudRate
while True: # looping. h1=ser.readline() #reading serial data. if h1: ss = int(h1.decode('utf-8')) # decode and make a int value if ss== 1: # true while obstacle. print("Oh :< Jump!! ") pyautogui.press('up') #Auto press [UP] key
Jalankan kode Python Anda dan pasang LDR di tempat yang sesuai dengan metode coba dan kesalahan. Ya !!! Permainan Dino otomatis siap.💃 🕺
Anda dapat mengembangkan ini ke level berikutnya dengan menggunakan LDR lain untuk mendeteksi burung juga.
GitHub Repo : https://github.com/rafitc/arduDino
YouTube Video
Kode
Cuplikan kode #1
Cuplikan kode #2
Cuplikan kode #1Arduino
// Arduino Dino Game ! :) int sensorPin = A0; //Connect LDR on A0 int sensorValue = 0; // variable to store the value coming from LDR int thresholdValue = 43; //this one depend on your trial and error method. void setup() { Serial.begin(9600); //Serial begin on 9600 } void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); //read sensor data //Serial.println(sensorValue); //uncomment this while first use and take your thresholdValue and set. if(sensorValue <= thresholdValue){ //if condition to detect obstacle. Serial.println(1); //Serial printing if condition is true. } delay(50); //delay 50ms }
Cuplikan kode #2Python
import subprocess import time import pyautogui import serial subprocess.call([r'C:\Program Files\Mozilla Firefox\Firefox.exe', #Add google chrome path for chrome users. '-new-tab', 'https://chromedino.com/']) #to open browser and open Dino game. #I'm a firefox user so, i don't have Dino game, #Here i'm using chromedino.com to play game. time.sleep(6) #give a short time to open and setup all. print("All sett :)") ser = serial.Serial('COM4') #Update with your arduino [port] ser.baudrate = '9600' #set baudRate while True: # looping. h1=ser.readline() #reading serial data. if h1: ss = int(h1.decode('utf-8')) # decode and make a int value if ss== 1: # true while obstacle. print("Oh :< Jump!! ") pyautogui.press('up') #Auto press [UP] key
Github
GitHUb repo link https://github.com/rafitc/arduDino