Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by KnightRiderGuy · Sep 06, 2015 at 08:13 PM · c#buttoncodepagearduino

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
     }
   }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need Help With Arduino Code to Unity 0 Answers

Help with coding crane 0 Answers

Send input to Arduino from Unity Android App 0 Answers

Combine If Statement Conditions 1 Answer

Assigning a GameObject variable to equal another GameObject variable via C# script. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges