0
\$\begingroup\$

So I am making an interactive game using Unity and an Arduino Uno. However, I now want to migrate my Arduino project onto a Bluno Beetle boardBluno Beetle. I am using the following code on Unity:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{
    public static PlayerController Instance { set; get; }
    public float Speed;
    public Vector2 height;
    public float xMax, xMin, yMin, yMax;
    public bool buttonPressed = false;

    public Rigidbody2D Character;
    public Timer timer;
    public Rigidbody2D _rigidbody;

    public bool timerStarted = false;

    private SerialPort sp = new SerialPort("\\\\.\\COM9", 9600);

    Animator anim;
    string value;
    int button;

    private void Awake()
    {
        FetchReferences();
        Instance = this;

        //print("hi");

        if (AvatarChooser.ava == 1)
        {
            anim = GameObject.FindWithTag("Player").GetComponent<Animator>();
            anim.runtimeAnimatorController = Resources.Load<RuntimeAnimatorController>("3");
            //print("hi");
        }

        if (AvatarChooser.ava == 2)
        {
            anim = GameObject.FindWithTag("Player").GetComponent<Animator>();
            anim.runtimeAnimatorController = Resources.Load<RuntimeAnimatorController>("1");
        }

        if (AvatarChooser.ava == 3)
        {
            anim = GameObject.FindWithTag("Player").GetComponent<Animator>();
            anim.runtimeAnimatorController = Resources.Load<RuntimeAnimatorController>("2");
        }
    }

    [ContextMenu("FetchReferences")]
    private void FetchReferences()
    {
        if (!Character) Character = GameObject.FindWithTag("Player").GetComponent<Rigidbody2D>();
        if (!timer) timer = GameObject.Find("Timer").GetComponent<Timer>();
    }

    private void Start()
    {
        if (!sp.IsOpen)
        {
            sp.Open(); // Open 
        }
        sp.ReadTimeout = 1;

        //print(AvatarChooser.ava);
    }

    private void Update()
    {
        if (sp.IsOpen)
        {
            try
            {

                value = sp.ReadLine(); //Read the information
                button = int.Parse(value);

                if (button == 9) //*Input.GetKeyDown(KeyCode.Space*/)  //jump
                {
                    buttonPressed = true;

                   if (!timerStarted)
                    {

                        timerStarted = true;
                        timer.BeginTimer();
                    }
                }
                else
                {

                    timer.StopTimer();
                }
            }
            catch (System.Exception)
            {

            }
        }
    }


    private void FixedUpdate()
    {
        Character.position = new Vector3
                    (
                        Mathf.Clamp(GetComponent<Rigidbody2D>().position.x, xMin, xMax),
                        Mathf.Clamp(GetComponent<Rigidbody2D>().position.y, yMin, yMax)
                    );
    }

    void OnDisable()
    {
        sp.Close();
    }
}

This code works perfectly with the Arduino Input, yet it doesn't work with this Beetle board. I know the Beetle board is transmitting the data correctly as I checked the output on the Arduino Serial Monitor as well as the Teraterm application. Unity also doesn't give me any errors regarding this. I have no idea why this isn't working. Can unity accept input data from this device?

I am wondering if unity isn't accepting the input as it's from a BLE device. When I use a normal Arduino Uno as the input, the game does respond. So i know the problem isn't with the game or the Arduino code. How can i fix this issue? Please help

This is the Arduino code which is uploaded onto the Bluno Beetle:

#include <Bounce2.h>
#include <SoftwareSerial.h>
//#include <ArduinoBLE.h>
#include <Wire.h>
//SoftwareSerial bluetooth_port(10, 11);

// Connect each button with one connection
// to GND and the other to a digital pin.
const byte buttonPin1 = 2;
const byte buttonPin2 = 3;

class Button{

  private:

    byte m_buttonPin;
    byte m_counter = 0;
    unsigned long m_buttonPressTimeout;
    unsigned long m_previousMillis;

  public:

    Button(byte buttonPin):
      m_buttonPin(buttonPin),
      m_counter(0),
      m_buttonPressTimeout(1500), // Button press timeout in ms.
      m_previousMillis(0){}

    void Update(){
      if(m_counter > 0 && millis() - m_previousMillis >= m_buttonPressTimeout)
      {
        //Serial.print("Count from Update() just before it's reset to 0 = ");
        Serial.println(GetCounter());
        m_counter = 0;
      }
    }

    void IncrementCounter(){
      m_counter++;
      if(m_counter > 4){m_counter = 4;}
      if(m_counter == 1)
      {
        m_previousMillis = millis();
      }
    }

    friend void IncrementCounter(Button&);

    void IncrementCounter(Button&){
      IncrementCounter();
    }

    byte GetCounter(){
      return m_counter;
    }

};

Bounce buttonOneDebouncer = Bounce();
Bounce buttonTwoDebouncer = Bounce();
Button ButtonOne(buttonPin1);
Button ButtonTwo(buttonPin2);

void setup(){
  Serial.begin(9600);
  //Serial.begin(115200);
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  buttonOneDebouncer.attach(buttonPin1);
  buttonTwoDebouncer.attach(buttonPin2);
  buttonOneDebouncer.interval(25);
  buttonTwoDebouncer.interval(25);
}

void loop(){

  // Call the Update function as fast as possible.
  ButtonOne.Update();
  ButtonTwo.Update();

  // Button one pressed.
  if(buttonOneDebouncer.update()){
    if(buttonOneDebouncer.fell()){
      if(digitalRead(buttonPin2) == 0){
        ButtonOne.IncrementCounter();
      }
    }
  }

  // Button two pressed.
  if(buttonTwoDebouncer.update()){
    if(buttonTwoDebouncer.fell()){
      if(digitalRead(buttonPin1) == 0){
        ButtonOne.IncrementCounter(ButtonTwo);
      }
    }
  }
  if(digitalRead(buttonPin1) == 0 && (digitalRead(buttonPin2) == 0))
  {
  Serial.println("9");
  }

}

This is the data received from the Beetle to the Arduino Serial Monitor Serial Monitor

Any suggestions and tips or help will be very appreciated. Also any input on BLE devices for Unity will also be appreciated

\$\endgroup\$
  • \$\begingroup\$ @DMGregory what really confuses me is that if the input is basically coming from an Arduino compatible board and works in the same way as an Arduino board then why can't Unity just accept the input? \$\endgroup\$ – user19964 Jan 16 at 14:51
  • \$\begingroup\$ This likely is not a Unity issue. Unity should not be able to distinguish what device is communicating on the COM port. Is it possible the Beetle is mapped to a different COM port on your PC than the \\COM9 one you've configured your script to listen to? \$\endgroup\$ – DMGregory Jan 16 at 14:52
  • \$\begingroup\$ @DMGregory the arduino uno uses the COM 4 port on my laptop and the Beetle uses the COM 9 one. When I play the game, if the port isn't referenced correctly then Unity says that port doesn't exist. So I have checked that the ports are correctly referenced. \$\endgroup\$ – user19964 Jan 16 at 15:37
  • \$\begingroup\$ @DMGregory do you think that perhaps Unity isn't accepting the input as its a BLE device? I think I may need to buy an asset from the asset store to be able to use a BLE device as an input \$\endgroup\$ – user19964 Jan 21 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.