Convert an Int to a String
OK I think some of my issue is that I'm getting a conflict of information coming in from the Serial port from the Arduino. the LDR sensor is reading in a string and the buttons are coming in as 0 = Button Off, 1 = RightButton, & 2 = Left button,
Is it possible to change the numbers to perhaps letter characters something like: in stead of "0" maybe the capital letter "O" for off and a capital "W" for Left and capital "E" for right.
I do know that on the Arduino side I can send those characters out to the serial port but what would I need to change on my unity side to do that?
// Update is called once per frame
void Update () {
/*string dataFromArduinoString = sp.ReadLine ();
int dFAI = int.Parse (dataFromArduinoString);
print (dFAI);
DirectionArrow (dFAI);*/
try
{
//DirectionArrow(sp.ReadByte());
//print(sp.ReadByte());
string dataFromArduinoString = sp.ReadLine ();
int dFAI = int.Parse (dataFromArduinoString);
print (dFAI);
DirectionArrow (dFAI);
}
catch (System.Exception) {
}
//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;
// 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)]);
}
//////////
// Switch cases I'm using to catch the button information defined in the update above /////////
void DirectionArrow(int buttonStatus)
//void DirectionArrow(int Direction)
{
switch (buttonStatus)
{
//OFF Buttons States
case 0:
//deactivate Left Direction Arrow
SystemGuidanceManagerScript WAOff = FindObjectOfType<SystemGuidanceManagerScript>();
WAOff.WestArrowOff ();
SystemGuidanceManagerScript EAOff = FindObjectOfType<SystemGuidanceManagerScript>();
EAOff.EastArrowOff();
SystemGuidanceManagerScript SFXOff = FindObjectOfType<SystemGuidanceManagerScript>();
SFXOff.TopIndicatorOff();
SystemGuidanceManagerScript BGSOff = FindObjectOfType<SystemGuidanceManagerScript>();
BGSOff.BarLEDsSounderOff();
break;
//LEFT BUTTON ON STATE
case 1:
//Activate Left Direction Arrow Indicator
SystemGuidanceManagerScript WAOn = FindObjectOfType<SystemGuidanceManagerScript>();
WAOn.WestArrow();
SystemGuidanceManagerScript WBSOn = FindObjectOfType<SystemGuidanceManagerScript>();
WBSOn.BarLEDsSounderOn();
break;
//RIGHT BUTTON ON STATE
case 2:
//Activate Right Direction Arrow Indicator
SystemGuidanceManagerScript EAOn = FindObjectOfType<SystemGuidanceManagerScript> ();
EAOn.EastArrow ();
SystemGuidanceManagerScript EBSOn = FindObjectOfType<SystemGuidanceManagerScript>();
EBSOn.BarLEDsSounderOn();
break;
}
}
Ok On the Arduino side I changed the Code to send 3 capital characters in s$$anonymous$$d of numbers.