- Home /
The question is answered, right answer was accepted
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.
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).
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:
$$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
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.
@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.
@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());
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
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