Download

[Free][Windows only] UE4Duino 2 - Arduino (COM port) communication

Install Visual Studio with C++ features, generate Visual Studio project files (available through right clicking the .uproject file) and start the project. Unreal Engine should propose rebuilding of the plugin.

Also, set “EnabledByDefault” to true with Notepad++ in the UE4Duino.uplugin file, if you have not done that already.

Thanks! That’s exactly what can be done. Or else, I’d have to upload a new zip for each hotfix version of the engine. And we’re already in 4.19.2.

Don’t. There’s a bug (don’t know if it’s still there, but it was so in 4.18) in the engine that prevents the plugin from compiling if it’s set to EnabledByDefault and you use it in a component. Some people have reported it not compiling even used in the Pawn blueprint. It’s like the engine considers it’s not being used and doesn’t compile. Then the game fails silently because a connection with the Serial port is not made. It’s frustrating.

I don’t know why but when I replay the game the data goes a lot more slower than the first time, but if i reconect the arduino starts great but if you replay again the slowliness starts again. Any solution to that? (I’m using flush port)

I think that i’ve found a bug, i’m trying to send a bunch of int’s via serial.print and in the console everything works fine but when i try on ue4 and the ReadLine node tells me that is nothing to read. The code is similar to this:


  
      String str = Serial.readString();

      if(str == "snd")
      {
        Serial.print(myIMU.roll, 2);
        Serial.print("/r");
        Serial.print(myIMU.pitch, 2);
        Serial.print("/r");
        Serial.print(myIMU.yaw, 2);
        Serial.print("/r");
        Serial.print(myIMU.ax, 3);
        Serial.print("/r");
        Serial.print(myIMU.ay, 3);
        Serial.print("/r");
        Serial.print(myIMU.az, 3);
        Serial.print("/r");
        Serial.print(v_flex1);
        Serial.print("/r");
        Serial.print(v_flex2);
        Serial.print("/r");
        Serial.print(v_flex3);
        Serial.print("/r");
        Serial.print(v_flex4);
        Serial.print("/r");
        Serial.println(v_flex5);
      }

I think that i’ve found a bug, i’m sending like 6 ints via


Serial.print();

and in the console everything works fine but in ue4 the Read Line node tells me that there’s nothing to read :confused:



      String str = Serial.readString();

      if(str == "snd")
      {
        Serial.print(myIMU.roll, 2);
        Serial.print("/r");
        Serial.print(myIMU.pitch, 2);
        Serial.print("/r");
        Serial.print(myIMU.yaw, 2);
        Serial.print("/r");
        Serial.print(myIMU.ax, 3);
        Serial.print("/r");
        Serial.print(myIMU.ay, 3);
        Serial.print("/r");
        Serial.print(myIMU.az, 3);
        Serial.print("/r");
        Serial.print(v_flex1);
        Serial.print("/r");
        Serial.print(v_flex2);
        Serial.print("/r");
        Serial.print(v_flex3);
        Serial.print("/r");
        Serial.print(v_flex4);
        Serial.print("/r");
        Serial.println(v_flex5);
      }


Bug is a very strong word lol
I guess you already know that, but Serial.println will be the one the plugin will consider to read everything before, because it sends the ’
’ break that ReadLine expects as a line break.
Also, are you certain that you are using the ReadLine function **after **Arduino has sent the all the data? Because the computer can read faster than Arduino can send, and then you’ll get either part of the data or even none of it.
The examples script I package within the plugin folder uses Serial.println and I use ReadLine just fine with that. But if I’m reading back from a command I’ve just sent from the engine, I wait for a small delay, to make sure Arduino has time to read the command and completely answer it.

Srry for using the word Bug, but i’m with this for like 6 hours and i don’t know what to do, here’s my ue4 code



Event Tick -> isValid? -> Print (snd) -> Delay(0.1) -> Read Line -> Print String
(Arduino)----------|--------------|---------------------------|


Event BeginPlay -> Open Serial Port -> Branch(True) -> Set Arduino -> Set WriteLineEnd

Event End Play -> isValid? -> ClosePort
(Arduino)--------------------------|


I recommend using Flush as soon as you open the port to make sure there’s no data left to be read there.



Event BeginPlay -> Open Serial Port -> Branch(True) -> Set Arduino -> Set WriteLineEnd -> Flush


Also use flush after Read Line. Just to make sure you won’t get the rest of what was not read in this call when you read it the next time.

By setting WriteLineEnd you’re just affecting the PrintLine function from the plugin, which you seem to not be using.

But those might not be the problem yet. What’s your loop frequency in Arduino? Do you use sleep to slow it down a bit? Is it receiving the whole “snd” string at once? It could be reading so frequently that it might be getting it in pieces.

I’m asking a lot of questions because those are the ones I ask myself when something isn’t working. To communicate with Arduino exchanging a lot of data is really cumbersome without some sort of protocol, because data might come in pieces all the time.

Loop frequency? The baud rate is 9600 and i’ve got to admit that the loop is a bit slower 'cause is making a lot of calculations (I’m using arduino mega). I don’t using sleep, and how can i know that its reciving the snd?

Thxs God it was a problem of mine not yours, now i’m reciving data (The problem was that i’ve used an if for a lcd that i wasn’t using that makes loop more slower.) Anyway thxs for your time

Hahaha it’s always something like that. That’s why I make myself all those questions. Even the basic ones like “is Arduino working?”, “is it receiving what I’m sending?”.
Glad you figured it out :slight_smile:

Hi guys… I’m an arduino pro, but a UE noob. I downloaded the current version of UE 4.19.2, I created a ‘Plugins’ folder within my project and drop the contents of the github project there. When starting UE, it complains about missing or out of date binaries. could it be my folder structure? or is 4.19.2 not supported?

my path is:
C:\Program Files\Epic Games\UE_4.19\MyProject\Plugins\UE4Duino

Hey! thanks for this plugin.

I have a litle problem with my Code, and I hope someone can help me. I’m changin’ de rotation of a character by an encoder, and, it works… more or less. The problem is the encoder makes too much ‘ticks’ with just 1 turn. I mean, it returns me like 2 or 3 ints, positive and negative, so… it fks me. I thinks is a problem with how encoders works, but I’m not sure. Someone knows how i can do this?

Unreal is telling you that you don’t have the binaries of the plugin compiled. You need VisualStudio (with c++) to compile them. Also, the engine only compiles code on code projects. In order to convert your project into a code project:

  • remove the plugin from it so you can launch the editor and open the project
  • go to File > New C++ class…
  • You can choose any class (just to add code to the project). I usually choose BlueprintFunctionLibrary because on most of my projects I end up needing one anyways.
  • After it compiles with the new class, close the editor. Your project is now a code project.
  • Add the plugin source files again and launch the project.
  • This time Unreal will ask you if you want to build the plugin. Say Yes.

If don’t want to do all that, on the latest plugin release there’s a zip with source/binaries for 4.19.2.

I don’t know what an encoder is, but it sounds like Arduino is sending information faster than the engine is reading. After each read on the engine, call Flush on the Arduino variable. That’ll clean up any extra information left in the serial port and, next frame, you read the latest information from Arduino.

Or, like many analog sensors I’ve used, it could be noise from the encoder (I still don’t know what it is, so I’m guessing). In that case, I’d filter the noise. A simple way to filter noise is to always add the latest value to a fixed size array and use the average from the values in the array. An array of length 5 should suffice. Most of the time, even less. You just have to keep track of the latest index you added to in order to keep cycling the array, always replacing the oldest value there.

Encoder is like an “endless potencimeter” that can check what is the direction of rotation. I’ve discoverd that the value I get depends of velocity of rotation, so… Im sure idk how to take int from arduino, but i solved it using a string wich printed the value of the int.

Now i have a new problem, i don’t know how to use more than 1 variable on unreal from arduino… and again, the value that “read int” returns is a fixed value, and not the value of variable I created on Arduino. I think I can use prints for everything i need, but it’d be interesting know how to use Ints, Floats…

Thanks for everything.

You’re welcome!

About the Ints and Floats from Arduino, they have to be broken down into 4 byte groups before being sent, respecting endianness. Then, using ReadInt etc in Unreal will re-group and interpret the next 4 bytes in the serial port. For examples of breaking ints and floats in Arduino and sending them to Unreal, take a look at the ArduinoTypesExample.ino file in the plugin folders. The float method in the example works for both floats and ints. The int method works only for ints.

For working with more than one variable, you can have synchronization problems. For instance, Unreal could start reading when Arduino has already sent 2 bytes of an int, making Unreal receive only the last 2 of them if you used flush on BeginPlay. And you should, because if you don’t, you might get all previous data in the port (and it could be a lot).
To solve the synchronization problem I use a “protocol”. I described this method in this post.

It works well. Thank you very much. ^^ Perhaps Is Bluetooth mode supported?

Wow… It’s hard to me get it… I’m trying to do what i want with this help, thanks. but again, I cannot choose what variable read with unreal. If I have 2 ints, for example, to use them in rotation Z and X, there is no method to tell unreal “Read int called Xrotation” or something like this…

Maybe I’m asking something obvious, but I’ve never use arduino before, and just got 2 month with Unreal. Students in practices life :D.

Tons of thanks, again, and sorry for this questions.

Ok… im trying to use your arduino code applied on my own code, and it doesnt work. I feel so useless xdd. I don’t understand how in 3 years there is no even 1 tutorial of how this plugin works