SerialPort sp;


// here we read the serial port from arduino board

int getInput() {

    if (arduino == true)

    {

        if (sp.IsOpen == false)

            sp.Open();

        {

            string myInputString;

            try

            {

                myInputString = sp.ReadLine();

                sp.Write("OK");      // confirmation

            }

            catch (System.TimeoutException ex)

            {

                // error

            }

        }

    }

    return 0;

}



void MoveObject(int Direction)

{

    if (Direction == 49)

    {

        if (steering < maxSteeringAngle)

            steering += 5;

    }


    else if (Direction == 50)

    {

        if (steering > -maxSteeringAngle)

            steering -= 5;

    }


    else if (Direction == 51)

    {

        if (motor < maxMotorTorque)

            motor += 100;

    }


    else if (Direction == 52)

    {

        if (motor > -maxMotorTorque)

            motor -= 100;

    }


    else if (Direction == 48)

    {

        motor = 0;

        steering = 0;

    }

    // go ahead with the car

    carControl(motor, steering);       

}



void FixedUpdate()

{

    getInput();

    if (start == true)

    {

        if (arduino == true)

        {

            if (sp.IsOpen == false)

                sp.Open();

            {

                int myByte;

                try

                {

                    myByte = sp.ReadByte();

                    MoveObject(myByte);

                    // confirmation

                    sp.Write(myByte.ToString());     

                }

                catch (System.TimeoutException ex)

                {

                    // error

                }

            }

        }

        else

        {

            motor = maxMotorTorque * Input.GetAxis("Vertical");

            steering = maxSteeringAngle * Input.GetAxis("Horizontal");

            carControl(motor, steering);

        }

    }

}



void Start()

{

    start = false;

    axleInfos = new AxleInfo[2];


    motor = 0.0f;

    steering = 0.0f;

    speed = 5.0f;


    // front axis

    WheelCollider wheelFL = GameObject.Find("physicsFL").GetComponentInChildren<WheelCollider>();

    WheelCollider wheelFR = GameObject.Find("physicsFR").GetComponentInChildren<WheelCollider>();

    bool front_motor = false;

    bool front_steering = true;

    axleInfos[0] = new AxleInfo(wheelFL, wheelFR, front_motor, front_steering);


    // rear axis

    WheelCollider wheelRL = GameObject.Find("physicsRL").GetComponentInChildren<WheelCollider>();

    WheelCollider wheelRR = GameObject.Find("physicsRR").GetComponentInChildren<WheelCollider>();

    axleInfos[1] = new AxleInfo(wheelRL, wheelRR);


    // check the control

    string myControl = ControlGroupScript.myControl;

    if (myControl.Equals("arduino"))

    {

        // car controlled by arduino board

        arduino = true;

        // change the COM port if needed (mmc devmgmt.msc)

        sp = new SerialPort("COM3", 19200, Parity.None, 8, StopBits.One);

        sp.Open();

        sp.ReadTimeout = 5;

    }

    else if (myControl.Equals("keyboard"))

    {

        // car controlled by keyboard

        arduino = false;

    }

}