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 · Nov 25, 2015 at 01:02 PM · lightingdataarduinosensorsending

Not Fully Understanding how To Correctly Receive LDR Arduino Data

I'm not sure I'm using this correctly for what I want which is for the audio clips to be activated when the light hits a value close to dusk or sunset on the Arduino LDR sensor. This top Code is the Arduino Code side and the bottom one is the Unity3D Code.

 return;
         print (message);
         float fl = float.Parse (message) / 100.0f;
         fl = 1.0f - fl;


This is how I'm sending the information to Unity From Arduino:

 //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);
Comment
Add comment · Show 10
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 iwaldrop · Nov 26, 2015 at 05:52 AM 0
Share

I think unity is on top and arduino on bottom, no? What shows up in the console with your print statement? What does fl resolve to? How are you consu$$anonymous$$g the fl value once you produce it?

avatar image KnightRiderGuy iwaldrop · Nov 26, 2015 at 12:11 PM 0
Share

Yes the bottom code snippet is from the Arduino side. I posted a topic in the forum where I was able to post the whole code:

$$anonymous$$y Forum Post

avatar image Scribe · Nov 26, 2015 at 04:00 PM 0
Share

Have a look at the delegate they use in the example at the bottom of this page.

I think your current method of reading the last line in the serial port will work until you get one positive feedback, and then the last line will remain not blank, and so your if will continue to fire. If you move it so it only happens due to the DataRecieved event (as in the example), it should work better.

avatar image KnightRiderGuy Scribe · Nov 26, 2015 at 06:45 PM 0
Share

Thanks Scribe but I'm not sure I follow?

avatar image Scribe KnightRiderGuy · Nov 26, 2015 at 08:53 PM 0
Share
 SerialPort sp = new SerialPort("/dev/cu.wchusbserial1420", 115200);
 
 void Awake () {
     ...
 
     sp.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
 
     ...
 }
 
 private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e){
     SerialPort serialport = (SerialPort)sender;
     string indata = sp.ReadLine();
     
     float lightVal = float.Parse(indata);
 
     if(lightVal < someCutoff){
         DoSomething();
     }
 }

$$anonymous$$aybe that helps convey my meaning?

The last bit may be wrong as I don't know if the data will be able to parsed directly to a float?!

Show more comments
avatar image Scribe · Nov 27, 2015 at 09:59 AM 0
Share

Well it looks like you would only get information sent when between a certain light level (assu$$anonymous$$g -of course- that your circuit is correct!). The problem I was seeing with your current code is this bit:

 message2 = sp.ReadLine();
 
 if (message2!="") {
     light$$anonymous$$eUp(message2);
 }

What I believe is probably happening, is as soon as you get one positive result, i.e. when light level satisfies: threshold -range < light level < threshold + range

you send a new line of data to the serialport, then, even if your lightlevel leaves the desired range, your last line of data sent is still not "", unless you explicitly write nothing to the serial port from the arduino.

Hence the if statement will fire forever, after the first correct light reading! presumably not what you want, thus why I suggested using the DataRecieved event ins$$anonymous$$d.

I would also suggest just printing all the data to test what kind of values you should be expecting to recieve from your light sensor.

Hope that helps!

avatar image KnightRiderGuy Scribe · Nov 27, 2015 at 12:42 PM 0
Share

I think I get some of what you are doing here. Now would I need to change anything on the Arduino side of the code?

I posted both of my entire codes to that forum post because I'm not totally sure what needs to go and what needs to stay... I do a lot of cobbling together and mucking about and after a while it gets kind of confusing and convoluted.

avatar image Scribe KnightRiderGuy · Nov 27, 2015 at 12:49 PM 0
Share

So you can choose (I think) to either change the unity code, or the arduino code, or both depending on how you want to deal with things.

Assu$$anonymous$$g the if statement on the Arduino is working, I would leave that code alone and focus on implementing the DataRecieved event as I suggested in the code smaple I gave previously.

If the arduino if-statement is not necessarily firing, I would suggest sending all the data to unity (by changing the arduino code) and then printing everything in unity (by changing the unity code) and working out what you want the if statement to be, within unity. (you could always move the if logic back to the arduino once you have debugged what was happening).

Finally, another option would be to do Serial.println(""); on every loop in your arduino, that way you make sure that the last line is "" again, once the if statement is no longer true. This should really only be a temporary solution as it is not good practice

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

35 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

Related Questions

Photon Network Sending Bool 1 Answer

Change Value Of UI Light Slider to Read At Specific Points Of Slider Value 1 Answer

Unity AR - arduino/ Realtime,Unity AR - Arduino/Real-time 0 Answers

How can I manage the information from a sensor photoresistor in arduino? 0 Answers

Live data visualization with HoloLens 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