Skip to content

Instantly share code, notes, and snippets.

@hhayley hhayley/ArduinoSerial.cs
Last active Jun 13, 2019

Embed
What would you like to do?
ITP CAMP 2019 - Unity C# scripts for Serial Communication from Arduino
using UnityEngine;
/*
* To use 'System.IO.Ports;', you should change in Player settings
* Build Settings - Player Settings - 'Player' tab
* Configuration - Api Compatibility Level* ---> change to ‘NET 4.x’
*/
using System.IO.Ports;
public class ArduinoSerial : MonoBehaviour
{
public string port; //ex) MAC Arduino Port "/dev/cu.usbmodem146101"
public int baudrate = 9600; // ex) Arduino : Serial.begin(9600);
private SerialPort sp;
public string incomingData;
public Material objMaterial;
private Color cubeColor;
private float sensorValue;
void Start()
{
sp = new SerialPort(port, baudrate);
sp.ReadTimeout = 50;
sp.Open();
}
void Update()
{
try
{
incomingData = sp.ReadLine();
sensorValue = float.Parse(incomingData);
Debug.Log(sensorValue);
}
catch (System.Exception)
{
}
Color sensorColor = objMaterial.color;
float mPotVal = scale(0.0f, 1024.0f, 0.0f, 1.0f, sensorValue);
sensorColor.a = mPotVal;
objMaterial.color = sensorColor;
/*
//Write to Arduino
if (Input.GetKeyDown("1"))
{
sp.Write("1");
sp.BaseStream.Flush();
}
else if (Input.GetKeyDown("0"))
{
sp.Write("0");
sp.BaseStream.Flush();
}
*/
}
//Map the sensor value
public float scale(float OldMin, float OldMax, float NewMin, float NewMax, float OldValue)
{
float OldRange = (OldMax - OldMin);
float NewRange = (NewMax - NewMin);
float NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin;
return (NewValue);
}
}
using UnityEngine;
/*
* To use 'System.IO.Ports;', you should change in Player settings
* Build Settings - Player Settings - 'Player' tab
* Configuration - Api Compatibility Level* ---> change to ‘NET 4.x’
*/
using System.IO.Ports;
public class ArduinoSerialMultiple : MonoBehaviour
{
[Tooltip("Arduino Port")]
public string port = "/dev/cu.usbmodemXXX";
[Tooltip("Arduino Serial Baudrate")]
public int baudrate = 9600; // ex) Arduino : Serial.begin(9600);
private SerialPort sp;
public string incomingData;
[Tooltip("The number of values from Arduino")]
public int dataLength;
public Material objMaterial;
private float potValue;
private float windSensorValue;
void Start()
{
sp = new SerialPort(port, baudrate);
sp.ReadTimeout = 50;
sp.Open();
}
void Update()
{
try
{
incomingData = sp.ReadLine();
OnMessageArrived(incomingData);
}
catch (System.Exception)
{
}
Color sensorColor = objMaterial.color;
float mPotVal = scale(0.0f, 1024.0f, 0.0f, 1.0f, potValue);
sensorColor.a = mPotVal;
objMaterial.color = sensorColor;
float mWindVal = scale(550.0f, 1024.0f, 0.1f, 5.0f, windSensorValue);
this.transform.localScale = new Vector3(mWindVal, mWindVal, mWindVal);
//Write to Arduino
if (Input.GetKeyDown("1"))
{
sp.Write("1");
sp.BaseStream.Flush();
Debug.Log(1);
}
else if (Input.GetKeyDown("0"))
{
sp.Write("0");
sp.BaseStream.Flush();
Debug.Log(0);
}
}
void OnMessageArrived(string msg)
{
Debug.Log("Message arrived: " + msg);
//split the message by comma
string[] tokens = msg.Split(',');
//Debug.Log(tokens.Length);
dataLength = tokens.Length;
// Convert String to float
float numVal = float.Parse(tokens[0]);
potValue = numVal;
float numVal2 = float.Parse(tokens[1]);
windSensorValue = numVal2;
}
//Map the sensor value
public float scale(float OldMin, float OldMax, float NewMin, float NewMax, float OldValue)
{
float OldRange = (OldMax - OldMin);
float NewRange = (NewMax - NewMin);
float NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin;
return (NewValue);
}
}
using UnityEngine;
/*
* To use 'System.IO.Ports;', you should change in Player settings
* Build Settings - Player Settings - 'Player' tab
* Configuration - Api Compatibility Level* ---> change to ‘NET 4.x’
*/
using System.IO.Ports;
public class ArduinoSerialSimple : MonoBehaviour
{
public string port; //ex) MAC Arduino Port "/dev/cu.usbmodem146101"
public int baudrate = 9600; // ex) Arduino : Serial.begin(9600);
private SerialPort sp;
public string incomingData;
void Start()
{
//Open Serial Port
sp = new SerialPort(port, baudrate);
sp.ReadTimeout = 50;
sp.Open();
}
void Update()
{
//Read from Arduino
try
{
incomingData = sp.ReadLine();
Debug.Log(incomingData);
}
catch (System.Exception)
{
}
//Write to Arduino
if (Input.GetKeyDown("1"))
{
sp.Write("1");
sp.BaseStream.Flush();
}
else if (Input.GetKeyDown("0"))
{
sp.Write("0");
sp.BaseStream.Flush();
}
}
void OnApplicationQuit()
{
sp.Close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.