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);
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?
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:
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.
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?!
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!
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.
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
Your answer
Follow this Question
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