Using a Touch Sensor to Play Chrome's Dino game

人机交互 Maggie ⋅ 于 2020-05-03 16:39:32 ⋅ 216 阅读


This project was inspired by Automated Dino Game Using LDR on Arduino Project Hub. It's like a cheating program that automatically plays the game for you.

In this project, you can control the jumping of the little dinosaur in the game with your touch on the sensor (you still have to play the game yourself).

Hardware Preparation

:ballot_box_with_check:Arduino :ballot_box_with_check:Base Shield :ballot_box_with_check:Grove-Touch_Sensor

  1. Connect Grove-Touch_Sensor to port D2 of Grove-Base Shield.
  2. Plug Grove - Base Shield into Arduino.
  3. Connect Arduino to PC via a USB cable


Codes on Arduino

// Arduino Dino Game ! :)

const int TouchPin = 2;    // set the port where you connected your touch sensor
int sensorValue = 0;  // variable to store the value coming from your touch sensor
int thresholdValue = 45; //this one depend on your trial and error method. 

void setup() {
void loop() {
  // read the value from the sensor:
   int sensorValue = digitalRead(TouchPin);
   Serial.println(sensorValue);  //uncomment this while first use and take your thresholdValue and set. 

      delay(40);     //delay 50ms

Codes on Python

import subprocess   
import time
import pyautogui
import serial            #See Error Prone Codes[1] below if you could not import this module
from serial import SerialException
#uncomment it if you don't have chrome in your computer. If you have chrome, open the web page #'chrome://dino' and directly run the code[r'/Applications/',  '_blank', ''])  

time.sleep(6)                   #give a short time to open and setup all.
print("All sett :)")

#Update with your arduino [port]
ser = serial.Serial('/dev/cu.usbmodem14201')        
#set Serial Baud Rate at the baud rate of your sensor (in my case it's 9600). 
ser.baudrate = '9600'           
# Baud rate roughly means the speed that data is transmitted, 
# and it is a derived value based on the number of symbols transmitted per second. 

while True:       # looping. 
        h1=ser.readline()        #reading serial data.
        #decode and make it an int value. See Error prone codes[2] below
        ss = ord(h1.decode().strip()) 
        #print(ss)  uncomment this line if you want to check the value of ss

        if ss == 49:   # true while obstacle.
            print("Oh :< Jump!! ")
            #print(ss)  uncomment this line if you want to check the value of ss
  'space')         #Auto press [space] key

    except serialException:
        print(error)       # Maybe don't do this, or mess around with the interval

Error Prone Codes

1.If you don't have the module "serial ", type this in your terminal instead of "pip install serial"
pip install pyserial
2.If we directly print(h1) without any decoding, the output will be like below
b'0\r\n'  #this is when the touch sensor doesn't sense your fingers
b'1\r\n'  #this is when the touch sensor senses your fingers

# The return value is the corresponding decimal integer

Python prints a TXT file with b' \r\n', so if we only want the return value of 0 or 1, we need to decode it with .decode().strip() after the variable

 #add .decode().strip() after the variable you want to decode, and then t ransform the datatype from str to int
ss = ord(h1.decode().strip()) 
48   #this is when the touch sensor doesn't sense your fingers
49   #this is when the touch sensor senses your fingers

The return value is no longer 0 or 1, but 48 and 49, this is because we used the method ord(), which returns an integer representing Unicode code point for the given Unicode character.

the corresponding Unicode character for 0 is 48 and the corresponding Unicode character for 1 is 49 (check ASCII table for further information)


本帖由 YX 于 2月前 加精
回复数量: 0
    • 请注意单词拼写,以及中英文排版,参考此页
    • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
    • 支持表情,使用方法请见 Emoji 自动补全来咯,可用的 Emoji 请见 :metal: :point_right: Emoji 列表 :star: :sparkles:
    • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif
    • 发布框支持本地存储功能,会在内容变更时保存,「提交」按钮点击时清空