├── Beef.cs ├── README.md ├── Spam.cs ├── arduinoController.cs └── buttonpress.ino /Beef.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using UnityEngine.SceneManagement; 4 | 5 | public class Beef : MonoBehaviour 6 | { 7 | public GameObject arduinoScriptContainer; 8 | arduinoController arduino; 9 | 10 | public Sprite beef1; 11 | public Sprite beef2; 12 | public Sprite beef3; 13 | public Sprite beef4; 14 | public Sprite beef5; 15 | public Sprite beef6; 16 | 17 | public GameObject good; 18 | public GameObject verygood; 19 | public GameObject fail; 20 | 21 | public GameObject beefImage; 22 | 23 | public GameObject cutline; 24 | 25 | private int num_mistakes = 0; 26 | private int cutnum = 0; 27 | 28 | private int pressedButton = -1; 29 | private int prevButton = -1; 30 | 31 | private double[] cutpos = {-1.65, -1.3, -0.85, -0.16, 0.4, 1, 1.6, 2.3}; 32 | 33 | void Start () 34 | { 35 | arduino = arduinoScriptContainer.GetComponent(); 36 | 37 | } 38 | 39 | private void updateButton(){ 40 | prevButton = pressedButton; 41 | pressedButton = -1; 42 | if(arduino.but_one == 1 && prevButton != 1){ 43 | pressedButton = 1; 44 | } else if(arduino.but_two == 1 && prevButton != 2){ 45 | pressedButton = 2; 46 | } else if(arduino.but_three == 1 && prevButton != 3){ 47 | pressedButton = 3; 48 | } else if(arduino.but_four == 1 && prevButton != 4){ 49 | pressedButton = 4; 50 | } else if(arduino.but_five == 1 && prevButton != 5){ 51 | pressedButton = 5; 52 | } else if(arduino.but_six == 1 && prevButton != 6){ 53 | pressedButton = 6; 54 | } 55 | } 56 | 57 | void Update () 58 | { 59 | 60 | if (Input.GetKeyDown("space")) 61 | { 62 | slice(); 63 | } 64 | updateButton(); 65 | if(cutnum == pressedButton - 1){ 66 | slice(); 67 | } else if (pressedButton != -1){ 68 | Debug.Log("previous button is: " + prevButton); 69 | Debug.Log("currently pressed button is:" + pressedButton); 70 | num_mistakes++; 71 | } 72 | 73 | } 74 | 75 | public void slice (){ 76 | cutnum++; 77 | if (cutnum == 1) { 78 | beefImage.GetComponent().sprite = beef1; 79 | cutline.transform.position = new Vector2 ((float)cutpos [cutnum + 1], -2.19f); 80 | Debug.Log(cutnum + 1); 81 | Debug.Log(cutpos [cutnum + 1]); 82 | } else if (cutnum == 2) { 83 | beefImage.GetComponent().sprite = beef2; 84 | cutline.transform.position = new Vector2 ((float)cutpos [cutnum + 1], -2.19f); 85 | } else if (cutnum == 3) { 86 | beefImage.GetComponent().sprite = beef3; 87 | cutline.transform.position = new Vector2 ((float)cutpos [cutnum + 1], -2.19f); 88 | } else if (cutnum == 4) { 89 | beefImage.GetComponent().sprite = beef4; 90 | cutline.transform.position = new Vector2 ((float)cutpos [cutnum + 1], -2.19f); 91 | } else if (cutnum == 5) { 92 | beefImage.GetComponent().sprite = beef5; 93 | cutline.transform.position = new Vector2 ((float)cutpos [cutnum + 1], -2.19f); 94 | } else if (cutnum == 6) { 95 | beefImage.GetComponent().sprite = beef6; 96 | cutline.SetActive(false); 97 | finishScene (); 98 | } 99 | } 100 | 101 | private void finishScene (){ 102 | Debug.Log(num_mistakes); 103 | if (num_mistakes < 5) { 104 | verygood.SetActive (true); 105 | } else if (num_mistakes < 10) { 106 | good.SetActive (true); 107 | } else { 108 | fail.SetActive (true); 109 | } 110 | Invoke ("loadNext", 4); 111 | } 112 | 113 | private void loadNext(){ 114 | SceneManager.LoadScene("SPAM"); 115 | } 116 | 117 | } 118 | 119 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # unity-arduino-cooking-mama 2 | files for art and arduino interactive project 3 | 4 | it's pretty simple, the buttonpress.ino file sends info through the serial port and Unity grabs the string from the serial port and parses it 5 | 6 | you'll need to change your player settings from .NET 2.0 subset to .NET 2.0! 7 | 8 | I hard coded my port in ("/dev/....") in the arduinoController.cs file but you might need to check which port your arduino is plugged into and change it to that string instead :) 9 | 10 | Thank you to Sidney Church for providing me with starter code and arduino help! 11 | -------------------------------------------------------------------------------- /Spam.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using UnityEngine.SceneManagement; 4 | using UnityEngine.UI; 5 | 6 | public class Spam : MonoBehaviour 7 | { 8 | public GameObject arduinoScriptContainer; 9 | arduinoController arduino; 10 | 11 | public GameObject verygood; 12 | public GameObject good; 13 | public GameObject fail; 14 | 15 | public Text countdown; 16 | private bool countingdown = true; 17 | int stage = 0; 18 | 19 | float currCountdownValue; 20 | public IEnumerator StartCountdown(float countdownValue = 10) 21 | { 22 | Debug.Log(currCountdownValue); 23 | currCountdownValue = countdownValue; 24 | while (currCountdownValue > 0 && countingdown) 25 | { 26 | yield return new WaitForSeconds(1.0f); 27 | currCountdownValue--; 28 | countdown.text = (int)currCountdownValue + ""; 29 | } 30 | } 31 | 32 | void Start () 33 | { 34 | arduino = arduinoScriptContainer.GetComponent(); 35 | StartCoroutine(StartCountdown()); 36 | 37 | } 38 | 39 | void Update () 40 | { 41 | if (Input.GetKeyDown("space")) 42 | { 43 | countingdown = false; 44 | finishScene (); 45 | } 46 | if (stage == 0 && arduino.photocell_one == 1) { 47 | stage = 1; 48 | } else if (stage == 1 && arduino.photocell_two == 1) { 49 | countingdown = false; 50 | finishScene (); 51 | } 52 | if (currCountdownValue <= 0) { 53 | finishScene (); 54 | } 55 | 56 | 57 | } 58 | 59 | 60 | private void finishScene (){ 61 | if (currCountdownValue > 5) { 62 | verygood.SetActive (true); 63 | } else if (currCountdownValue > 0) { 64 | good.SetActive (true); 65 | } else { 66 | fail.SetActive (true); 67 | } 68 | Invoke ("loadNext", 4); 69 | } 70 | 71 | private void loadNext(){ 72 | SceneManager.LoadScene("final"); 73 | } 74 | 75 | } 76 | 77 | -------------------------------------------------------------------------------- /arduinoController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using System; 5 | using System.IO.Ports; 6 | 7 | public class arduinoController : MonoBehaviour { 8 | SerialPort arduino = new SerialPort("/dev/cu.usbmodem1411", 9600); 9 | 10 | public int photocell_one = 0; 11 | public int photocell_two = 0; 12 | public int but_one = 0; 13 | public int but_two = 0; 14 | public int but_three = 0; 15 | public int but_four = 0; 16 | public int but_five = 0; 17 | public int but_six = 0; 18 | 19 | 20 | void Start () { 21 | try{ 22 | arduino.Open (); 23 | } catch { 24 | arduino = null; 25 | } 26 | } 27 | 28 | // Update is called once per frame 29 | void Update () { 30 | //arduino.WriteLine("1"); //this will send a 1 to the arduino letting it know that we are ready to recieve data 31 | //arduino.BaseStream.Flush(); //this ensures that the data has gotten to the arduino 32 | if(arduino != null) { 33 | string data = arduino.ReadLine(); 34 | serialParse(data); 35 | } 36 | } 37 | 38 | public void serialParse(string incoming) 39 | { 40 | string[] result = incoming.Split(new Char[]{','}, StringSplitOptions.RemoveEmptyEntries); 41 | // for (int i = 0; i < result.Length; i++) { 42 | // Debug.Log (i + ": " + result [i] + " "); 43 | // } 44 | try{ 45 | photocell_one = int.Parse(result[0]); 46 | photocell_two = int.Parse(result[1]); 47 | but_one = int.Parse(result[2]); 48 | but_two = int.Parse(result[3]); 49 | but_three = int.Parse(result[4]); 50 | but_four = int.Parse(result[5]); 51 | but_five = int.Parse(result[6]); 52 | but_six = int.Parse(result[7]); 53 | 54 | }catch{ 55 | } 56 | 57 | } 58 | 59 | 60 | } 61 | 62 | -------------------------------------------------------------------------------- /buttonpress.ino: -------------------------------------------------------------------------------- 1 | const int buttonOne = 3; 2 | const int buttonTwo = 4; 3 | const int buttonThree = 5; 4 | const int buttonFour = 6; 5 | const int buttonFive = 7; 6 | const int buttonSix = 8; 7 | const int buttonSeven = 9; 8 | 9 | int buttonStateOne = 0; 10 | int buttonStateTwo = 0; 11 | int buttonStateThree = 0; 12 | int buttonStateFour = 0; 13 | int buttonStateFive = 0; 14 | int buttonStateSix = 0; 15 | int buttonStateSeven = 0; 16 | 17 | int photocellReadingOne = 0; 18 | int photocellReadingTwo = 0; 19 | 20 | void setup() { 21 | 22 | Serial.begin(9600); 23 | pinMode(buttonOne, INPUT); 24 | pinMode(buttonTwo, INPUT); 25 | pinMode(buttonThree, INPUT); 26 | pinMode(buttonFour, INPUT); 27 | pinMode(buttonFive, INPUT); 28 | pinMode(buttonSix, INPUT); 29 | pinMode(buttonSeven, INPUT); 30 | 31 | } 32 | 33 | void loop() { 34 | 35 | buttonStateOne = digitalRead(buttonOne); 36 | buttonStateTwo = digitalRead(buttonTwo); 37 | buttonStateThree = digitalRead(buttonThree); 38 | buttonStateFour = digitalRead(buttonFour); 39 | buttonStateFive = digitalRead(buttonFive); 40 | buttonStateSix = digitalRead(buttonSix); 41 | buttonStateSeven = digitalRead(buttonSeven); 42 | 43 | photocellReadingOne = analogRead(A0); 44 | photocellReadingTwo = analogRead(A1); 45 | if(photocellReadingOne > 10) { 46 | Serial.print("1,"); 47 | } else { 48 | Serial.print("0,"); 49 | } 50 | if(photocellReadingTwo < 20) { 51 | Serial.print("1,"); 52 | } else { 53 | Serial.print("0,"); 54 | } 55 | 56 | if(buttonStateOne == HIGH){ 57 | Serial.print("1,"); 58 | } else { 59 | Serial.print("0,"); 60 | } 61 | 62 | if(buttonStateTwo == HIGH){ 63 | Serial.print("1,"); 64 | } else { 65 | Serial.print("0,"); 66 | } 67 | 68 | if(buttonStateThree == HIGH){ 69 | Serial.print("1,"); 70 | } else { 71 | Serial.print("0,"); 72 | } 73 | 74 | // if(buttonStateFour == HIGH){ 75 | // Serial.print("1,"); 76 | // } else { 77 | // Serial.print("0,"); 78 | // } 79 | // 80 | if(buttonStateFive == HIGH){ 81 | Serial.print("1,"); 82 | } else { 83 | Serial.print("0,"); 84 | } 85 | 86 | if(buttonStateSix == HIGH){ 87 | Serial.print("1,"); 88 | } else { 89 | Serial.print("0,"); 90 | } 91 | 92 | if(buttonStateSeven == HIGH){ 93 | Serial.print("1,"); 94 | } else { 95 | Serial.print("0,"); 96 | } 97 | 98 | Serial.println(""); 99 | delay(100); 100 | 101 | } 102 | --------------------------------------------------------------------------------