Need Help With Arduino Code to Unity
I'm having some issues understanding what I need to do in order to make my Arduino code send information to Unity for me to be able to change the Unity UI based on the status of an Arduino pin or LED on or off status.
I'm also wondering why on start up pin 13 flashes? Is there a way to disable that on start up flashing as any device I have hooked up to it will flash when activated. And well lol If I have K.I.T.T.s Oil Slick function connected to that pin then every time I start the system he'll spooge oil all over the garage floor, and well... we can't have that now can we ;) lol
Here is my Arduino code that I am working with.
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
}
}
}
Comment
Not sure how that helps as I don't know what i'm supposed to be looking at?