loading

in this instructable we will be creating a pressure button as input for games, which is similar to a regular digital button but instead of writing 1 or 0 we will be writing the variation in pressure as we press or release and the flappy bird will jump accordingly, the application for such an input methods are many and this is one of them, i hope you enjoy :)

Step 1: Get the Tools

1- a barometer sensor, I used model gy-65

2-4 male to female wires

3- a balloon

4- a cup to insert barometer inside

5-(optional) a covering of some sort to provide protection for barometer, I used a kinder surprise egg

6- tape;

Step 2: Lets Assemble Our Pressure Button

1- connect the wires to the barometer, you need to connect the ground, vcc, SDA and SLC.

2-attach the four wires with a tape

3-make holes in your egg, one for the wires to pass through the others to make the barometer under the pressure of the containing environment(the Cup) and not the egg itself.

4-insert the barometer inside the egg

5-connect the wires to your arduino, make sure to connect SDA to pin A4 and SLC to pin A5, as we will be using a predefined external library.

6-put your egg or barometer inside the cup and cover it with a ballon :)

Step 3: Lets Download and Import Adafruit-BMP085-Library

1- click on the link below and download the library

Adafruit library

2- import the library into your arduino IDE simply by clicking

a-Sketch-> add File

b-add both adafruit_BMP085.cpp and adafruit_BMP085.h files.

3-dont forget to include your library from Sketch->include Library


Step 4: Lets Code Our Arduino

for a more clear instruction please watch the video, i will be including the code at the end of this step.

what we basically want to do is :

1- include my library #include "Adafruit_BMP085.h"

2- make an instance of the barometer , I called it bmp Adafruit_BMP085 bmp;

3-read and store the pressure simply by long data = bmp.readPressure();

the issue I had with the readings is that the numbers were too big and didn't make much sense, luckily

much of that number is noise and doesn't add to our reading...for example my initial reading was

9xxxxx but I noticed that the 9 never changes as I apply or release pressure so I simply got rid of it by subtracting 90000, the next thing I got rid of the last two digit to the right as they were "jumpy" and inconsistent by dividing over 100 and i ended up with a nice and clear 2 digit number where 10 was my resting value(no pressure applied) but i wanted to make it into a zero so instead of substacting 90000 at the beginning i subtracted 91000 and I was ready to send my data

the code:

#include "Adafruit_BMP085.h"
Adafruit_BMP085 bmp;
void setup() {
  Serial.begin(9600);
  bmp.begin();
}
void loop() {
  long data = bmp.readPressure();
  data -= 91000;
  data /= 100;
  if (data >= 0) {
    Serial.write(data);
    delay(30);
  }
  delay(20);
}

Step 5: Lets Build Flappy Bird in Our Unity :)

to make flappy bird I followed a tutorial on youtube which is very clear and begginer friendly

flappy bird unity tutorial

but for this tutorial all you need is to have a bird sprite and simply attach the following script to it

using UnityEngine;
using System.Collections; using System.IO.Ports; public class Bird : MonoBehaviour { public AudioSource player; public Vector3 gravity; public Vector3 jumpVelocity; public int rotationspeed;

private bool didFlap; private Vector3 velocity; private SerialPort port; // Use this for initialization void Start () { port = new SerialPort(@"\\.\" + "COM11", 9600); port.Open(); port.ReadTimeout = 25; didFlap = false; velocity = Vector3.zero; }

void Update() { if (port.IsOpen) {

try {

float value = port.ReadByte(); if (value > 0) { value = Mathf.Clamp(value, 0,2); didFlap = true; jumpVelocity.y = value; } } catch (System.Exception) {

}

} if (Input.GetKeyDown(KeyCode.Space)) { didFlap = true; }

} // Update is called once per frame void FixedUpdate () { velocity -= gravity * Time.deltaTime; if (didFlap) { didFlap = false; if (!player.isPlaying) {

player.Play(); } if (velocity.y <0) { velocity = Vector3.zero; } velocity += jumpVelocity; }

transform.position += velocity*Time.deltaTime; float angle = 0; if (velocity.y < 0) {

angle=Mathf.Lerp(0, -90, -velocity.y/3); } transform.rotation = Quaternion.Euler(0f, 0f, angle); } }

About This Instructable

243views

3favorites

License:

Bio: Hi eveyone, my name is Raed, I'm a computer science student from Jordan, I'm passionate about learning and developing mobile games in Unity3D ... More »
More by RaedT:A Morse Code Translator( Light -> Text & Text ->Light)   How to turn your 4X4 keypad into 4X4XN  Make a Punching Bag in Arduino and Unity Using a Joystic and Balloon  
Add instructable to: