I have a UI slider set to react to information from an Arduino. Is there a way to read in the value of the UI slider so that when the slider reaches a certain value I can then use that information in my script to trigger something?
UPDATE: I added the whole code so you can see what it is I may be messing up.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.IO.Ports;
using System.Threading;
public class Sending : MonoBehaviour {
//int sysHour = System.DateTime.Now.Hour;
//Random Clips
//public AudioClip[] darknessDetectedVoices;
//public AudioClip[] brightnessDetectedVoices;
public AudioClip[] BrightnessAudioClips;
public AudioClip[] DarknessAudioClips;
//DTMF Tones
public AudioClip DTMFtone01;
public AudioSource source;
//UI Text Reference
//public Text MessageCentreText;
//_isPlayingSound is true when a sound is currently playing - just as the name suggests.
private bool _isPlayingSound;
public GameObject LightSlider;
public Slider slider;
Slider lightSlider;
public static Sending sending;
//public static SerialPort sp = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
public static SerialPort sp = new SerialPort("/dev/cu.wchusbserial1420", 115200); //115200
public string message2;
//Button States
bool button01State = false;
void Awake () {
if (sending == null) {
DontDestroyOnLoad (gameObject);
sending = this;
} else if (sending != this) {
Destroy (gameObject);
}
}
float timePassed = 0.0f;
// Use this for initialization
void Start () {
OpenConnection();
lightSlider = GetComponent<Slider> ();
if(slider == null) slider = GetComponent<Slider>(); // search slider in this object if its not set in unity inspector
if(source == null) source = GetComponent<AudioSource>(); // search audiosource in this object if its not set in unity inspector
}
// Update is called once per frame
void Update () {
//timePassed+=Time.deltaTime;
//if(timePassed>=0.2f){
//print("BytesToRead" +sp.BytesToRead);
message2 = sp.ReadLine();
//print(message2);
// timePassed = 0.0f;
//}
/*if (message2!="") {
print("called");
lightMeUp(message2);
//MessageCentreText.text = "LIGHT VALUE CHANGED";
}*/
///////////////////
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)
// I dont know if a higher value means darker, if so add a "1 - " right after the "="
float input = /* 1 - */ float.Parse (message) / 100f;
// set the slider to the 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.8f)
source.PlayOneShot (BrightnessAudioClips [Random.Range (0, BrightnessAudioClips.Length)]);
else if (slider.value < 0.2f)
source.PlayOneShot (DarknessAudioClips [Random.Range (DarknessAudioClips.Length)]);
//////////////////
}
public void OpenConnection()
{
if (sp != null)
{
if (sp.IsOpen)
{
sp.Close();
print("Closing port, because it was already open!");
}
else
{
sp.Open(); // opens the connection
sp.ReadTimeout = 16; // sets the timeout value before reporting error
print("Port Opened!");
// message = "Port Opened!";
}
}
else
{
if (sp.IsOpen)
{
print("Port is already open");
}
else
{
print("Port == null");
}
}
}
void OnApplicationQuit()
{
sp.Close();
}
//Movie Player Toggle
public static void sendYellow(){
sp.Write("y");
}
//Analyzer Toggle
public static void sendYellow2(){
sp.Write("A");
}
//Pod 7DLA Toggle
public static void sendYellow3(){
sp.Write("D");
}
//Pod PENG Toggle
public static void sendYellow4(){
sp.Write("P");
}
//Pod 6RM Toggle
public static void sendYellow5(){
sp.Write("6");
}
//Pod Laser Toggle
public static void sendYellow6(){
sp.Write("Z");
}
//Auto Phone Toggle
public static void sendGreen(){
sp.Write("g");
//sp.Write("
");
}
//Oil Slick Toggle
public static void sendRed(){
sp.Write("r");
}
//Surveillance Mode Toggle
public static void sendBlue(){
sp.Write("b");
}
//Scanner Toggle
public static void sendRed2(){
sp.Write("1");
}
//Fog Lights Toggle
public static void sendGreen2(){
sp.Write("f");
}
//Head Lights Toggle
public static void sendGreen3(){
sp.Write("h");
}
//Hight Beams Toggle
public static void sendWhite(){
sp.Write("H");
}
//Rear Hatch Popper
public static void sendPulse1(){
sp.Write("p");
}
//Grappling Hook Launch
public static void sendPulse2(){
sp.Write("q");
}
//Auto Doors Right Pulse
public static void sendPulse3(){
sp.Write("R");
}
//Auto Doors Left Pulse
public static void sendPulse4(){
sp.Write("L");
}
//Startup and Shutdown Pulse
public static void sendPulse5(){
sp.Write("S");
}
/*void lightMeUp(string message){
//If there is a sound currently playing, return immediately so we don't play another sound simultaneously.
//if(_isPlayingSound) is shorthand for if(_isPlayingSound == true), in case you where wondering.
if (_isPlayingSound)
return;
print (message);
float fl = float.Parse (message) / 100.0f;
fl = 1.0f - fl;
LightSlider.GetComponent<Slider>().value = fl;
//Do something Here if Light Value / UI Slide on the LDR is near Darkness
if (LightSlider.GetComponent<Slider> ().value <= 0.1) {
Debug.Log ("DARKNESS DETECTED");
//Pick a random sound.
AudioClip randomClip = darknessDetectedVoices [UnityEngine.Random.Range (0, darknessDetectedVoices.Length)];
//Play that sound.
GetComponent<AudioSource> ().PlayOneShot (randomClip);
//Flag that we are currently playing a sound.
_isPlayingSound = true;
//Start a coroutine that will set the flag to false once the sound stops playing, so we can start playing a new sound.
StartCoroutine (RunResetIsPlayingSoundFlag (randomClip.length));
if (_isPlayingSound)
return;
}
//Do Something Here if the Light Value on the LDR is near Daylight brightness
else if (LightSlider.GetComponent<Slider> ().value >= 0.6) {
Debug.Log ("NEAR DAYLIGHT DETECTED");
//Pick a random sound.
AudioClip randomClip2 = brightnessDetectedVoices [UnityEngine.Random.Range (0, brightnessDetectedVoices.Length)];
//Play that sound.
GetComponent<AudioSource> ().PlayOneShot (randomClip2);
//Flag that we are currently playing a sound.
_isPlayingSound = true;
//Start a coroutine that will set the flag to false once the sound stops playing, so we can start playing a new sound.
StartCoroutine (RunResetIsPlayingSoundFlag (randomClip2.length));
if (_isPlayingSound)
return;
}
//BONUS if Possible Do something if the LDR is deliberately played with like waving hand over or blocking and un blocking the sensor.
}
private IEnumerator RunResetIsPlayingSoundFlag(float soundDuration)
{
//Wait until the sound finishes playing.
yield return new WaitForSeconds(soundDuration);
//yield return new WaitForSeconds(10.0f);
//Disable the _isPlayingSound flag, so we can play a new sound since this one is over.
_isPlayingSound = false;
} */
//Comlink Clock Close Button
public void GoMainScreen(){
StartCoroutine(LoadT3());
GetComponent<AudioSource>().PlayOneShot(DTMFtone01);
}
IEnumerator LoadT3(){
yield return new WaitForSeconds(0.0f); // wait time
}
}