How Do I Activate The Switch Statement
OK I have been at this for hours, I can receive the characters into Unity through the serial port from the Arduino but HOW do I get them to activate my switch statement at the bottom of the script.
// 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);*/
string message3 = sp.ReadLine(); //get the message...
if(message3 == ""); //if its empty stop right here
print(message3);
DirectionArrow (message3);
///////////
/// CALL THIS TO THE SWITCH CASE BOTTOM OF SCRIPT.... HOW DO I DO THAT??
///////////
}
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)]);
}
void DirectionArrow(string buttonStatus)
{
switch (buttonStatus)
{
//OFF Buttons States
case "O":
//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 "W":
//Activate Left Direction Arrow Indicator
SystemGuidanceManagerScript WAOn = FindObjectOfType<SystemGuidanceManagerScript>();
WAOn.WestArrow();
SystemGuidanceManagerScript WBSOn = FindObjectOfType<SystemGuidanceManagerScript>();
WBSOn.BarLEDsSounderOn();
break;
//RIGHT BUTTON ON STATE
case "E":
//Activate Right Direction Arrow Indicator
SystemGuidanceManagerScript EAOn = FindObjectOfType<SystemGuidanceManagerScript> ();
EAOn.EastArrow ();
SystemGuidanceManagerScript EBSOn = FindObjectOfType<SystemGuidanceManagerScript>();
EBSOn.BarLEDsSounderOn();
break;
}
}
What do you mean by "Activate The Switch Statement"?
Do you mean that you're getting to the DirectionArrow() function, but none of your case statements is executing?
$$anonymous$$aybe put some logging in the Case statements to show us what is going on.
Also at line 24, change to this:
catch (System.Exception ex) {
print( ex.$$anonymous$$essage);
return;
}
I bet that your "E" character is returning with the linefeed or carriage return.
At line 19, do this:
DirectionArrow (message3.Trim());
@jmonasterio, Hey that seems to be executing the case statements now, The ONLY weird thing is that when I go to my System Status scene on the 2nd time, not the first time but the 2nd time, the bar graph animated lights are all lighted up.
Answer by jmonasterio · Dec 29, 2015 at 12:53 AM
I bet that your "E" character is returning with the linefeed or carriage return.
At line 19, do this:
DirectionArrow (message3.Trim());
Your other issues should probably be in a new question, after you accept this answer.
@jmonasterio, That certainly seems to have worked, thanks man I had been at this all day trying to figure out what was going on...... just a few small lines of code too, go figure.
:)
Along with this part you had mentioned @jmonasterio, this fixed the issue perfectly although I'm not sure how it knows to read that in the bottom part of the script? But I'm not gonna knock what works ;)
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;
}
//print("BytesToRead" +sp.BytesToRead);
message2 = sp.ReadLine();
Also not sure why my:
public string message2;
public string message3; //Not sure why this was here as it does not seem to make a difference??
For message 2 does not display anything in the inspector.... $$anonymous$$or and not very important just more curious about that?