Decryptor Dev Log - Arduino

By Kunjal Purohit

    The first challenge was adding the serial port to the program. I attempted to solve this issue by looking it up on the internet. Afterwards, I found the software called Arduity that somebody has already built. Installing this, allowed me to gain a better understanding of how the communication works between Arduino and Unity. Since I was not familiar with the functions of Unity Hub, I was struggling to correct the COM port from its default: COM7, to COM3. However, after some guess and check with the application, I realized I had to change the COM on the Unity Editor, save that, then remove the default on Unity Hub and add a new SerialController with COM3.

Screenshot of changing the COM port in the code. Screenshot of changing the COM port in Unity Hub.

    While I was working on this, I realized that the Api compatibility level is incorrect for this project. Thus, I changed it to allow for effective communication between the devices.

Changing the Api Compatibility Level to allow for Serial Port communication to Arduino. Edit→ Project Settings → Player → Api Compatibility Level → .NET 4.x

    After I got this running, I connected my pieces of code for the Arduino software, received the command from Unity to display the cross and check, and the Arduino keypad. Arduino Code:

Setting up the basis for me to run the code. Code for Keypad. Setup and loop to run both the I^2C Display and the 4x4 Matrix. Code for the Check and Cross that appears on the I^2C Display. Correspondence of 4x4 Matrix with the previous code of QWERTY and Numbered Button.

    Another challenging aspect I encountered was integrating the serial port functions into the Unity Modules. It turns out that I needed the object. To obtain the object name of the serial controller, I needed to make the Serial Controller Functions work in the respective Unity Modules, which was missing. I acquired the object in the startup function of each of those Unity Modules. Subsequently, with that, I was able to have the Serial Controller Functions work. I added integration in the general checker where it recognizes matching options. I included a function to send the check mark, which is transmitted by sending the character “C”. When an incorrect combination is detected, it sends the letter “X” to Arduino. Upon receiving this, it displays a cross.

Code to fetch the object, find “Serial Controller” and send information to Arduino on whether to send a cross or a check.

    Next, I integrated the keypad by adding code to the existing Button Logic. I included the translation code to map the Arduino keypad codes. Once again, I encountered a problem: the keystrokes pressed on the Arduino would not map correctly. The process is as follows: the keystroke is pressed on the Arduino at a certain point in time, and the command is received in Unity. However, the issue lies in the sequential nature of the keystroke scanning in Unity, which checks for each tile button one at a time. The time when the tile is being checked does not align with the time when the respective button for the tile would be triggered. Thus, the interface misses the intersection point between the signals for both. Consequently, I need to cache the button pressed on the Arduino.

    To provide that button when the respective tile is scanned, I needed to add a caching logic to cache the pressed button until the tile has been scanned. So then, I save the last pressed button on the Arduino. When the tiles are scanned and the respective tile comes up, I extract the cache value and provide it for the tile. Using this similar logic, I had to save the value of the cached button across different tiles. That means preserving the value across various update calls to the button logic. For that, I used a static variable which allowed me to save the value and share the same value across different tile objects. After I added that code, I could correctly see the keypad presses translating to the button presses. And then the whole code started working end-to-end.

Copyright 2023

For Team Access Only:

Sign In