Conveyor belt in Unity controlled by Arduino

Introduction

In this arti­cle we will see a down­load­able solu­tion that con­sists of a con­vey­or belt made in Uni­ty, whose oper­a­tion is con­trolled using an Arduino plate.

MOST SEARCHED VIDEOS FROM MY CHANNEL

ABOUT UNITY

ABOUT BLENDER

Downloads

In the next pack­age you will find two fold­ers, one with the Uni­ty files and one with the Arduino files.

Inside the Uni­ty fold­er you will find a com­pi­la­tion to test the sim­u­la­tion and also a Uni­ty pack­age that you can import into your project and access the assets of this simulation. 

GDT Conveyor Belt in Unity Controlled by Arduino Shield
GDT Con­vey­or Belt in Uni­ty Con­trolled by Arduino Shield
Ver­sion: 1.0
Pub­lished: Jan­u­ary 28, 2020

Upload the program to the Arduino

In the down­loads you will find a fold­er called "Arduino", which con­tains the pro­gram that reads the dig­i­tal entries, forms a word and sends it through the ser­i­al port.

The pro­gram is quite sim­ple so it will work on any Arduino board you can get, in my case I used an Arduino Mega.

In the fol­low­ing video you can see how to record the pro­gram on the Arduino board, you will need the Arduino soft­ware.

Video 1: Upload the pro­gram in Arduino

Circuit for digital inputs

The Arduino pro­gram that comes with the down­load uses pins 2 and 3 for the dig­i­tal inputs. Pin 2 con­trols the sta­tus of the feed­ing machine and pin 3 con­trols the sta­tus of the con­vey­or belt.

There are many ways to con­nect sig­nals to the Arduino's dig­i­tal input, I will show two alter­na­tives using switches.

WARNING
BE SURE TO CONNECT THE ELEMENTS TO THE ARDUINO PROPERLY, A SHORT CIRCUIT CAN DESTROY THE BOARD.
IT IS NECESSARY TO KNOW THE LEG DISTRIBUTION OF EACH PARTICULAR CONTACT, BETTER IF YOU HAVE A TESTER TO MEASURE CONTINUITY.

Alternative 1: Two-position switch

In my case I used two posi­tion switch­es for the dig­i­tal inputs, in these switch­es the mid­dle ter­mi­nal makes con­tact with one end or the oth­er depend­ing on the posi­tion of the switch. There­fore, at the ends we con­nect the high and low volt­age sig­nals (in my case 5 Volts and 0 Volts respec­tive­ly). The mid­dle ter­mi­nal of the switch is con­nect­ed to the dig­i­tal input of the Arduino. 

I repeat the warn­ing, the switch I used is like the one shown in fig­ure 1 and in this par­tic­u­lar case it is con­nect­ed that way.

two-position switch for arduino digital inputs
Fig. 1: Wiring dia­gram of a two-posi­tion switch for dig­i­tal input of the Arduino

Alternative 2: Pull-up resistor switch

Per­haps you only have one switch, i.e. a con­tact that clos­es or opens. In the Arduino's dig­i­tal inputs there should be con­stant high or low volt­age sig­nals, we should not leave dig­i­tal inputs uncon­nect­ed to any­thing (con­nec­tion to the air as we say here) as this will lead to unpre­dictable behavior. 

To do this, a small cir­cuit with a pull-up resis­tor must be imple­ment­ed, as shown in fig­ure 2.

In this case we use a resis­tor con­nect­ed to the GND ter­mi­nal of the Arduino and the oth­er end con­nect­ed on one side to the dig­i­tal input and on the oth­er to the switch, the oth­er end of the switch is con­nect­ed to the high volt­age sig­nal (5 Volt for example).

switch connection with pull-up resistor for arduino digital inputs
Fig. 2: Wiring dia­gram of a pull-up resis­tor switch for dig­i­tal input of the Arduino. 

The cir­cuit in fig­ure 2 works as fol­lows: when the switch is open there is no cur­rent flow­ing through the resis­tor, there­fore the 0 Volts are trans­ferred to the dig­i­tal input and the Arduino reads a low lev­el. When we acti­vate the switch the cur­rent will flow through the resis­tance and the Arduino's input will have a high lev­el signal.

Check if the Arduino program is working

The Arduino pro­gram is in charge of read­ing the sta­tus of the dig­i­tal inputs, cre­at­ing a bina­ry word that rep­re­sents those states and send­ing it by ser­i­al port.

If we open the Ser­i­al mon­i­tor, from the Tools tab, we should see the states of the dig­i­tal mes­sage inputs. As you can see in the fol­low­ing video. 

Video 2: Check that the Arduino pro­gram is working

Test the simulation

Once we have record­ed the pro­gram on the Arduino board, we can run the com­pi­la­tion that comes with the download.

When enter­ing the sim­u­la­tion, in the upper left cor­ner there is a but­ton to con­nect Uni­ty to the Arduino board.

In this part you may have prob­lems with the con­nec­tion, prob­a­bly cor­rect this in anoth­er ver­sion of the solu­tion. To make sure the con­nec­tion works, the Arduino board must be con­nect­ed before open­ing the com­pi­la­tion and the con­nec­tion process may take sev­er­al sec­onds to com­plete. If after 30 sec­onds the board is not con­nect­ed, check the pre­vi­ous step and reopen the sim­u­la­tion and try again.

Video 3: Prue­ba de la cin­ta trans­porta­do­ra en Uni­ty con­tro­la­da por Arduino.

Import package and fix "API Compatibility Level" error

To access the source files of the sim­u­la­tion is the Uni­ty pack­age, you can import it as seen in the fol­low­ing video.

Video 4: Import bun­dle and fix API Com­pat­i­bil­i­ty Lev­el error

When import­ing the pack­age we will get errors because some scripts can­not access the pack­age with the class­es that allow access to the ser­i­al port.

To fix this, fol­low the steps shown in video 4. Go to Edit > Project Set­tings > Play­er > Oth­er Set­tings and in the field "API Com­pat­i­bil­i­ty Lev­el" set ".NET 4.x".

How the Conveyor belt in Unity controlled by Arduino works

Inside the pack­age is the scene that is seen in the sim­u­la­tion, we can access it and see its ele­ments, in fig­ure 3 we can see the hier­ar­chy of the scene.

In the hier­ar­chy we have three machines, "Toori Machine" is the machine that places objects on the con­vey­or belt. "GDT Con­vey­or Belt" is the con­vey­or belt and "Box" is the box where the objects fall and are destroyed.

hierarchy of conveyor belt simulation in arduino controlled unity
Fig. 3: Hier­ar­chy of the scene that comes inside the package. 

Conveyor Belt

The con­vey­or belt is a 3D mod­el with a struc­ture and a belt. 

To achieve the ani­ma­tion of the oper­a­tion what is done is to add off­set to the UV map of the tex­ture. In this case, as the tex­ture is ver­ti­cal­ly dis­trib­uted, we add off­set to the Y com­po­nent of the UV map. When the UV map has an off­set of 1 in the Y com­po­nent the tex­ture returns to the ini­tial state.

So to achieve the effect of the con­vey­or belt mov­ing for­ward or back­ward we grad­u­al­ly increase the UV map off­set on one of the components.

To make the belt trans­port objects, you have a Col­lid­er box in Trig­ger mode that detects when an object is on the belt, these objects must have a Rigid­Body com­po­nent assigned to them. 

Script that controls the conveyor belt in Unity.
Fig. 4: Script that con­trols the con­vey­or belt in Unity

Then in the Con­vey­or Script you define the OnTrig­ger­Stay func­tion that will be called when an object is on the con­vey­or belt. This func­tion will be in charge of mov­ing the object, to syn­chro­nize the move­ment of the belt with the move­ment of the object there is a para­me­ter in the inspec­tor called "Syn­cU­VMove­ment­Fac­tor", by default it is set to 2, as shown in fig­ure 2.

The con­vey­or belt has a pub­lic func­tion that allows it to be acti­vat­ed or deac­ti­vat­ed and that can be called from any oth­er Script. We exe­cute this func­tion with the infor­ma­tion we read from the Arduino board, that is, it will be acti­vat­ed or deac­ti­vat­ed accord­ing to the sta­tus of the dig­i­tal input 3 of the board.

Feeding Machine

To make the sim­u­la­tion a lit­tle more com­plete, a machine that places objects on the con­vey­or belt is includ­ed. This machine is con­trolled with the "Feed­ing­Ma­chine" script and also has a pub­lic method that allows you to turn it on or off. This machine will be con­trolled with the dig­i­tal input 2 of the Arduino.

Script that controls the machine that places objects on the conveyor belt in unity
Fig. 5: Script that con­trols the machine that places objects on the con­vey­or belt

In the inspec­tor we can place the pre­fab of the objects to be placed on the con­vey­or belt and we can change the times between one object and another.

Communication between Arduino and Unity

Esta parte se resuelve uti­lizan­do una ver­sión mod­i­fi­ca­da de la solu­ción WRMHL com­par­ti­da por el equipo RELATIVTY. Te ani­mo a vis­i­tar los links para ver la solu­ción orig­i­nal y los proyectos.

This part is solved using a mod­i­fied ver­sion of the WRMHL solu­tion shared by the RELATIVTY team. I encour­age you to vis­it the links, see the orig­i­nal solu­tion and the projects they have.

In order to com­mu­ni­cate Uni­ty with Arduino you must have a GameOb­ject in the hier­ar­chy with the "wrmhlRead" script assigned. 

Fig. 6: Script that con­trols com­mu­ni­ca­tion between Arduino and Unity 

I have added an inte­ger called "arduinoMes­sage­Length" in the inspec­tor, which indi­cates the length of the bina­ry word that is sent from the Arduino. This is used to cor­rect­ly syn­chro­nize Uni­ty with Arduino in case there are more devices using the ser­i­al port. 

In our case, since we sent a two-dig­it word from the Arduino, you must put two in that field. If you add more dig­its to the word, you must mod­i­fy this field appropriately.

The read­ing of the Arduino mes­sage is done in the "ReadMes­sage­An­dAc­tion()" method. Here inside we can per­form the action we want in Uni­ty. In our case we exe­cute the pub­lic meth­ods of the Con­vey­or­Belt and Feed­ing­Ma­chine scripts, to acti­vate or deac­ti­vate the machines accord­ing to the Arduino's command.

Conclusion

We've seen how the con­vey­or belt solu­tion works at Uni­ty con­trolled by Arduino.

With the files pro­vid­ed you can cre­ate a new sim­u­la­tion accord­ing to your needs. 

Scroll to Top
Secured By miniOrange