Lab Course week 4:

Arduino and Unity

Work in progress: we are refactoring this module in the spring semester of 2025-2026, so the below is likely to change.

Preparation

This final more or less organised week, we are going to connect our Arduino with the software of Unity, thereby going through the looking glass. We are going to let the physical world enter the real, and vice versa.

  1. In order to get the most out of this workshop, you should have at least exercise 2 of week 3 ready to roll. In that exercise, we used the serial monitor to send data (e.g. red or green) to the Arduino and have it put on the LED with the corresponding color.
  2. Have a look at this explanation of multi-threading by Computerphile. This explains how we can have a computer do multiple things at one time. Don't worry if you don't understand it completely – a little working knowledge will suffice.
  3. If you want, you could also read up on the internals of serial communication. However, this goes quite deep and is not necessary for accomplishing our dream.

IMPORTANT: In the exercises below, you will get some unity and some arduino code. This is tested and set up for this particular workflow. Do not change it if you do not know what you are doing. Don't ask chatgpt (or the like) to change it if it doesn't work as you expect it to – you will get into more trouble if you go throught that path. Instead, ask one of us to help you if it doesn't work out.

Exercise 1: blinking LEDs again

Using you breadboard, hook up two LEDs: one on port 12 and one on port 13. Download this code and upload it to your Arduino. Now, if you type an A in the Serial Monitor of the Arduino, the LEDs start blinking.

The loop()-method actually contains two blocks. The first block is dependent on the value of Serial.available():


    if (Serial.available() > 0) {
      // String command = Serial.readStringUntil('\n');
      // command.trim();
          char command = (char)Serial.read();
      if (command == 'A') {
        running = true;
      } else if (command == 'B') {
        running = false;
        digitalWrite(led1, LOW);
        digitalWrite(led2, LOW);
      }
    }
The second block actually makes use of the variable running that was set in the first block:

    if (running) {
      static unsigned long lastToggleTime = 0;
      static bool ledState = false;
    
      if (millis() - lastToggleTime >= 500) {
        lastToggleTime = millis();
        ledState = !ledState;
        digitalWrite(led1, ledState);
        digitalWrite(led2, !ledState);
      }
    }

Within your group, discuss what is going on in this code; why do we need two blocks and how do they work together?

Exercise 1a: check the serial communication

Clone this UnityArduinoTemplate with your GitHub Desktop or download the zip-file. Add this project to your UnityHub. If necessary download the right editor version. If you do not want to install a new project but adjust your project, you can download this package and add it to your project

Open the scene Student_Scene from the folder _StudentWorkHere. Addjust the settings from the SerialPortManager in the inspector. Change the Port to the port you have connected your Arduino to and check the Bautrate.

Hit the play button, check the Console for what is happening. Check if the serial connection had been made.

Is anything changing with your Aduino setup on starting and stopping the Unity Scene?

Exercise 2: two-way interaction - Check out the Demo folder in the Unity Project

Until now, we have only communicated from the computer to the Arduino. However, nothing prohibits us from doing the same thing from the Arduino to Unity.

Open the scene SpeedByPot from the folder SerialCommunication/Demo/Scenes. Addjust the settings from the SerialPortManager in the inspector. Change the Port to the port you have connected your Arduino to and check the Bautrate.

Copy the file ReadAndWrite.ino from the folder Docs to a different place on you computer and upload this code to your Arduino

For this to work, you need to add a potentiometer to your breadboard and hook up its washer to port A0. Make use of the knowledge you gained at part 2 of exercise 2 of week 2 😎 to have the value of the potentiometer send over the serial port.

Check the Serial Monitor or the Serial Plotter of the Arduino IDE to ascertain that the potentiometer is working and that the Arduino is sending the value of the potmeter over the USB cable.

Note: in order for this to work you need to close the Serial Monitor, as only one process can listen to incoming messages on a serial port at any give time. If the Serial Monitor is doing that, you cannot hav

Check out all the different things that are happening in the scene by turning the potentiometer on your breadboard. And check out the documentation in the Docs folder.

Assignment

Hamburger-menu