// Executed each frame void Update() { //--------------------------------------------------------------------- // Send data //--------------------------------------------------------------------- // If you press one of these keys send it to the serial device. A // sample serial device that accepts this input is given in the README. if (Input.GetKeyDown(KeyCode.A)) { Debug.Log("Sending A"); serialController.SendSerialMessage("A"); } if (Input.GetKeyDown(KeyCode.Z)) { Debug.Log("Sending Z"); serialController.SendSerialMessage("Z"); } // Sends the string "doot doot" through the COM port. The Arduino should send // ("Doot Success") to its USB Serial Port if (Input.GetKeyDown(KeyCode.B)) { Debug.Log("Doot Doot"); serialController.SendSerialMessage("DootDoot"); } // If the bluetooth module disconnects (flashing light) then "E" can be pressed // to attempt to reconnect to it if (Input.GetKeyDown(KeyCode.E)) { Debug.Log("Reconnecting"); serialController.Reconnect(); } //--------------------------------------------------------------------- // Receive data //--------------------------------------------------------------------- string message = serialController.ReadSerialMessage(); if (message == null) { return; } // Check if the message is plain data or a connect/disconnect event. if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_CONNECTED)) { Debug.Log("Connection established"); } else if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_DISCONNECTED)) { Debug.Log("Connection attempt failed or disconnection detected"); } else { Debug.Log("Message arrived: " + message); } }
public void TakeDamage(float amount) { if (!m_IsInvulnerable) { // Reduce current health by the amount of damage done. m_CurrentHealth -= amount; // Change the UI elements appropriately. SetHealthUI(); // If the current health is at or below zero and it has not yet been registered, call OnDeath. if (m_CurrentHealth <= 0f && !m_Dead) { OnDeath(); } else { // Send a message to the reciever if (m_Controller != "Keyboard") { m_SerialController.SendSerialMessage(m_PlayerTeamID + "D"); } } } }
// Executed each frame void Update() { //--------------------------------------------------------------------- // Send data //--------------------------------------------------------------------- // If you press one of these keys send it to the serial device. A // sample serial device that accepts this input is given in the README. if (Input.GetKeyDown(KeyCode.A)) { Debug.Log("Sending A"); serialController.SendSerialMessage("A"); } if (Input.GetKeyDown(KeyCode.Z)) { Debug.Log("Sending Z"); serialController.SendSerialMessage("Z"); } if (Input.GetKeyDown(KeyCode.K)) { Debug.Log("Turning off the lights."); serialController.SendSerialMessage("K"); } if (Input.GetKeyDown(KeyCode.L)) { Debug.Log("Let there be light"); serialController.SendSerialMessage("L"); } //--------------------------------------------------------------------- // Receive data //--------------------------------------------------------------------- string message = serialController.ReadSerialMessage(); if (message == null) { return; } // Check if the message is plain data or a connect/disconnect event. if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_CONNECTED)) { Debug.Log("Connection established"); } else if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_DISCONNECTED)) { Debug.Log("Connection attempt failed or disconnection detected"); } else { Debug.Log("Message arrived: " + message); } }
void Start() { serialController = GameObject.Find("SerialController").GetComponent <SerialController>(); // Will attach a VideoPlayer to the main camera. GameObject camera = GameObject.Find("Main Camera"); videoPlayer = camera.AddComponent <UnityEngine.Video.VideoPlayer>(); // Play on awake defaults to true. Set it to false to avoid the url set // below to auto-start playback since we're in Start(). videoPlayer.playOnAwake = false; // By default, VideoPlayers added to a camera will use the far plane. // Let's target the near plane instead. //videoPlayer.renderMode = UnityEngine.Video.VideoRenderMode.CameraNearPlane; // This will cause our scene to be visible through the video being played. //videoPlayer.targetCameraAlpha = 0.5F; // Set the video to play. URL supports local absolute or relative paths. // Here, using absolute. videoPlayer.url = "Assets/Movies/" + GameControl.CurrentAnimal + "Intro.mp4"; serialController.SendSerialMessage("1"); // Skip the first 100 frames. //videoPlayer.frame = 100; // Restart from beginning when done. videoPlayer.isLooping = false; // Add handler for loopPointReached videoPlayer.loopPointReached += EndReached; // Start playback. This means the VideoPlayer may have to prepare (reserve // resources, pre-load a few frames, etc.). To better control the delays // associated with this preparation one can use videoPlayer.Prepare() along with // its prepareCompleted event. switch (GameControl.CurrentAnimal) { case "Elephant": serialController.SendSerialMessage("B"); break; case "Lion": serialController.SendSerialMessage("R"); break; default: serialController.SendSerialMessage("G"); break; } videoPlayer.Play(); }
//only takes two ints- can probably be sent in any part of the program //can run game w/o Arduino connection, play w/ message_L and message_R and get that working in Unity //test tomorrow with hardware void SerialSend(int message_L, int message_R) { // FOR MOTOR BACKBACK // Send in int values from 0 - 654 for each arm message_L = Mathf.Clamp(message_L, 0, 654); message_R = Mathf.Clamp(message_R, 0, 654); //these "L" and "R" chars are how MotorParty.ino knows which int goes to which motor serialController.SendSerialMessage("L" + message_L); serialController.SendSerialMessage("R" + message_R); }
// - - - - - - - - - - - - - - - - - - - - - - - // send a string message to the arduino board // message is sent to arduino character by character for more robustness // a command executed by the arduino program must end with ">" private void SendToArduino(string msg) { // optional : flushing Arduino command buffer in case bad characters are still in it // serialController.SendSerialMessage(">"); foreach (char c in msg) { serialController.SendSerialMessage(c.ToString()); } // Arduinos commands are executed when a ">" is received serialController.SendSerialMessage(">"); Debug.Log("Sending " + msg + ">"); }
void SendDatas() { if (Input.GetKeyDown(KeyCode.A)) { Debug.Log("Sending A"); serialController.SendSerialMessage("A"); } if (Input.GetKeyDown(KeyCode.Z)) { Debug.Log("Sending Z"); serialController.SendSerialMessage("Z"); } }
// Executed each frame void Update() { //--------------------------------------------------------------------- // Send data //--------------------------------------------------------------------- // If you press one of these keys send it to the serial device. A // sample serial device that accepts this input is given in the README. if (Input.GetKeyDown(KeyCode.A)) { Debug.Log("Sending A"); serialController.SendSerialMessage("A"); // serialController.SendSerialMessage("1.75"); //test to send a string --worked Vector3 sd = new Vector3(1.2f, 1.6f, 1.5f); string ty = sd.ToString(); serialController.SendSerialMessage(ty); //test to send a vector --worked. } if (Input.GetKeyDown(KeyCode.Z)) { Debug.Log("Sending Z"); serialController.SendSerialMessage("Z"); } //--------------------------------------------------------------------- // Receive data //--------------------------------------------------------------------- string message = serialController.ReadSerialMessage(); if (message == null) { return; } // Check if the message is plain data or a connect/disconnect event. if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_CONNECTED)) { Debug.Log("Connection established"); } else if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_DISCONNECTED)) { Debug.Log("Connection attempt failed or disconnection detected"); } else { Debug.Log("Message arrived: " + message); } }
void Start() { serialController.SendSerialMessage("q"); videoPlayer = GameObject.Find("Main Camera").AddComponent <UnityEngine.Video.VideoPlayer>(); videoPlayer.playOnAwake = false; audioSource = GameObject.Find("HowtoPlay" + "_1").GetComponent <AudioSource>(); videoPlayer.url = "Assets/Movies/" + "HowtoPlay" + ".mp4"; videoPlayer.isLooping = false; // Add handler for loopPointReached videoPlayer.loopPointReached += EndReached; videoPlayer.prepareCompleted += Prepared; videoPlayer.Prepare(); }
// Executed each frame void Update() { //--------------------------------------------------------------------- // Send data //--------------------------------------------------------------------- // If you press one of these keys send it to the serial device. A // sample serial device that accepts this input is given in the README. if (GetComponent <ObjectDetect>().triggerSignal != prev) { if (GetComponent <ObjectDetect>().triggerSignal == false) { Debug.Log("Sending Z"); serialController.SendSerialMessage("Z"); } else if (GetComponent <ObjectDetect>().triggerSignal == true) { Debug.Log("Sending A"); serialController.SendSerialMessage("A"); } } prev = GetComponent <ObjectDetect>().triggerSignal; //--------------------------------------------------------------------- // Receive data //--------------------------------------------------------------------- string message = serialController.ReadSerialMessage(); if (message == null) { return; } // Check if the message is plain data or a connect/disconnect event. if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_CONNECTED)) { Debug.Log("Connection established"); } else if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_DISCONNECTED)) { Debug.Log("Connection attempt failed or disconnection detected"); } else { Debug.Log("Message arrived: " + message); } }
void Update() { serialControllerL.SendSerialMessage(Left); serialControllerR.SendSerialMessage(Right); /* * if (i % 2 == 0) * { * serialControllerL.SendSerialMessage("F0P70\nF1P70\nF2P70\nF3P70"); * serialControllerR.SendSerialMessage("F0P70\nF1P70\nF2P70\nF3P70"); * } * else if (i % 2 == 1) * { * serialControllerL.SendSerialMessage("F0P20\nF1P30\nF2P40\nF3P50"); * serialControllerR.SendSerialMessage("F0P20\nF1P30\nF2P40\nF3P50"); * } */ if (i % 50 == 0) { ts = stopWatch.Elapsed; PrintElapsedTime(ts); } i++; //UnityEngine.Debug.Log("We've been through the loop " + i + " times."); }
private void OnTriggerEnter(Collider collider) { // If is an enemy tank if (m_OtherTanks.Contains(collider.gameObject) && !OtherHasFlag()) { collider.GetComponent <TankHealth>().m_HasFlag = true; if (m_Controller != "Keyboard") { m_SerialController.SendSerialMessage("C"); } } // If is own tank if (m_SelfTanks.Contains(collider.gameObject)) { // Start healing collider.GetComponent <TankHealth>().m_Healing = true; collider.GetComponent <TankHealth>().StartHeal(); // If tank has flag if (collider.GetComponent <TankHealth>().m_HasFlag) { collider.GetComponent <TankHealth>().m_HasFlag = false; if (m_Team == "Red") { m_GameManager.GetComponent <GameManager>().m_RedTeamScore += 1; m_GameManager.GetComponent <GameManager>().UpdateScore(m_Team); } else { m_GameManager.GetComponent <GameManager>().m_BlueTeamScore += 1; m_GameManager.GetComponent <GameManager>().UpdateScore(m_Team); } m_OtherFlag.transform.position = m_OtherFlagPosition; } } }
// Executed each frame void Update() { //--------------------------------------------------------------------- // Receive data //--------------------------------------------------------------------- string message = serialController.ReadSerialMessage(); if (message == null) { return; } // Check if the message is plain data or a connect/disconnect event. if (ReferenceEquals(messageIN, SerialController.SERIAL_DEVICE_CONNECTED)) { Debug.Log("Connection established"); } else if (ReferenceEquals(messageIN, SerialController.SERIAL_DEVICE_DISCONNECTED)) { Debug.Log("Connection attempt failed or disconnection detected"); } else { Debug.Log("Message arrived: " + messageIN); } messageIN = message; //--------------------------------------------------------------------- // Send data //--------------------------------------------------------------------- serialController.SendSerialMessage(messageOUT); }
// Executed each frame void Update() { //--------------------------------------------------------------------- // Send dados //--------------------------------------------------------------------- // If you press one of these keys send it to the serial device. A // sample serial device that accepts this input is given in the README. if (Input.GetKeyDown(KeyCode.Alpha1) || Input.GetKeyDown(KeyCode.Keypad1)) { Debug.Log("Sending lights ON"); serialController.SendSerialMessage("1"); } if (Input.GetKeyDown(KeyCode.Alpha2) || Input.GetKeyDown(KeyCode.Keypad2)) { Debug.Log("Sending lights OFF"); serialController.SendSerialMessage("2"); } //--------------------------------------------------------------------- // Receive dados //--------------------------------------------------------------------- string message = serialController.ReadSerialMessage(); if (message == null) { return; } // Check if the message is plain dados or a connect/disconnect event. if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_CONNECTED)) { Debug.Log("Connection established"); } else if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_DISCONNECTED)) { Debug.Log("Connection attempt failed or disconnection detected"); } else { Debug.Log("Message arrived: " + message); } }
// send message to set led void led(string s) { if (manager.getIsSerialLED()) { ledMsg = s; serialControllerLED.SendSerialMessage(ledMsg); } }
private void SendSerialMessage(string letter) { if (playerControl == false) { return; } switch (letter) { case "A": if (Tightenings < MaxTightenings) { Tightenings++; serialController.SendSerialMessage("A"); } break; case "D": if (Tightenings > 0) { Tightenings--; serialController.SendSerialMessage("D"); } break; case "X": Tightenings = 0; serialController.SendSerialMessage("X"); break; case "B": if (Tightenings + 100 < MaxTightenings) { Tightenings += 100; serialController.SendSerialMessage("B"); } else if (Tightenings < MaxTightenings) { Tightenings = MaxTightenings; serialController.SendSerialMessage("C"); } break; case "O": if (Tightenings - 100 > 0) { Tightenings -= 100; serialController.SendSerialMessage("O"); } else if (Tightenings > 0) { Tightenings = 0; serialController.SendSerialMessage("P"); } break; } }
// Executed each frame void Update() { if (Input.inputString != "") { int asciiCode = System.Convert.ToInt32(Input.inputString[0]); //ugly hack. Can't seem to get the arduino to detect enter key (/r?) so I send a specific command when ascii code 13 (enter key) is pressed. serialController.SendSerialMessage(Input.inputString.Substring(0, 1)); } }
// Update is called once per frame void Update() { currentTime = System.DateTime.Now; if ((currentTime - lastUpdate).Milliseconds > timeBetweenUpdates) { Debug.Log("Updating Slaves to: {111211311411511611711811911A11B11}\n"); serialController.SendSerialMessage("{111211311411511611711811911A11B11}\n"); lastUpdate = currentTime; } }
private void OnMouseDown() { if (anim.isPlaying) { serialController.SendSerialMessage("foff"); anim.Stop(); particle1.Stop(); particle2.Stop(); spotLight.enabled = false; } else { serialController.SendSerialMessage("fon"); anim.Play(); particle1.Play(); particle2.Play(); spotLight.enabled = true; } }
void Send_IMU_State() { string data = ""; data += Round_Float(transform.rotation.x, 4) + " "; data += Round_Float(transform.rotation.y, 4) + " "; data += Round_Float(transform.rotation.z, 4) + " "; data += Round_Float(transform.rotation.w, 4); serialport.SendSerialMessage(data); }
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.S)) { serialController.SendSerialMessage("s"); msg = "Start To Comunicate"; } if (Input.GetKeyDown("0")) { serialController.SendSerialMessage("0"); msg = "Turn Off The Light"; } if (Input.GetKeyDown("1")) { serialController.SendSerialMessage("1"); msg = "Turn On The Light"; } }
public override void SendMessageToArduino(string message) { Debug.Log($"Sending to arduino[{(ArduinoConnected ? 0 : 1)}]{gameObject.name}: {message} "); if (!ArduinoConnected) { return; } serialController.SendSerialMessage(message); MessagesSent++; }
private void ChooseStartingCities(object sender, SerialController serial) { serial.SendSerialMessage("S"); ChooseRandomCities(); arduinoA.serial.SendSerialMessage("Z" + startingIndex); arduinoA.serial.SendSerialMessage("Z" + endIndex); arduinoB.serial.SendSerialMessage("Z" + startingIndex); arduinoB.serial.SendSerialMessage("Z" + endIndex); }
private void Update() { if (GameControl.Button1Count > 0 && GameControl.Button2Count > 0 && GameControl.Button3Count > 0 && GameControl.Button4Count > 0 && GameControl.Button5Count > 0) { //send stop message serialController.SendSerialMessage("s"); SceneManager.LoadScene("PressButton"); } string message = serialController.ReadSerialMessage(); if (message == null) { return; } // Check if the message is plain data or a connect/disconnect event. if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_CONNECTED)) { Debug.Log("Connection established"); } else if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_DISCONNECTED)) { Debug.Log("Connection attempt failed or disconnection detected"); } else { Debug.Log("Message arrived: " + message); } InputHandler(message); }
private void SendCommands() { string output = null; if (commandsToSendNext.Count == 1) { Command command = commandsToSendNext.Pop(); output = JsonUtility.ToJson(command); } else if (commandsToSendNext.Count > 0) { CommandList commandList = new CommandList { modules = commandsToSendNext.ToArray() }; output = JsonUtility.ToJson(commandList); commandsToSendNext.Clear(); } if (output != null && !output.Equals(lastCommand)) { lastCommand = output; if (serialController.enabled == true) { serialController.SendSerialMessage(output); } if (ShowOutMessages == true) { Debug.Log(output); } /* * bool moduleIdToBeDisplayed = false; * * for (int i = 0; i < MessageFilterById.Length; i++) * { * if (MessageFilterById[i] == command.id) * { * moduleIdToBeDisplayed = true; * break; * } * } * * if (MessageFilterById.Length == 0 || moduleIdToBeDisplayed) * { * Debug.Log(JsonUtility.ToJson(command)); * } */ } }
void OreHit(Ore.OreType type) { if (type == thisType /* && thisType == oreSwitcher.oreActive*/) { hitAmount++; if (hitAmount >= hitThreshold) { Instantiate(thisOre); stringMessage = EnumtoChar(type).ToString(); stringMessage += hitAmount; serialController.SendSerialMessage(stringMessage); hitAmount = 0; //oreSwitcher.DeactivateOre(); print(stringMessage); } else { stringMessage = EnumtoChar(type).ToString(); stringMessage += hitAmount; serialController.SendSerialMessage(stringMessage); print(stringMessage); } } }
void writeValues() { string writeVal = "<" + string.Format("{0:N02}", detach) + ", " + string.Format("{0:N02}", rotateLeftVal1) + ", " + string.Format("{0:N02}", rotateLeftVal2) + ", " + string.Format("{0:N02}", rotateLeftVal3) + ", " + string.Format("{0:N02}", rotateLeftVal4) + ", " + string.Format("{0:N02}", rotateRightVal1) + ", " + string.Format("{0:N02}", rotateRightVal2) + ", " + string.Format("{0:N02}", rotateRightVal3) + ", " + string.Format("{0:N02}", rotateRightVal4) + ">"; //string writeVal = "<" + string.Format("{0:N02}", detach) + ", " + string.Format("{0:N02}", rotateRightVal1) + ", " + string.Format("{0:N02}", rotateRightVal2) + ", " + string.Format("{0:N02}", rotateRightVal3) + ", " + string.Format("{0:N02}", rotateRightVal4) + ", " + string.Format("{0:N02}", rotateLeftVal1) + ", " + string.Format("{0:N02}", rotateLeftVal2) + ", " + string.Format("{0:N02}", rotateLeftVal3) + ", " + string.Format("{0:N02}", rotateLeftVal4) + ">"; //mySPort.WriteLine(writeVal); serialController.SendSerialMessage(writeVal); //string writeVal = string.Format("{0:N03}", rotateLeftVal) + "&" + string.Format("{0:N03}", rotateRightVal) + "&"; //mySPort.WriteLine(writeVal); print("RotateLeftVal: " + writeVal); //print("detach: " + detach); }
void OnCollisionEnter(Collision ctl) { //collision point? // add an explosion here if (explosion != null && ctl.gameObject.name == "loop") { Debug.Log("boom"); // Instantiate(explosion, transform.position,transform.rotation); } ContactPoint contact = ctl.contacts[0]; Vector3 pos = contact.point; Vector3 a = GetComponent <Rigidbody>().velocity; p.text = pos.ToString(); v.text = a.ToString(); //Debug.Log(x_cor + "||" + y_cor + "...cur_position:" + GetComponent<Rigidbody>().position); if (ffl == 0) { ffl = 1; } else { // string xx = (delta_angle).ToString("0.00"); // send the data int..., without end_sign... serialController.SendSerialMessage("666"); // if (delta_angle != 0) Debug.Log("Send 666"); } pre_angle = angle; //SceneManager.LoadScene("Mini_Game"); /* LineRenderGameObject2 = GameObject.Find("spot"); // draw the line after receive the data from arduino * lineRenderer2 = (LineRenderer)LineRenderGameObject2.GetComponent("LineRenderer"); * lineRenderer2.SetPosition(0, new Vector3(cur_pos[0], 0.0f, cur_pos[2])); * lineRenderer2.SetPosition(1, new Vector3((float)x_cor, 0.0f, (float)y_cor)); */ }
void Update() { { if (left != lastL) { serialControllerL.SendSerialMessage(left); } if (right != lastR) { serialControllerR.SendSerialMessage(right); } lastL = left; lastR = right; } if (i % 50 == 0) { ts = stopWatch.Elapsed; // PrintElapsedTime(ts); } i++; }
// Use this for initialization void Start() { serialController = GameObject.Find("SerialController").GetComponent <SerialController>(); GameControl.Button1Count = 0; GameControl.Button2Count = 0; GameControl.Button3Count = 0; GameControl.Button4Count = 0; GameControl.Button5Count = 0; RandomScene(); for (int i = 0; i < 50; i++) { serialController.SendSerialMessage("2"); } videoPlayer = GameObject.Find("Main Camera").AddComponent <UnityEngine.Video.VideoPlayer>(); videoPlayer.playOnAwake = false; audioSource = GameObject.Find("PressButtonLoop" + "_1").GetComponent <AudioSource>(); videoPlayer.url = "Assets/Movies/" + "PressButtonLoop" + ".mp4"; videoPlayer.isLooping = true; // Add handler for loopPointReached videoPlayer.prepareCompleted += Prepared; videoPlayer.Prepare(); }