Wow that was a bit of a mouthful for a question title, sorry but I could not think of a better way to word it.
Anyways @Soraphis,
Here is a new question based on the last updated script.
- Is there a way to have the light UI slider only trigger the audio clips wen it hits a certain pint of the slider like in my attached image?
- Is it possible to make it so that if the LDR sensor was deliberately played with i.e. someone waving their hand repeatedly over the sensor that I could trigger audio responses from a 3rd array of audio clips.
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 AudioClip DTMFtone02;
public AudioClip DTMFtone06;
public AudioClip DTMFtone08;
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 () {
//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)
// 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 (0, 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();
}
}