The code that will run on Feather will receive a numerical value over the USB serial connection and rotate the servo shaft an amount corresponding to that value – visually indicating which on-screen cube was activated.

Program the Feather

Connect your Feather to your computer using data + power, known good micro B USB cable.

Open the Arduino IDE, and from the top menu, go to Tools -> Board and ensure Adafruit Feather M4 Express (SAMD51) is selected. Then go to Tools -> Port and select the port corresponding to your Feather board – this is usually the last entry in the list.

Now that you know the Feather's port address, briefly head back into Unity and enter it in the Serial Comms script's com port field as seen above.

Go to File -> New to create a new sketch and delete the default code that appears inside. Click Copy Code on the window below, return to Arduino, paste the code into the sketch and save it.

#include <Servo.h>

Servo servo;
int numberRecvd;
String dataString = "";

void setup(){
  
  Serial.begin(9600);
  
  pinMode(5, OUTPUT);
  servo.attach(5);
  servo.write(0);
}

void loop(){
  
  if (Serial.available() > 0){
    
    dataString = "";
    while (Serial.available() > 0)
    {
      dataString += char(Serial.read());
      delay(2);
    }

    numberRecvd = dataString.toInt();

    switch (numberRecvd) {
      case 1:
        servo.write(125);
        break;
      case 2:
        servo.write(90);
        break;
      case 3:
        servo.write(55);
        break;
      default:
        servo.write(0);
        break;
    }

    Serial.flush();
    Serial.print("received: ");
    Serial.println(numberRecvd);
  }
  
  delay(20);
}

Go to Sketch -> Upload to send the project code to your Feather.

Reposition pointer

After the code is uploaded, you should hear the servo activate as it positions to its zero point.

Loosen the screw from the servo’s output shaft and reposition the horn and pointer so that they’re pointing to the right as seen above, with the servo wires feeding out below).

Secure the horn & pointer in place with the screw.

This guide was first published on Jan 20, 2021. It was last updated on Jan 20, 2021.

This page (Arduino Code) was last updated on Sep 29, 2021.

Text editor powered by tinymce.