I'm trying to figure out how the Arduino knows or can tell between the two different types of data being sent over the serial port. In the serial monitor it looks like the data string is pretty much the same as the Temperature sensor so how does that data get differentiated from one another?
void loop()
{
Serial.print("Requesting temperatures...");
sensor_incar.requestTemperatures();
Serial.println("done");
Serial.print("Incar");
Serial.println(sensor_incar.getTempCByIndex(0));
// read the new light value (in range 0..1023):
int sensorValue = analogRead(lightSensorPin);
// if the value changed by 10
if(lightSensorValue - sensorValue > 10 || sensorValue - lightSensorValue > 10){
lightSensorValue = sensorValue; // save the new value
float p = lightSensorValue * (100.0 / 1023.0); // make the value to range 0..100
// the Parentheses may be for compiler optimization idk
Serial.println(p); // send it to unity
}
delay(30);
Simple answer: The Arduino doesn't distinguish. That is the programmers job. To the Arduino, "Bytes Is Bytes". It neither knows, nor cares, what those bytes mean. YOU need to figure out how you want to handle the data, and write your code accordingly.
where we are using the colon as an end of transmit marker (delimiter)
OK thanks BulldogLowell,
OK the console in in Unity may be a little new and unfamiliar to you sorry I realize this is an Arduino forum but I just want to share the data I'm receiving into Unity from the serial port. and also the data that shows up in the Arduino Serial monitor. I did this with the codes you posted and these were the results:
void loop()
{
Serial.print("Requesting temperatures...");
sensor_incar.requestTemperatures();
Serial.println("done");
Serial.print("Incar");
Serial.println(sensor_incar.getTempCByIndex(0));
Serial.println("T32.00:");
// read the new light value (in range 0..1023):
int sensorValue = analogRead(lightSensorPin);
// if the value changed by 10
if(lightSensorValue - sensorValue > 10 || sensorValue - lightSensorValue > 10){
lightSensorValue = sensorValue; // save the new value
float p = lightSensorValue * (100.0 / 1023.0); // make the value to range 0..100
// the Parentheses may be for compiler optimization idk
Serial.println(p); // send it to unity
Serial.println("L138:");
}
delay(30);
I'm not the greatest at code so please be patient. If the data is being sent in as:
T32.00
and
L138
I'm still pretty fuzzy on how I can differentiate that as something useful inside of Unity or I think the term is pars it to a string something like this: Unity Code.
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.Message);
return;
}
//Temperature Sensor Data
TemperatureText.text = TempSensorData.ToString ();
//print("BytesToRead" +sp.BytesToRead);
message2 = sp.ReadLine();
string message = sp.ReadLine(); //get the message...
if(message == "") 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 (message) / 100f;
// set the slider to the value
float oldValue = slider.value; // -------- this is new
slider.value = input;
I'm still pretty fuzzy on how I can differentiate that as something useful inside of Unity
I see that you are asking questions about how to do things in Unity, again. The last time that you did this you got the answer in a Unity forum and we went on from there. The Arduino output can, within reason, be adjusted to any format to suit Unity. Does Unity have a way of splitting a string at a token such as a comma, for instance ?
BulldogLowell:
OK, Unity I am not familiar... but do you know how to parse a simple JSON object using Unity?
you could just build a JSON string and write it to serial, I imagine.
I have heard of JSON but I'm not familiar with it at all, my code knowledge is pretty limited, I work best with working sample projects. Right now I've been able to get the buttons to work along with the LDR sensor, but even on the Unity side I'm fuzzy as hell as to how Unity knows that the data coming in from the Arduino is parsed as a "message 2 defined in the top part of the unity script. I'll shorten it down like this:
public string message2;
message2 = sp.ReadLine();
string message = sp.ReadLine(); //get the message...
if(message == "") 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 (message) / 100f;
// set the slider to the value
float oldValue = slider.value; // -------- this is new
slider.value = input;
UKHeliBob:
I see that you are asking questions about how to do things in Unity, again. The last time that you did this you got the answer in a Unity forum and we went on from there. The Arduino output can, within reason, be adjusted to any format to suit Unity. Does Unity have a way of splitting a string at a token such as a comma, for instance ?
Yes I realize this Bob,
But for this I think my Arduino script is not quite right as I have it coded like this so I'm not sure how the data gets sent out in any kind of distinguishable format.
void loop()
{
Serial.print("Requesting temperatures...");
sensor_incar.requestTemperatures();
//Serial.println("done");
Serial.print("Incar");
Serial.println(sensor_incar.getTempCByIndex(0));
// read the new light value (in range 0..1023):
int sensorValue = analogRead(lightSensorPin);
// if the value changed by 10
if(lightSensorValue - sensorValue > 10 || sensorValue - lightSensorValue > 10){
lightSensorValue = sensorValue; // save the new value
float p = lightSensorValue * (100.0 / 1023.0); // make the value to range 0..100
// the Parentheses may be for compiler optimization idk
Serial.println(p); // send it to unity
Serial.println("L138:");
}
delay(30);
UKHeliBob:
I see that you are asking questions about how to do things in Unity, again. The last time that you did this you got the answer in a Unity forum and we went on from there. The Arduino output can, within reason, be adjusted to any format to suit Unity. Does Unity have a way of splitting a string at a token such as a comma, for instance ?
P.S. I realize that a lot of this is on the Unity side but I think Arduino to Unity is still a pretty new thing with a lot of them as I find more about this doing Arduino searches.
Besides code is pretty much code to you guys.
Unity is using C#.... well sometimes they use a form of Java they call "Unity script" I hear but I switched tying to learn C# as I hear it's a much better language more compatible with stuff like the Arduino.
But for this I think my Arduino script is not quite right as I have it coded like this so I'm not sure how the data gets sent out in any kind of distinguishable format.
It sounds to me as though you have got this back to front. First, what is the most convenient format for Unity to decode. Secondly, can the Arduino output that format.
UKHeliBob:
It sounds to me as though you have got this back to front. First, what is the most convenient format for Unity to decode. Secondly, can the Arduino output that format.
Well obviously it can otherwise I would not be able to read in both my buttons which as I have mentioned before are coming into unity as characters O, N, W, E, S,
The LDR values seem to be coming in as values like this:
20.92
Consequently on the Unity side I send out characters and numbers to the Arduino to turn on LEDs, I take it you've watched the videos I have been posting links to that demo all of this?
On the Unity side it's doing something with this to use the LDR values to adjust a UI slider.
Not sure if this will make sense it barely does to me
//print("BytesToRead" +sp.BytesToRead);
message2 = sp.ReadLine();
string message = sp.ReadLine(); //get the message...
if(message == "") 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 (message) / 100f;
// set the slider to the value
float oldValue = slider.value; // -------- this is new
slider.value = input;
where we are using the colon as an end of transmit marker (delimiter)
Hey there BulldogLowell,
I'm not sue I'm using your method correctly, or course i'm not sure that I'm not either. Take a look at my Arduino code.
void loop()
{
//Request temperature from DS18B20
sensor_incar.requestTemperatures();
//Get the temperature
float incarTemperature = sensor_incar.getTempCByIndex(0);
//Send it to Unity? How ?
Serial.println(incarTemperature);
Serial.println("T32.00:"); //Not sure how this Works as some sort of marker?
// read the new light value (in range 0..1023):
int sensorValue = analogRead(lightSensorPin);
// if the value changed by 10
if(lightSensorValue - sensorValue > 10 || sensorValue - lightSensorValue > 10){
lightSensorValue = sensorValue; // save the new value
float p = lightSensorValue * (100.0 / 1023.0); // make the value to range 0..100
// the Parentheses may be for compiler optimization idk
Serial.println(p); // send it to unity
//Serial.println("L138:"); //Not sure how this Works as some sort of marker?
}
I have also attached a couple of screen grabs of the Arduino Serial monitor and the Unity console. Near as I can tell the data is being sent as numerical values that Unity can use.
PaulS:
You are not. The fact that you are sending a temperature is indicated by the fact that you send a T first, THEN send the temperature.
You would do that like so:
Serial.print("T");
Serial.println(incarTemperature);
Ahh OK thanks PaulS,
I now get these results in the Serial Monitor.
You can see where I press my buttons S, N, O, W, and where I cover the LDR sensor with my hand.
On Another not and yes I realize it the much hated by you all Unity side, code is code it's C# after all
I found this site that looks like at the top of his C# unity script he is separating the Serial information somehow. I'm not very good with code but thats what it looks like to me.... Did I win the Banana?
The monkey can now press the button
PaulS:
You are not. The fact that you are sending a temperature is indicated by the fact that you send a T first, THEN send the temperature.
You would do that like so:
Serial.print("T");
Serial.println(incarTemperature);
Hey Paul I just noticed that when I changed the Arduino Code to this:
void loop()
{
//Request temperature from DS18B20
sensor_incar.requestTemperatures();
//Get the temperature
float incarTemperature = sensor_incar.getTempCByIndex(0);
//Send it to Unity? How ?
Serial.print("T");
Serial.println(incarTemperature);
// read the new light value (in range 0..1023):
int sensorValue = analogRead(lightSensorPin);
// if the value changed by 10
if(lightSensorValue - sensorValue > 10 || sensorValue - lightSensorValue > 10){
lightSensorValue = sensorValue; // save the new value
float p = lightSensorValue * (100.0 / 1023.0); // make the value to range 0..100
// the Parentheses may be for compiler optimization idk
Serial.println(p); // send it to unity
}
delay(30);
The LDR data does not seem to come through, even though you can see it in the Arduino Serial monitor?
And yet if I add this in then it does?
//Send it to Unity
Serial.print("T");
Serial.println(incarTemperature);
Serial.println("T32.00:"); //Not sure how this Works as some sort of marker?
The LDR data does not seem to come through, even though you can see it in the Arduino Serial monitor?
And yet if I add this in then it does?
If the serial monitor shows correct data, then you have a problem in the Unity software. This is not a Unity forum. It seems like you are using the Arduino as a "shotgun" trying to hit the Unity "barn", blindfolded. You have to be more analytical and examine the Unity code to see why it's not interpreting the input correctly.
aarg:
If the serial monitor shows correct data, then you have a problem in the Unity software. This is not a Unity forum. It seems like you are using the Arduino as a "shotgun" trying to hit the Unity "barn", blindfolded. You have to be more analytical and examine the Unity code to see why it's not interpreting the input correctly.
Nope, I've been told it's how it's being sent, if you looked at my attachments you would know this
Knightriderguy:
Nope, I've been told it's how it's being sent, if you looked at my attachments you would know this
I did look at your attachments. It looked like the "Txxx" ones were correct. I assumed that the others were just unfinished business. Whatever you did to make the "T" work, just apply to the rest of the readings...