Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO.Ports;
  5. using System.Threading;
  6.  
  7. public class TriangleColours : MonoBehaviour
  8. {
  9. public static SerialPort sp = new SerialPort("COM7", 9600);
  10.  
  11. public SpriteRenderer[] Panels;
  12. private Color32[] colours = { new Color32(255, 0, 0, 255), new Color32(50, 0, 0, 255) };
  13.  
  14. public int BaseColour;
  15. public int FirstColour;
  16. public float FirstRed;
  17. public float SecondRed;
  18. public float FirstGreen;
  19. public float SecondGreen;
  20. public float FirstBlue;
  21. public float SecondBlue;
  22.  
  23. public float RedDifference;
  24. public float GreenDifference;
  25. public float BlueDifference;
  26.  
  27. public float steps;
  28.  
  29. public float CurrentRed;
  30.  
  31. public float waitTime;
  32.  
  33. public float timer;
  34.  
  35. public int DoOnce;
  36. public int Setup;
  37.  
  38. public float ASBR; //AddSubtractByRed
  39.  
  40. void Start()
  41. {
  42. OpenConnection();
  43. }
  44.  
  45. void Update()
  46. {
  47. //Debug.Log(DoOnce);
  48. if (DoOnce == 0)
  49. {
  50. if (Setup == 0)
  51. {
  52. timer = 100;
  53. steps = 1000;
  54. BaseColour = Random.Range(0, 1);
  55. FirstColour = Random.Range(0, 2);
  56. while (FirstColour == BaseColour)
  57. {
  58. FirstColour = Random.Range(0, 2);
  59. }
  60. Debug.Log(BaseColour);
  61. Debug.Log(FirstColour);
  62. for (int i = 0; i < 9; i++)
  63. {
  64. Panels[i].color = colours[BaseColour];
  65. }
  66.  
  67. FirstRed = (float)colours[BaseColour].r;
  68. SecondRed = (float)colours[FirstColour].r;
  69. FirstGreen = (float)colours[BaseColour].g;
  70. SecondGreen = (float)colours[FirstColour].g;
  71. FirstBlue = (float)colours[BaseColour].b;
  72. SecondBlue = (float)colours[FirstColour].b;
  73.  
  74. RedDifference = FirstRed - SecondRed;
  75. GreenDifference = FirstGreen - SecondGreen;
  76. BlueDifference = FirstBlue - SecondBlue;
  77. CurrentRed = FirstRed;
  78. ASBR = (RedDifference / steps);
  79.  
  80. Debug.Log(CurrentRed);
  81. Debug.Log(SecondRed);
  82. Debug.Log(ASBR);
  83. Setup = 1;
  84. }
  85. //Debug.Log(timer);
  86. timer++;
  87. if (timer % 100 == 0)
  88. {
  89. //Debug.Log("Ran");
  90. while (CurrentRed != SecondRed)
  91. {
  92. CurrentRed = CurrentRed - (ASBR);
  93. Debug.Log(CurrentRed);
  94.  
  95. //for (int i = 0; i < 9; i++)
  96. //{
  97. // Panels[i].color = new Color32((byte)CurrentRed, 0, 0, 255);
  98. // //Debug.Log(CurrentRed);
  99. //}
  100. }
  101. }
  102.  
  103. //DoOnce = 1;
  104. }
  105. }
  106.  
  107. public void OpenConnection()
  108. {
  109. if(sp != null)
  110. {
  111. if(sp.IsOpen)
  112. {
  113. sp.Close();
  114. Debug.Log("Closing, As It Was Already Open");
  115. }
  116. else
  117. {
  118. sp.Open();
  119. Debug.Log("Port Has Been Opened");
  120. }
  121. }
  122. else
  123. {
  124. if(sp.IsOpen)
  125. {
  126. Debug.Log("Port Is Already Open");
  127. }
  128. else
  129. {
  130. Debug.Log("Port == null");
  131. }
  132. }
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement