Problems getting arduino to control character

Hello

I’m fairly new at this and is trying to make a simple race game along the line of Star Wars pod racing.

But I can’t get my arduino to work properly and I don’t know what’s wrong. I saw a video online of a guy, who made his arduino work with unity, so I sort of did what he did and just chaged some of the code.
My code is working if I use the “F” and “J” buttons but the arduino won’t.

This is the code I’ve got so far:

{
        if (Input.GetKey(KeyCode.J)  Input.GetKey(KeyCode.F))
        {
            float translation = Input.GetAxis("Vertical") * acc * curSpeed;
            translation *= Time.deltaTime;
            transform.Translate(0, 0, curSpeed * Time.deltaTime);
            curSpeed = Mathf.Clamp(curSpeed, maxSpeed, minSpeed);
            curSpeed = curSpeed + acc;
        }

        if (Input.GetKey(KeyCode.F)  !Input.GetKey(KeyCode.J))
        {
            transform.Rotate(0, rotaSpeed * Time.deltaTime, 0);
            float translation = Input.GetAxis("Vertical") * acc * curSpeed;
            translation *= Time.deltaTime;
            transform.Translate(0, 0, curSpeed * Time.deltaTime);
            curSpeed = Mathf.Clamp(curSpeed, maxSpeed, minSpeed);
        }

        if (Input.GetKey(KeyCode.J)  !Input.GetKey(KeyCode.F))
        {
            transform.Rotate(0, -rotaSpeed * Time.deltaTime, 0);
            float translation = Input.GetAxis("Vertical") * acc * curSpeed;
            translation *= Time.deltaTime;
            transform.Translate(0, 0, curSpeed * Time.deltaTime);
            curSpeed = Mathf.Clamp(curSpeed, maxSpeed, minSpeed);
        }
    }

    void moveObject(int direction)
    
    {
        if (direction == 1)
        {
            transform.Rotate(0, rotaSpeed * Time.deltaTime, 0);
            float translation = Input.GetAxis("Vertical") * acc * curSpeed;
            translation *= Time.deltaTime;
            transform.Translate(0, 0, curSpeed * Time.deltaTime);
            curSpeed = Mathf.Clamp(curSpeed, maxSpeed, minSpeed);
        }
        if (direction == 2)
        {
            transform.Rotate(0, -rotaSpeed * Time.deltaTime, 0);
            float translation = Input.GetAxis("Vertical") * acc * curSpeed;
            translation *= Time.deltaTime;
            transform.Translate(0, 0, curSpeed * Time.deltaTime);
            curSpeed = Mathf.Clamp(curSpeed, maxSpeed, minSpeed);
        }
        if (direction == 3)
        {
            float translation = Input.GetAxis("Vertical") * acc * curSpeed;
            translation *= Time.deltaTime;
            transform.Translate(0, 0, curSpeed * Time.deltaTime);
            curSpeed = Mathf.Clamp(curSpeed, maxSpeed, minSpeed);
            curSpeed = curSpeed + acc;
        }
    }
}

And this is my arduino code:

const int buttonPinLeft=8;
const int buttonPinRight=7;

void setup()
{
  Serial.begin(9600);
  
  pinMode(buttonPinLeft,INPUT);
  pinMode(buttonPinRight,INPUT);
  
  digitalWrite(buttonPinLeft,HIGH);
  digitalWrite(buttonPinRight,HIGH);
}

void loop()
{
  if(digitalRead(buttonPinLeft) == LOW)
  {
    Serial.write(2);
    Serial.flush();
    delay(20);
  }
  
  if(digitalRead(buttonPinRight) == LOW)
  {
    Serial.write(1);
    Serial.flush();
    delay(20);
  }
  
  if(digitalRead(buttonPinRight) == LOW  digitalRead(buttonPinLeft) == LOW)
  {
    Serial.write(3);
    Serial.flush();
    delay(20);
  }
}

how are you reading in the serial data in unity?

I would love to get arduino working with unity but I am not sure how to read in the serial data.

I found out! yaaay!

forgot to add:

if (sp.IsOpen)
        {
            try
            {
                moveObject(sp.ReadByte());
            }
            catch (System.Exception)
            {

            }
        }

I realised that I forgot to make unity read the arduino, thanks to your answer, so thank you good sir. :smile:

People are free to use what ever they want :smile:

Ahhh i guess you used IO ports.

It is a really cool thing to do especially if you can make a custom controller or something.

Yes and I think that arduino makes it fairly simple to make your own controller, especially for a novice like me :smile:

please, give me a tutorial about it.
i have the same problem… :frowning: please
video or ect

My problem was that I forgot to make my movement-script read the data sent by the arduino. I solved this by inserting this code into my movement-script:

    if (sp.IsOpen)
            {
                try
                {
                    moveObject(sp.ReadByte());
                }
                catch (System.Exception)
                {
     
                }
            }

My entire movement script ended up looking like this. Note: I used COM9:

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

public class PodRacerMove : MonoBehaviour
{
    public float maxSpeed;
    public float minSpeed;
    public float acc;
    public float deAcc;
    public float rotaSpeed;
    public float Speed;
    public float curSpeed;
  
    SerialPort sp = new SerialPort("COM9", 9600);

    void Start()
    {
        sp.Open();
        sp.ReadTimeout = 1;
    }

    void Update()
    {
        if (sp.IsOpen)
        {
            try
            {
       
            }
            catch (System.Exception)
            {
                
            }
        }
    }

    void moveObject(int direction)
    {
        transform.Translate(0, 0, curSpeed);

        if (direction == 3)
        {
            curSpeed += acc;
            if (curSpeed >= maxSpeed)
            {
                curSpeed = maxSpeed;
            }
        }

        else if (direction == 2)
        {
            transform.Rotate(0, rotaSpeed * Time.deltaTime, 0);
        }

        else if (direction == 1)
        {
            transform.Rotate(0, -rotaSpeed * Time.deltaTime, 0);
        }

        else if (direction == 4)
        {
            curSpeed -= deAcc;
            if (curSpeed <= minSpeed)
            {
                curSpeed = minSpeed;
            }
        }
    }
}

And my arduino code looks like this:

    const int buttonPinLeft=8;
    const int buttonPinRight=7;
     
    void setup()
    {
      Serial.begin(9600);
     
      pinMode(buttonPinLeft,INPUT);
      pinMode(buttonPinRight,INPUT);
     
      digitalWrite(buttonPinLeft,HIGH);
      digitalWrite(buttonPinRight,HIGH);
    }
     
    void loop()
    {
      if(digitalRead(buttonPinLeft) == LOW)
      {
        Serial.write(2);
        Serial.flush();
        delay(20);
      }
     
      if(digitalRead(buttonPinRight) == LOW)
      {
        Serial.write(1);
        Serial.flush();
        delay(20);
      }
     
      if(digitalRead(buttonPinRight) == LOW  digitalRead(buttonPinLeft) == LOW)
      {
        Serial.write(3);
        Serial.flush();
        delay(20);
      }

      if(digitalRead(buttonPinRight) == HIGH  digitalRead(butonPinLeft) == HIGH)
      {
        Serial.write(4);
        Serial.flush();
        delay(20);
       }
    }

This way I define, when both buttons are HIGH (not pressed) my curSpeed is decreased by my deAcc. Soon after I found this out I changed my movement to addForce instead of transform.translate, but then my time ran out and I had to hand in my assignment. I hope this helped and you are free to borrow my scripts and change them how you see fit. :smile: please write if you need more help.
People are free to contact me on my Skype, if they want, just pm me and I’ll send it to you. Then we can maybe help each other with scripting and arduino :slight_smile:

Thanks for posting the code.

Why do you need to flush the serial port every time?

I tended to use a code system when working with processing (first number is type second number is value since I am reading off sensor data too)