How can I connect my Arduino Mega Board to Unity? (MacOS)

Hello there!

Hope here are some Macbook Users which can help with my Problem.
I connected my Arduino Board with two Buttons to my Macbook Pro, by running the Code in the Arduino IDE the Buttons do work and by pressing any of them they "JUMP" or "DUCK".

So my problem now is, I want to control my Game in Unity with my Arduino Board. With the Buttons I want to simply Jump or Duck. But by running the Unity Game I get an Exception everytime, but my Serial Port is correct. I searched for it in my Terminal by "ls /dev/cu.*"

Can anyone help with that issue? I don't think that my code is wrong or the problem, but somehow my Unity can't connect to this SerialPort nor find it...

Welcome to the forum

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Which Arduino board do you have ?

Hi thanks for your help! I work with a Mega Controller Board.

Heres the code:

const int _jumpPin = 2;
const int _duckPin = 3;
const int _ledPin = 11;

int _lastJumpButtonState;
int _lastDuckButtonState;


void setup() {
  pinMode(_jumpPin, INPUT);
  pinMode(_duckPin, INPUT);
  pinMode(_ledPin, OUTPUT);

  Serial.begin(9600);
  Serial.setTimeout(5);
  Serial.flush();
}

void jump() {
  int jumpButtonState = digitalRead(_jumpPin);

  if (jumpButtonState == HIGH && jumpButtonState != _lastJumpButtonState) {
    Serial.println("JUMP");
    Serial.flush();
  }

  _lastJumpButtonState = jumpButtonState;
}

void duck() {
  int duckButtonState = digitalRead(_duckPin);

  if (duckButtonState != _lastDuckButtonState) {
    if (duckButtonState == HIGH)
      Serial.println("DUCK");
    else
      Serial.println("NO_DUCK");

    Serial.flush();
  }

  _lastDuckButtonState = duckButtonState;
}

void loop() {
  jump();
  duck();
}

It might be useful if you actually told us what exception you are seeing. :popcorn:

Also, if the exception is originating within the "Game in Unity", is it not more likely to be a problem there, rather than in the Arduino side of things? In which case, it's not really an Arduino problem, is it?

How are the input pins wired ?
Are there pulldown resistors keeping them in a known LOW state until the buttons are pressed or are they floating at an unknown voltage, maybe HIGH, maybe LOW ?

I am not familiar with Unity. What is it expecting to receive and how do you define the serial port that it uses ?

Note that by using the Arduino Serial port for Unity it is also used by the Arduino Serial monitor and in general only one device can use it

Consider using a different Serial port to connect to Unity to separate it from the Serial monitor but you will need extra hardware to do this

IOException: No such File or Directory

My Unity Game can't open the Serial Port of my Arduino Board, like it's not the right one or it is open somewhere else.

I thought if someone here creates some Controller Inputs/Output with Arduino for Unity, could help me with this problem

Well, that error seems pretty straightforward and seems to have nothing at all to do with your Arduino hardware or sketch. Your file name or path is incorrect. I think you would do better asking in a forum dedicated to the Unity game engine. And, perhaps you might considering showing them the code in question that's failing.

The input pins are correctly wired and I don't have a problem with the wiring, because on a windows the same wiring is working correctly and without problems.

I get the Serial Port number from the Arduino IDE when selecting the Board, you can see it under the name of the connected Board

In my Unity Code I'm opening the Serial Port by Name but somehow the Serial Port won't work, but I guess I have to search for a solution somewhere else. But thanks anyway!

Mac user here, no idea what Unity is.

It's apparently a game engine. And yeah, I had to look it up.

The Serial port shown by the IDE is on the PC and it is used to upload the code to the Arduino and to send and receive data using the IDE Serial monitor so it is already in use when Unity, whatever that is, tries to use it

That could be a problem

Does the Mac OS regard serial ports as files or directories ?