Arduino is connected to PC with USB so can be accessed via Serial I/O
and Port
. You first need to initialize serial and define port in you unity's Start()
method so it can listen to. In my code i am listening to the port from Update()
method. If you want to send input
to arduino see the method SerialCmdSend(string data)
which takes argument as string
value. You can define this according to your need or change it in arduino code.
Unity C# Code:
using UnityEngine;
using System.Net;
using System.IO;
using System.IO.Ports;
using System;
public class Controller : MonoBehaviour
{
SerialPort serial = new SerialPort();
void Start()
{
InitializeSerial();
}
void InitializeSerial()
{
try
{
serial.PortName = "Your Port";
serial.BaudRate = 9600;
serial.Handshake = System.IO.Ports.Handshake.None;
serial.Parity = Parity.None;
serial.DataBits = 8;
serial.StopBits = StopBits.One;
serial.ReadTimeout = 20;
serial.WriteTimeout = 50;
serial.DtrEnable = true;
if (!serial.IsOpen)
{
serial.Open();
}
}
catch (Exception e)
{
print("Error: " + e.Message);
}
}
void Update()
{
//check serial
if (serial.IsOpen)
{
try
{
//print("serial " + serial.ReadByte());
if (serial.ReadByte() == 0)
{
//Do something here
}
else
{
// Do something else
}
}
catch (Exception e)
{
print("Error: " + e.Message);
}
}
}
public void SerialCmdSend(string data)
{
if (serial.IsOpen)
{
try
{
byte[] hexstring = Encoding.ASCII.GetBytes(data);
foreach (byte hexval in hexstring)
{
byte[] _hexval = new byte[] { hexval };
serial.Write(_hexval, 0, 1);
Thread.Sleep(1);
}
}
catch (Exception ex)
{
print(ex);
}
}
}
}
Arduino Code:
Check your port first in the Arduino IDE, select Tools >> Ports >> The number that your Arduino is connected to and copy and paste this port in Unity code above.
const int RELAY_PIN1 = 3;
const int RELAY_PIN2 = 4;
const int buttonPin = 2;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
int incomingByte = 0;
bool onLight = false;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(RELAY_PIN1, OUTPUT);
pinMode(RELAY_PIN2, OUTPUT);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
}
if (buttonState == LOW) {
Serial.write(0);
Serial.flush();
}
// Delay a little bit to avoid bouncing
delay(20);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
if(onLight)
{
digitalWrite(RELAY_PIN1, HIGH);
digitalWrite(RELAY_PIN2, HIGH);
delay(500);
digitalWrite(RELAY_PIN1, LOW);
digitalWrite(RELAY_PIN2, LOW);
delay(500);
}
else
{
digitalWrite(RELAY_PIN1, LOW);
digitalWrite(RELAY_PIN2, LOW);
}
if (Serial.available() > 0)
{
char ltr = Serial.read();
if(ltr == 'r')
{
onLight = true;
}
else if(ltr = 'g')
{
onLight = false;
}
}
This is my code according to my need. You can totally change it or write your own code. The important thing is how do you want to receive input from Arduino to Unity. I am using Serial.write(0)
to receive input as integer value from Arduino. You may receive as string by using Serial.println("on")
. You should check Arduino docs here for more information.
Please let me know if you need further assistance.