All we're going to do for now is to continuously send serial messages to Unity along with a count of how many times we've looped. If you've ever worked with Arduinos before, you'll already be familiar with the Serial.print() family of functions.
#Arduino serial plotter scale code
This part is relatively easy compared to the C# code above. If you can not program your Arduino because access to the serial port is denied, simply run your Unity game again and hit the "Escape" key to close the port, then stop your game. One thing to note is that we no longer automatically close the serial port.
We can ignore these with a try/catch.ĭebug.Log( "ARDUINO->|| " + inData + " ||MSG SIZE:" + inSize.ToString()) Try //Sometimes malformed serial commands come through. If (Input.GetKeyDown(Ke圜ode.Escape) & sp.IsOpen) Removed the sp.Close line since we're now polling data. You only want to send commands to the Arduino during lower-priority times like handshaking and initialization. When you send a command, it will interrupt the incoming data stream until the transmission is complete, blocking out any further input from the Arduino. I should point out that there is a downside to sending commands to the Arduino.
Sp.Close() //You can't program the Arduino while the serial port is open, so let's close it. Sp.WriteTimeout = 1 //Shortest possible write time out.ĭebug.LogError( "Serial port: " + sp.PortName + " is unavailable") Sp.ReadTimeout = 1 //Shortest possible read time out. A 10 uF cap across RST and GND will prevent this. Sp.DtrEnable = false //Prevent the Arduino from rebooting once we connect to it. Sp = new SerialPort( "COM4", 115200, Parity.None, 8, StopBits.One) //Replace "COM4" with whatever port your Arduino is on. All code examples assume you will be working in Unity/C# and the Arduino IDE.Ĭreate a new C# script (I called mine "Interface") in Unity and attach it to any game object. Let's begin by brute-force connecting to the Arduino and sending our first command over serial. Because of this, I was forced to manually connect to the Arduino, verify that the device I'm talking to is the Arduino controller, and poll for incoming serial data myself. Namely, the event that is fired whenever there is incoming serial data does not work. The problem is, Unity, which uses the Mono compiler, does not fully support all of the great. My professor at the time previously expressed a great interest in hobby electronics and Arduinos, so I thought, "This is a perfect chance to create something that I love to work with too!" I forked my previous attempt at a C# serial communication system and began work on the "new and improved" version for my final project immediately.Ĭ# actually has pretty great support for serial ports right out of the box. In my final semester in the game program at my college, I felt that I needed to end my four year journey with something special - something that hasn't been done before at this school. It was a neat experiment, but it was ultimately shelved. It honestly didn't take too long before I had a basic proof-of-concept program that could set pins high and low on an Arduino board, but my original system was not very robust, reliable, or even intuitive to use. Audio Mixing, Big Class D Amps, and Light ShowsĪ couple of years ago, I attempted to write my own C# Arduino serial communication system so that I could interface my electronics projects through the Unity game engine.MegaBlaster Hardware Sega Genesis Video Game Music Player.Hardware Sega Genesis Video Game Music Player 3.0.Hardware Sega Genesis Video Game Music Player 2.0.