Arduino to Unity
hi, I am a beginner, I want to test use fsr sensor which connect to arduino to control a cube in one direction of Unity. This is my code, it doesn't work, could someone help me? I do not know which part is wrong.
Unity code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
public class movecube : MonoBehaviour {
private GameObject cube;
public float rate;
SerialPort serial=new SerialPort("COM5",9600);
void Start(){
serial.Open ();
serial.ReadTimeout = 2000;
}
void Update () {
if (!serial.IsOpen)
serial.Open ();
float speed=int.Parse(serial.ReadLine());
rate = speed / 10000;
float translation=Input.GetAxis("Vertical")*rate;
translation*=Time.deltaTime;
transform.Translate(0, 0, translation);
}
}
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() {
Serial.begin(9600);
}
void loop(void) {
fsrReading = analogRead(fsrPin);
Serial.print("Analog reading = ");
Serial.print(fsrReading); // the raw analog reading
Serial.println(fsrReading);
delay(1000);
Comment
Your answer
Follow this Question
Related Questions
My editor is very slow and choppy but in play mode the fps is normal 0 Answers
Unity (default) Fonts going pink in build (pink squares!!!!) 0 Answers
OnMouseDown not working! 0 Answers
Imported FBX has transform of (0,0,0), but actual position is NOT at (0,0,0) 1 Answer
Why pressing the up and down key doesn't mean the body in correct forward and back direction? 1 Answer