Last active
June 19, 2019 07:58
-
-
Save hhayley/5a79c8c465efb5af5bfef66c0c35b6e1 to your computer and use it in GitHub Desktop.
ITP CAMP 2019 - Arduino Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Unity <---> Arduino (Serial Communication) | |
- Send Multiple sensor values to Unity | |
Tested Unity version 2019.1.6f | |
For 2019 ITP CAMP Workshop "Make your own controller in Unity" | |
By Hayeon Hwang. 2019.06.13 | |
*/ | |
const int ledPin = LED_BUILTIN; | |
const int potPin = A0; // Analog input pin that the potentiometer is attached to | |
const int windSensorPin = A1; // Analog input pin that the potentiometer is attached to | |
int sensorValue = 0; | |
int lastSensorValue = 0; | |
int windValue = 0; | |
int lastWindValue = 0; | |
int ledState = 0; | |
int counter = 0; // ref - 'StateChangeDetection' example | |
void setup() { | |
Serial.begin(9600); // ***important to match with Unity | |
pinMode(LED_BUILTIN, OUTPUT); | |
} | |
void loop() { | |
// ARDUINO ---> UNITY | |
sensorValue = analogRead(potPin); | |
windValue = analogRead(windSensorPin); | |
if (sensorValue != lastSensorValue || windValue != lastWindValue) { | |
Serial.print(sensorValue); | |
Serial.print(','); | |
Serial.println(windValue); | |
delay(20); | |
} | |
lastSensorValue = sensorValue; | |
lastWindValue = windValue; | |
// UNITY ---> ARDUINO | |
readDatafromUnity(); | |
} | |
void readDatafromUnity() { | |
ledState = recvSerial(); | |
// Make a toggle switch style | |
if (ledState == HIGH) { | |
counter++; | |
} | |
if (counter % 2 == 0) { | |
digitalWrite(LED_BUILTIN, LOW); | |
} else { | |
digitalWrite(LED_BUILTIN, HIGH); | |
} | |
} | |
int recvSerial() { | |
if (Serial.available()) { | |
int serialData = Serial.read(); | |
switch (serialData) { | |
case '1': | |
return 1; | |
break; | |
case '0': | |
return 0; | |
break; | |
default: | |
return -1; | |
} | |
} | |
delay(20); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Unity <---> Arduino (Serial Communication) | |
- Send a single sensor value to Unity | |
Tested Unity version 2019.1.6f | |
For 2019 ITP CAMP Workshop "Make your own controller in Unity" | |
By Hayeon Hwang. 2019.06.13 | |
*/ | |
const int ledPin = LED_BUILTIN; | |
const int potPin = A0; // Analog input pin that the potentiometer is attached to | |
int sensorValue = 0; | |
int lastSensorValue = 0; | |
int ledState = 0; | |
int counter = 0; // ref - 'StateChangeDetection' example | |
void setup() { | |
Serial.begin(9600); // ***important to match with Unity | |
pinMode(LED_BUILTIN, OUTPUT); | |
} | |
void loop() { | |
// ARDUINO ---> UNITY | |
sensorValue = analogRead(potPin); | |
if (sensorValue != lastSensorValue) { | |
Serial.println(sensorValue); | |
delay(20); | |
} | |
lastSensorValue = sensorValue; | |
// UNITY ---> ARDUINO | |
readDatafromUnity(); | |
} | |
void readDatafromUnity() { | |
ledState = recvSerial(); | |
// Make a toggle switch style | |
if (ledState == HIGH) { | |
counter++; | |
} | |
if (counter % 2 == 0) { | |
digitalWrite(LED_BUILTIN, LOW); | |
} else { | |
digitalWrite(LED_BUILTIN, HIGH); | |
} | |
} | |
int recvSerial() { | |
if (Serial.available()) { | |
int serialData = Serial.read(); | |
switch (serialData) { | |
case '1': | |
return 1; | |
break; | |
case '0': | |
return 0; | |
break; | |
default: | |
return -1; | |
} | |
} | |
delay(20); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment