# Obstacle Avoidance Game With Distance Sensor

282

## Introduction: Obstacle Avoidance Game With Distance Sensor

Obstacle avoidance game like Flappy Bird. Move your hand to avoid the collision. It is easy to make and fun to play!

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: Things Used in This Project

obniz

IR distance sensor

Smartphone or computer

## Step 2: Setting Up Obniz

To set up obniz, all you need to do is to follow three steps.

1. Connect obniz to wifi.
2. Connect devices like LED or motors to obniz .
3. Scan QR code of obniz and start programming. You do not need to install any software.

## Step 3:

Connect obniz and the distance sensor as below.

• io0 : Signal（yellow）
• io1 : GND（black）
• io2 : VCC（red）

## Step 4:

We use HTML5 canvas.

`<canvas id="field" width="300" height="300"/>`
`let canvas = document.getElementById('field'); let ctx = canvas.getContext('2d');`

Set value of distance sensor to var "inputHeight", and use it anytime.

```let inputHeight = 0; let obniz = new Obniz("OBNIZ_ID_HERE");
obniz.onconnect = async function () {
let sensor = obniz.wired("GP2Y0A21YK0F", {vcc: 2, gnd: 1, signal: 0});
sensor.start(function (height) {
inputHeight = height;
})
};```

{vcc: 2, gnd: 1, signal: 0} should be changed if you connect devices different from Step 2.
This value is used as each frame input.

```let input = (300 - inputHeight);  input = Math.min(Math.max(0, input), canvas.height);
dot.push(input);```

## Step 5: Program

Please get the program from here

Participated in the
Game Life Contest

## Recommendations

6 1.1K
150 7.3K
2 360 30K
Large Motors Class

14,340 Enrolled