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 /
This question was closed Jan 03, 2016 at 05:33 PM by KnightRiderGuy for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by KnightRiderGuy · Jan 02, 2016 at 07:28 PM · c#valuesarduinoserialportserial.io.port

Read in 2 Different Arduino Values

OK don't know how much more simpler to put that. Arduino is sending out two values the "T" and the "L" one is for temperature and the other for Light value. now how the hell do I read those into Unity as separate values I can work with for the love of god come on. Arduino Serial Monitor Screen Grab

screen-shot-2016-01-02-at-12353-pm.png (83.3 kB)
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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by RogueCode · Jan 02, 2016 at 09:04 PM

If I'm understanding your question right, then all you need to do is:

     void MessageReceived(string message)
     {
       var type = message[0];
       var value = float.Parse(message.Substring(1));
       switch (type)
       {
         case 'T':
           HandleTemp(value);
           break;
         case 'L':
           HandleLight(value);
           break;
         default:
           print("Invalid");
           break;
       }
     }

If it all comes as one connected string then you'll need to build up a buffer and split every time you hit a \r\n (assuming those are what terminated your lines).

Comment
Add comment · Show 5 · 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 KnightRiderGuy · Jan 02, 2016 at 10:53 PM 0
Share

Thanks @RogueCode, Finally someone willing to help out. I'm using C# though, but this looks interesting. I was doing some experimenting and found that my Arduino Data does seem to be co$$anonymous$$g in as two separate strings:

Inspector Screen Grab

$$anonymous$$y Code is one mess though, it;s hard trying to figure out what I need to change.?

 // Update is called once per frame
 void Update () {
     try
     {
         /*string message3 = sp.ReadLine(); //get the message...
         if(message3 == "") return; //if its empty stop right here
         print(message3);
         DirectionArrow (message3.Trim());*/
     }
     catch (System.Exception ex) {
         //print (ex.$$anonymous$$essage);
         return;
     }

     //NOT I comented out above code just to experiment and verify that the code below (Temperature)
     //Was going to do what I suspected it would do on the UI Text.

     //HOW DO I GET THE TE$$anonymous$$PERATURE DATA FRO$$anonymous$$ THE SERIAL AND DISPLAY IT ON THIS UI TEXT?
     //Without is conflicting with the LDR data
     //Also HOW the heck does UNITY know to read out the LDR information as $$anonymous$$essage2?
     //when message3 (Buttons inputs) is working but not displaying anything in the inspector?
     TempSensorData = sp.ReadLine();
     if (TempSensorData == "")
         return;
     TemperatureText.text = TempSensorData.ToString ();



         //print("BytesToRead" +sp.BytesToRead);
         message2 = sp.ReadLine();
         
     //string message = sp.ReadLine(); //get the message...
     if(message2 == "") return; //if its empty stop right here

     // parse the input to a float and normalize it (range 0..1) (we could do this already in the Arduino)
     float input =  1 -  float.Parse (message2) / 100f;
     // set the slider to the value
     float oldValue = slider.value;
     slider.value = input;

     // after the slider is updated, we can check for the other things for example play sounds:
     if (source.isPlaying) return; // if we are playing a sound stop here

     // else check if we need to play a sound and do it
     if (slider.value > 0.9f && oldValue <= 0.9f) // ---------this has changed
         source.PlayOneShot (BrightnessAudioClips [Random.Range (0, BrightnessAudioClips.Length)]);

     else if (slider.value < 0.15f && oldValue >= 0.15f) //----------this has changed
         source.PlayOneShot (DarknessAudioClips [Random.Range (0, DarknessAudioClips.Length)]);
     

 }
     


 


[2]: /storage/temp/61127-sending.txt

sending.txt (9.7 kB)
screen-shot-2016-01-02-at-43943-pm.png (51.9 kB)
avatar image RogueCode · Jan 03, 2016 at 05:42 AM 0
Share

I'm not convinced that the code you posted actually works as you'd expect, since it appears it will be effected by the order that messages come in.

I'd delete all that update code and change to something like:

     void Update()
     {
       var read = sp.ReadLine();
       if (read == "")
         return;
 
       $$anonymous$$essageReceived(read);
     }
 
     void $$anonymous$$essageReceived(string message)
     {
       var type = message[0];
       var value = float.Parse(message.Substring(1));
       switch (type)
       {
         case 'T':
           HandleTemp(value);
           break;
         case 'L':
           HandleLight(value);
           break;
         default:
           print("Invalid");
           break;
       }
     }

In the HandleLight and HandleTemp methods you could write to the UI or whatever. You should add error handling to the float.Parse, else it will break whenever your data is malformed.

avatar image KnightRiderGuy RogueCode · Jan 03, 2016 at 01:44 PM 0
Share

@RogueCode, Thanks, I'm not sure I fully follow you I have my variable at the top of the script like this:

 public string TempSensorData;
     public string LDRdata;
     public string ButtonsData;


And I tried to do something like this for experimentation: void Update() { var read = sp.ReadLine(); if (read == "") return;

         $$anonymous$$essageReceived(read);
     }
 
     void $$anonymous$$essageReceived(string message)
     {
         var type = message[0];
         var value = float.Parse(message.Substring(1));
         switch (type)
         {
         case 'T':
             //HandleTemp(value);
             if (TempSensorData == "")
                 return;
             TemperatureText.text = TempSensorData.ToString ();
             break;
         case 'L':
             //HandleLight(value);
             break;
         default:
             print("Invalid");
             break;
         }
     }

I'm not the greatest at code so this I could use explained in a little more depth.

avatar image KnightRiderGuy RogueCode · Jan 03, 2016 at 02:38 PM 0
Share

@RogueCode, I forgot to ask about how I would also implement by buttons into this new method you have here?

 ButtonsData = sp.ReadLine(); //get the message...
             if(ButtonsData == "") return; //if its empty stop right here
             print(ButtonsData);
             DirectionArrow (ButtonsData.Trim());
avatar image KnightRiderGuy RogueCode · Jan 03, 2016 at 05:32 PM 0
Share

O$$anonymous$$ with a few extra tweaks from ShapeShifter from the Arduino forums this was essentially the correct answer. This is what I ended up needing to do.

 void Update()
     {
         string read = sp.ReadLine();
         if (read == "")
             return;
 
         $$anonymous$$essageReceived(read);
     }
 
     void $$anonymous$$essageReceived(string message)
     {
         char type = message[0];
         string value = message.Substring(1);
 
         switch (type)
         {
         case 'T':
             //temperature sensor reading
             //TemperatureText = value;
             //TempSensorData = value;
             //TemperatureText.text = TempSensorData.ToString ();
             TemperatureText.text = value;
             break;
 
         case 'L':
             //light sensor reading
             LDRdata = value;
             if(LDRdata == "") return; //if its empty stop right here
 
             // parse the input to a float and normalize it (range 0..1) (we could do this already in the Arduino)
             float input =  1 -  float.Parse (LDRdata) / 100f;
             // set the slider to the value
             float oldValue = slider.value;
             slider.value = input;
 
             // after the slider is updated, we can check for the other things for example play sounds:
             if (source.isPlaying) return; // if we are playing a sound stop here
 
             // else check if we need to play a sound and do it
             if (slider.value > 0.9f && oldValue <= 0.9f) // ---------this has changed
                 source.PlayOneShot (BrightnessAudioClips [Random.Range (0, BrightnessAudioClips.Length)]);
 
             else if (slider.value < 0.15f && oldValue >= 0.15f) //----------this has changed
                 source.PlayOneShot (DarknessAudioClips [Random.Range (0, DarknessAudioClips.Length)]);
             break;
 
         case 'O':
             //deactivate Left Direction Arrow
             // your existing code from your DirectionArrow() function goes here...
             //deactivate Left Direction Arrow
             SystemGuidance$$anonymous$$anagerScript WAOff = FindObjectOfType<SystemGuidance$$anonymous$$anagerScript>();
             WAOff.WestArrowOff ();
 
             SystemGuidance$$anonymous$$anagerScript EAOff = FindObjectOfType<SystemGuidance$$anonymous$$anagerScript>();
             EAOff.EastArrowOff();
 
             SystemGuidance$$anonymous$$anagerScript NAOff = FindObjectOfType<SystemGuidance$$anonymous$$anagerScript>();
             NAOff.NorthArrowOff();
 
             SystemGuidance$$anonymous$$anagerScript SAOff = FindObjectOfType<SystemGuidance$$anonymous$$anagerScript>();
             SAOff.SouthArrowOff();
 
             SystemGuidance$$anonymous$$anagerScript SFXOff = FindObjectOfType<SystemGuidance$$anonymous$$anagerScript>();
             SFXOff.TopIndicatorOff();
 
             SystemGuidance$$anonymous$$anagerScript BGSOff = FindObjectOfType<SystemGuidance$$anonymous$$anagerScript>();
             BGSOff.BarLEDsSounderOff();
 
             $$anonymous$$essageCentre$$anonymous$$anager2 $$anonymous$$C$$anonymous$$2D$$anonymous$$ = FindObjectOfType<$$anonymous$$essageCentre$$anonymous$$anager2> ();
             $$anonymous$$C$$anonymous$$2D$$anonymous$$.GoDefault$$anonymous$$essage ();
 
             $$anonymous$$essageCentre$$anonymous$$anager $$anonymous$$C$$anonymous$$D$$anonymous$$ = FindObjectOfType<$$anonymous$$essageCentre$$anonymous$$anager> ();
             $$anonymous$$C$$anonymous$$D$$anonymous$$.Go$$anonymous$$nightIndustries$$anonymous$$essage ();
             break;


I didn't include all the switch statements for the button calls as they are pretty much just copy and paste of my original switch statement button calls.

Follow this Question

Answers Answers and Comments

52 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 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

Get UI Slider Value 3 Answers

Problems With Sending Data through Serial Port 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Need Help Connecting Arduino to Unity 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