Introduction: Rotational Input From Arduino Using LDR(light-dependent Resistor) to Unity

About: 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. I love to explore new ways and methods such as sensors as inputs f…

from my experience , a lot of rotary input devices in Arduino depend on a potentiometer which provides a variable resistance. in this instructable, we will try to achieve the same effect using an LDR, LED and a Strip of paper that has various shades of a given color, could be white to black, attached to some form of a rotary mechanism(let's call it the color wheel) .

the main Idea behind this, is that the LED would shine a light on the Color wheel, which is reflected back to the LDR . and depending on which shade of color was in focus and the amount of absorption, a different intensity of light is reflected. we will use this variation to rotate a 3d object in unity as tool to Visualize the process.

Step 1: Lets Collect the Components

1- LDR

2- LED, preferable white but any color would do

3- 2 Resistors

4- a tape to mount our color paper on.

5- a strip of paper containing a gradient shading of any color, could be printed or cut from a book. just make sure the length of the paper is enough to cover the diameter of the tape, or which ever rotary mechanism you have in mind , and enough width to ensure good reflection. I used 2.4cm wide.

6- 7 male - male wires

7-some rotary mechanism to rotate our color wheel on.I used lego.

Step 2: Lets Assemble Our Color Wheel :)

1- find or print a strip of paper containing a gradient shading of any color. just make sure the length of the paper is enough to cover the diameter of the tape, or which ever rotary mechanism you have in mind , and enough width to ensure good reflection. see picture

2-wrap the paper around the tape and tape it.

3-let your creativity run wild and find a rotary mechanism to rotate your tape, I was lucky to find lego pieces that fit exactly .

Step 3: Lets Make Our Arduino Circut

please see the pictures included

Step 4: Time to Code : Arduino Part1

the code is simple but the values are most likely to be different depending on your LDR, lightning of your environment and the color you picked. so what we need to find out first is the range of LDR readings at the brightest shade and darkest one. simply observe the value on your serial Monitor using the following code:

void setup() { // put your setup code here, to run once:
  Serial.begin(9600);
}
void loop() {
  // put your main code here, to run repeatedly:
  int data = analogRead(A0);
  Serial.println(data);
  delay(20);
}

Step 5: Time to Code: Arduino Part2

once you have written your range of LDR readings it's time to make sense of the data, we are going map our lowest reading to 0 and our highest to 360 so that it corresponds directly to rotation in Unity. then to make sure that our values are within our range we constrain the value , here's the code:

void setup() {  // put your setup code here, to run once:
  Serial.begin(9600);
}void loop() {
  // put your main code here, to run repeatedly:
  int data = analogRead(A0);
  data= map(data,455,600,0,360);
  data=constrain(data,0,360);
  Serial.write(data);
  delay(20);
}

Step 6: Our Unity Code :)

please make sure that your unity projects allows serial Communication by

1- edit>project settings>player> scroll down to optimization and change api compatibility to .NET 2.0

2- create a 3dObject of your choice

3- and attach the following script to it which reads the values and sets the angle.

<p>using UnityEngine;<br>using System.Collections;
using System.IO.Ports;
public class Rotate : MonoBehaviour {
    public float speed;
    private int angle=0;
    private SerialPort port = new SerialPort(@"\\.\"+"COM11",9600);
	void Start () {
        port.Open();
        port.ReadTimeout = 25;
	}
	
	
	void Update () {   

 if (port.IsOpen) {   

        try {
                angle = port.ReadByte();
            }
            catch (System.Exception) {         }      }
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0f, angle, 0f),speed*Time.deltaTime);
	}
}