-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathofflineReadWriteArduino.cs
More file actions
52 lines (43 loc) · 1.04 KB
/
offlineReadWriteArduino.cs
File metadata and controls
52 lines (43 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using UnityEngine;
using System;
using System.Collections;
using System.IO.Ports;
public class offlineReadWriteArduino :MonoBehaviour {
SerialPort stream = new SerialPort("/dev/cu.usbmodem1421", 57600);
bool flip=false;
string hold;
// Use this for initialization
void Start () {
stream.Open ();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown ("q")) {
if (flip == false)
WriteToArduino ("ledOn ");
else
WriteToArduino ("ledOff ");
flip = !flip;
}
hold = ReadFromArduino (1);
if (hold != null&&hold.ToCharArray().Length>0) {
if (hold.ToCharArray () [0] == '1') {
gameObject.transform.localScale = new Vector3 (2, 2, 2);
}
if(hold.ToCharArray () [0] == '2')
gameObject.transform.localScale = new Vector3 (1, 1, 1);
}
}
public string ReadFromArduino (int timeout) {
stream.ReadTimeout = timeout;
try {
return stream.ReadLine();
}
catch (System.Exception) {
return null;
}
}
public void WriteToArduino(string message) {
stream.Write(message);
}
}