My Arduino code is kind of set up as a series of toggle on off states that are being sent from Unity to the Arduino through the serial port as characters.
How can I send information from the Arduino with my current Arduino code so that In Unity 3D I can check the status of the pin or LED that is on the Arduino pin.
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)) > 100 || (lightInt - analogRead(lightPin)) < -100){
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
}
}
}