Last active
August 29, 2015 14:00
-
-
Save takahi5/906fead14ba0a034541b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using Uniduino; | |
public class AngleSensorScript : MonoBehaviour { | |
public Arduino arduino; | |
public int pinValueX; | |
public int pinValueY; | |
public int pinValueZ; | |
private GameObject cube; | |
// Use this for initialization | |
void Start () { | |
arduino = Arduino.global; | |
arduino.Setup (ConfigurePins); | |
cube = GameObject.Find("Cube"); | |
} | |
void ConfigurePins( ) | |
{ | |
// Use Analog output 3, 4, 5 pin | |
arduino.pinMode(3, PinMode.ANALOG); //x | |
arduino.pinMode(4, PinMode.ANALOG); //y | |
arduino.pinMode(5, PinMode.ANALOG); //z | |
arduino.reportAnalog(3, 1); | |
arduino.reportAnalog(4, 1); | |
arduino.reportAnalog(5, 1); | |
} | |
// Update is called once per frame | |
void Update () { | |
pinValueX = arduino.analogRead (3); | |
pinValueY = arduino.analogRead (4); | |
pinValueZ = arduino.analogRead (5); | |
double degX = Mathf.Atan2(pinValueX - 507,pinValueZ - 558) / 3.14159 * 180.0 ; | |
double degY = Mathf.Atan2(pinValueY-520,pinValueZ-558) / 3.14159 * 180.0 ; | |
cube.transform.rotation = Quaternion.Euler((float)degX, 0, (float)degY); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment