격리 기간 동안 심심해서 크롬 공룡 게임을 자동으로 플레이하는 장치를 만들었습니다. 아두이노와 LDR(조도 센서)로 화면의 장애물을 감지하고, 파이썬 스크립트로 스페이스바를 자동 입력해 점프시킵니다.
작동 원리
아두이노 + LDR: 모니터 화면의 특정 위치에 LDR을 고정해 장애물(선인장, 새)이 지나갈 때 화면 밝기 변화를 감지합니다.
임계값 설정: 아날로그 값 읽기 코드를 업로드하고 시리얼 모니터로 장애물 있을 때/없을 때 값을 확인해 임계값을 정합니다.
파이썬 연동: 아두이노가 장애물 감지 시 시리얼로 '1' 전송 → 파이썬이 읽어서 pyautogui.press('up')로 점프 키 입력.
아두이노 배선
LDR을 A0 핀에 연결하고 풀다운 저항(10kΩ 권장)을 추가하세요.
코드
아두이노 코드
// Arduino Dino Game 자동화
int sensorPin = A0;
int sensorValue = 0;
int thresholdValue = 43; // 시험 후 조정
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(sensorPin);
// Serial.println(sensorValue); // 임계값 찾을 때 주석 해제
if(sensorValue <= thresholdValue) {
Serial.println(1); // 장애물 감지 시 1 전송
}
delay(50);
}
파이썬 코드
import subprocess
import time
import pyautogui
import serial
# 브라우저 실행 (크롬 경로로 수정 가능)
subprocess.call([r'C:\Program Files\Mozilla Firefox\firefox.exe', '-new-tab', 'https://chromedino.com/'])
time.sleep(6)
print("All sett :)")
ser = serial.Serial('COM4') # 아두이노 포트로 변경
ser.baudrate = 9600
while True:
h1 = ser.readline()
if h1:
ss = int(h1.decode('utf-8'))
if ss == 1:
print("Oh :< Jump!!")
pyautogui.press('up')