Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity To Check LED Status on The Arduino

Discussion in 'Scripting' started by KnightRiderGuy, Nov 27, 2015.

  1. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    How can I get Unity to check the status of an LED on the Arduino, by this I mean say that if the light is on or off to be able to send information back to Unity so that it can update the UI.

    Here is my Arduino code.

    Code (CSharp):
    1. int lightPin = 0; //Define Pin For Photoresistor
    2. int lightInt = 0;
    3. //int lightLevel = analogRead(0);
    4. //int threshold = 250;
    5. //int range = 50;
    6.  
    7. const byte rLed = 12; //Sets Pin number LED is conneted too
    8. const byte rLed2 = 1;
    9. const byte yLed = 11;
    10. const byte gLed = 10;
    11. const byte gLed2 = 5;
    12. const byte gLed3 = 4;
    13. const byte wLed = 3;
    14. const byte bLed = 2;
    15. const byte bLed2 = 13;
    16. char myChar;         //changed the name of this variable because they are not all colurs now
    17. const byte pulsePins[] = {6, 7, 8, 9};  //pins for a pulse output
    18. char pulseTriggers[] = {'p', 'q','R','L'};
    19. const int NUMBER_OF_PULSE_PINS = sizeof(pulsePins);
    20. unsigned long pulseStarts[NUMBER_OF_PULSE_PINS];
    21. unsigned long pulseLength = 500;
    22.  
    23. void setup()
    24. {
    25.   //Serial.begin (9600);
    26.   Serial.begin (115200);
    27.   Serial.setTimeout(13); //Added today Sun Nov 22 ( not sure if this is needed? )
    28.   pinMode(wLed, OUTPUT);
    29.   pinMode(rLed, OUTPUT);
    30.   pinMode(rLed2, OUTPUT);
    31.   pinMode(yLed, OUTPUT);
    32.   pinMode(gLed, OUTPUT);
    33.   pinMode(gLed2, OUTPUT);
    34.   pinMode(gLed3, OUTPUT);
    35.   pinMode(bLed, OUTPUT);
    36.   pinMode(bLed2, OUTPUT);
    37.   digitalWrite(wLed, LOW);
    38.   digitalWrite(rLed, LOW);
    39.   digitalWrite(rLed2, LOW);
    40.   digitalWrite(yLed, LOW);
    41.   digitalWrite(gLed, LOW);
    42.   digitalWrite(gLed2, LOW);
    43.   digitalWrite(gLed3, LOW);
    44.   digitalWrite(bLed, LOW);
    45.   digitalWrite(bLed2, LOW);
    46.  
    47.   for (int p = 0; p < NUMBER_OF_PULSE_PINS; p++)
    48.   {
    49.     pinMode(pulsePins[p], OUTPUT);
    50.     digitalWrite(pulsePins[p], LOW);
    51.   }
    52. }
    53.  
    54. void loop()
    55. {
    56.   //Light Sensor
    57.     if((lightInt - analogRead(lightPin)) > 500 || (lightInt - analogRead(lightPin)) < -500){
    58.       lightInt = analogRead(lightPin);
    59.       Serial.println(lightInt);
    60.     }
    61.     //read the current light level.
    62.     /*int lightLevel = analogRead(lightPin);
    63.     //If the light level is within our desired range, send it to Unity.
    64.     if(lightLevel > threshold - range && lightLevel < threshold + range)
    65.     Serial.println(lightLevel);
    66.     */
    67.   if (Serial.available())              //if serial data is available
    68.   {
    69.    
    70.     int lf = 10;
    71.  
    72.     myChar = Serial.read();             //read one character from serial
    73.     if (myChar == 'r')                  //if it is an r
    74.     {
    75.       digitalWrite(rLed, !digitalRead(rLed));  //Oil Slick Toggle
    76.     }
    77.     if (myChar == 'b')
    78.     {
    79.       digitalWrite(bLed, !digitalRead(bLed)); //Surveillance Mode Toggle
    80.     }
    81.  
    82.     if (myChar == 'y')
    83.     {
    84.       digitalWrite(yLed, !digitalRead(yLed)); //Movie Player Toggle
    85.     }
    86.  
    87.     if (myChar == 'g')
    88.     {
    89.       digitalWrite(gLed, !digitalRead(gLed)); //Auto Phone Toggle
    90.     }
    91.  
    92.     if (myChar == '1')
    93.     {
    94.       digitalWrite(bLed2, !digitalRead(bLed2)); //Scanner Toggle
    95.     }
    96.  
    97.     if (myChar == 'f')
    98.     {
    99.       digitalWrite(gLed2, !digitalRead(gLed2)); //Fog Lights Toggle
    100.     }
    101.  
    102.     if (myChar == 'h')
    103.     {
    104.       digitalWrite(gLed3, !digitalRead(gLed3)); //Head Lights Toggle
    105.     }
    106.  
    107.     if (myChar == 'H')
    108.     {
    109.       digitalWrite(wLed, !digitalRead(wLed)); //High Beams Toggle
    110.     }
    111.  
    112.  
    113.     //Rear Hatch Popper
    114.     for (int p = 0; p < NUMBER_OF_PULSE_PINS; p++)
    115.     {
    116.       if (myChar == pulseTriggers[p])
    117.       {
    118.         pulseStarts[p] = millis();  //save the time of receipt
    119.         digitalWrite(pulsePins[p], HIGH);
    120.       }
    121.     }
    122.  
    123.  
    124.     //Grappling Hook Launch
    125.     for (int q = 0; q < NUMBER_OF_PULSE_PINS; q++)
    126.     {
    127.       if (myChar == pulseTriggers[q])
    128.       {
    129.         pulseStarts[q] = millis();  //save the time of receipt
    130.         digitalWrite(pulsePins[q], HIGH);
    131.       }
    132.     }
    133.  
    134.  
    135.     //Auto Doors Right Pulse
    136.     for (int R = 0; R < NUMBER_OF_PULSE_PINS; R++)
    137.     {
    138.       if (myChar == pulseTriggers[R])
    139.       {
    140.         pulseStarts[R] = millis();  //save the time of receipt
    141.         digitalWrite(pulsePins[R], HIGH);
    142.       }
    143.     }
    144.  
    145.  
    146.     //Auto Doors Left Pulse
    147.     for (int L = 0; L < NUMBER_OF_PULSE_PINS; L++)
    148.     {
    149.       if (myChar == pulseTriggers[L])
    150.       {
    151.         pulseStarts[L] = millis();  //save the time of receipt
    152.         digitalWrite(pulsePins[L], HIGH);
    153.       }
    154.     }
    155.   }
    156.  
    157.   //the following code runs each time through loop()
    158.   for (int p = 0; p < NUMBER_OF_PULSE_PINS; p++)
    159.   {
    160.     if (millis() - pulseStarts[p] >= pulseLength)  //has the pin been HIGH long enough ?
    161.     {
    162.       digitalWrite(pulsePins[p], LOW);   //take the pulse pin LOW
    163.     }
    164.   }
    165.  
    166.   for (int q = 0; q < NUMBER_OF_PULSE_PINS; q++)
    167.   {
    168.     if (millis() - pulseStarts[q] >= pulseLength)  //has the pin been HIGH long enough ?
    169.     {
    170.       digitalWrite(pulsePins[q], LOW);   //take the pulse pin LOW
    171.     }
    172.   }
    173. }