Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question FSR sensor integration using Arduino with unity 3d

Discussion in 'Code Editors & IDEs' started by engineerkhan, Apr 3, 2022.

  1. engineerkhan

    engineerkhan

    Joined:
    Dec 2, 2021
    Posts:
    1
    Hy, I am using FSR sensor but facing error in unity "Input string is not in the correct format" Please review my code and let me know about the error in code. I want Integration of FSR sensor with unity 3d.
    Thanks in advance

    Arduino Code:
    int fsrPin = 0; // the FSR and 10K pulldown are connected to a0
    int fsrReading; // the analog reading from the FSR resistor divider

    void setup(void) {
    Serial.begin(9600);
    }
    void loop(void) {
    fsrReading = analogRead(A0);

    Serial.print("Analog reading = ");
    Serial.print(fsrReading); // print the raw analog reading

    if (fsrReading < 1) {
    Serial.println(" -no pressure");
    } else if (fsrReading >3) {
    Serial.println(" - Light touch");
    } else if (fsrReading > 500) {
    Serial.println(" - Light squeeze");
    }
    else if (fsrReading > 800) {
    Serial.println(" - Medium squeeze");
    } else {
    Serial.println(" - Big squeeze");
    }
    delay(100);
    }

    Unity 3d Code:

    using System.Collections;
    using System.Collections.Generic;
    using System.IO.Ports;
    using UnityEngine;
    public class vw : MonoBehaviour
    {
    SerialPort Stream = new SerialPort("COM3",9600);
    int fsrReading;
    // Start is called before the first frame update
    void Start()
    {
    Stream.Open();
    }

    // Update is called once per frame
    void Update()
    {
    string h = Stream.ReadLine();
    fsrReading = int.Parse(h);

    }
    }