A demo for mirroring a servo's motion in Unity and controlling the motor in Unity. An Express server connects to the Arduino server via the Johnny Five API, which communicates to Unity through endel's Native Websocket library.
Watch it in action on YouTube: https://youtu.be/OCzmfuKflg4
- Connect the Arduino to your computer using a USB connection.
- Connect the servo to the Arduino using the following circuit:
- Power to the Arduino's 5V pin or to a driver
- Ground to GND
- Signal pin to pin 9
- Feedback pin to pin A0 (under Analog In)
- Open the Arduino IDE and select the Arduino.
- Upload the
StandardFirmata
sketch. (File > Examples > Firmata > Standard Firmata)
- Set up the Arduino following the steps in the section above.
- Open a terminal window in the
UnityTwinning
directory. - Run
cd Server
to navigate to the server subdirectory. - Create a
.env
file in the subdirectory withIP_ADDRESS=YOUR_IP_ADDRESS
, and fill in your server's IP address (e.g.localhost:3000
) - Run the following commands in the terminal window to start the server:
npm install
npm run
- Wait for the servo to calibrate.
- Open the
UnityTwinning
scene in theAssets\Scenes
folder. - Click on the
WebsocketMgr
in the scene and input the IP address and port in theWebsocketMgr
component. - Press play to connect to the server. This step must be done AFTER the server says it is ready for input
To control the motor in the UnityTwinning
Scene use the keyboard:
- Q: Rotate motor counterclockwise
- E: Rotate motor clockwise
Tested with:
- Arduino: Uno R3
- Servo: Datan S1213 Analog Servo Motor with Feedback
- Unity version: 2022.3.1f1
- Native Websocket: 1.1.4
npm list
: