Unity 3D with Arduino Code Question
I have begun doing some experiments and playing around with an Arduino uno and my Unity 3D interface. I was following the tutorial here: Communicating From Unity to Arduino
I am on a Mac and I have gotten this tutorial to work although it took me a while to figure out the port communication stuff.
In the tutorial he has his code where when you click on one of the cubes the LED comes on and when you click on another the previous LED turns off and the new corresponding LED turns on, very cool but not quite what I am after. What I want to do is have more a toggle on off with the same button, but being new to the Arduino Unity communication I am not sure what to change in the code to do this?
Here is a link to my demo video of the project so far: Demo Video of my project
If anyone is familiar with Arduino and unity who know what I am looking to do and can help that would be awesome, the guy on R3Dstar has put up a sample project and the codes so they are easy to find.
This is my Arduino code that I need to be adjusted to be able for unity to Read back the status of the LED on the Arduino:
int lightPin = 0; //Define Pin For Photoresistor
int lightInt = 0;
//int lightLevel = analogRead(0);
//int threshold = 250;
//int range = 50;
const byte rLed = 12; //Sets Pin number LED is conneted too
const byte rLed2 = 1;
const byte yLed = 11;
const byte gLed = 10;
const byte gLed2 = 5;
const byte gLed3 = 4;
const byte wLed = 3;
const byte bLed = 2;
const byte bLed2 = 13;
char myChar; //changed the name of this variable because they are not all colurs now
const byte pulsePins[] = {6, 7, 8, 9}; //pins for a pulse output
char pulseTriggers[] = {'p', 'q','R','L'};
const int NUMBER_OF_PULSE_PINS = sizeof(pulsePins);
unsigned long pulseStarts[NUMBER_OF_PULSE_PINS];
unsigned long pulseLength = 500;
void setup()
{
//Serial.begin (9600);
Serial.begin (115200);
Serial.setTimeout(13); //Added today Sun Nov 22 ( not sure if this is needed? )
pinMode(wLed, OUTPUT);
pinMode(rLed, OUTPUT);
pinMode(rLed2, OUTPUT);
pinMode(yLed, OUTPUT);
pinMode(gLed, OUTPUT);
pinMode(gLed2, OUTPUT);
pinMode(gLed3, OUTPUT);
pinMode(bLed, OUTPUT);
pinMode(bLed2, OUTPUT);
digitalWrite(wLed, LOW);
digitalWrite(rLed, LOW);
digitalWrite(rLed2, LOW);
digitalWrite(yLed, LOW);
digitalWrite(gLed, LOW);
digitalWrite(gLed2, LOW);
digitalWrite(gLed3, LOW);
digitalWrite(bLed, LOW);
digitalWrite(bLed2, LOW);
for (int p = 0; p < NUMBER_OF_PULSE_PINS; p++)
{
pinMode(pulsePins[p], OUTPUT);
digitalWrite(pulsePins[p], LOW);
}
}
void loop()
{
//Light Sensor
if((lightInt - analogRead(lightPin)) > 500 || (lightInt - analogRead(lightPin)) < -500){
lightInt = analogRead(lightPin);
Serial.println(lightInt);
}
//read the current light level.
/*int lightLevel = analogRead(lightPin);
//If the light level is within our desired range, send it to Unity.
if(lightLevel > threshold - range && lightLevel < threshold + range)
Serial.println(lightLevel);
*/
if (Serial.available()) //if serial data is available
{
int lf = 10;
myChar = Serial.read(); //read one character from serial
if (myChar == 'r') //if it is an r
{
digitalWrite(rLed, !digitalRead(rLed)); //Oil Slick Toggle
}
if (myChar == 'b')
{
digitalWrite(bLed, !digitalRead(bLed)); //Surveillance Mode Toggle
}
if (myChar == 'y')
{
digitalWrite(yLed, !digitalRead(yLed)); //Movie Player Toggle
}
if (myChar == 'g')
{
digitalWrite(gLed, !digitalRead(gLed)); //Auto Phone Toggle
}
if (myChar == '1')
{
digitalWrite(bLed2, !digitalRead(bLed2)); //Scanner Toggle
}
if (myChar == 'f')
{
digitalWrite(gLed2, !digitalRead(gLed2)); //Fog Lights Toggle
}
if (myChar == 'h')
{
digitalWrite(gLed3, !digitalRead(gLed3)); //Head Lights Toggle
}
if (myChar == 'H')
{
digitalWrite(wLed, !digitalRead(wLed)); //High Beams Toggle
}
//Rear Hatch Popper
for (int p = 0; p < NUMBER_OF_PULSE_PINS; p++)
{
if (myChar == pulseTriggers[p])
{
pulseStarts[p] = millis(); //save the time of receipt
digitalWrite(pulsePins[p], HIGH);
}
}
//Grappling Hook Launch
for (int q = 0; q < NUMBER_OF_PULSE_PINS; q++)
{
if (myChar == pulseTriggers[q])
{
pulseStarts[q] = millis(); //save the time of receipt
digitalWrite(pulsePins[q], HIGH);
}
}
//Auto Doors Right Pulse
for (int R = 0; R < NUMBER_OF_PULSE_PINS; R++)
{
if (myChar == pulseTriggers[R])
{
pulseStarts[R] = millis(); //save the time of receipt
digitalWrite(pulsePins[R], HIGH);
}
}
//Auto Doors Left Pulse
for (int L = 0; L < NUMBER_OF_PULSE_PINS; L++)
{
if (myChar == pulseTriggers[L])
{
pulseStarts[L] = millis(); //save the time of receipt
digitalWrite(pulsePins[L], HIGH);
}
}
}
//the following code runs each time through loop()
for (int p = 0; p < NUMBER_OF_PULSE_PINS; p++)
{
if (millis() - pulseStarts[p] >= pulseLength) //has the pin been HIGH long enough ?
{
digitalWrite(pulsePins[p], LOW); //take the pulse pin LOW
}
}
for (int q = 0; q < NUMBER_OF_PULSE_PINS; q++)
{
if (millis() - pulseStarts[q] >= pulseLength) //has the pin been HIGH long enough ?
{
digitalWrite(pulsePins[q], LOW); //take the pulse pin LOW
}
}
}
Answer by Adamcbrz · Sep 08, 2015 at 01:52 PM
So a simple way of doing this is change is if statement in the RYGFromUnity.ino to:
if(strcmp(myCol,"r:0")==0)
//turn red light off
if(strcmp(myCol,"r:1")==0)
//turn red light on
Then in unity you will need to pass the that string based on the state of the button
public static void sendRed(bool active){
sp.Write("r:" + ((active)?1,0));
}
I was pretty vague, assuming if you got through that tutorial you can fill in the gaps when needed.
Answer by KnightRiderGuy · Nov 28, 2015 at 12:00 AM
How would I do this if my Arduino Code is configured like this: (have Unity check the LED status)
if (myChar == 'H')
{
digitalWrite(wLed, !digitalRead(wLed)); //High Beams Toggle
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612015142im_/https://answers.unity.com/themes/thub/images/avi.jpg)