Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. //PINS
  2. int r = 9;
  3. int g = 10;
  4. int b = 11;
  5.  
  6. int speaker = 4;
  7.  
  8. int button = 7;
  9. //ANALOG PINS
  10. int photoResistor = 0;
  11. int moisture = 1;
  12. int temperature = 2;
  13.  
  14. //SENSOR INFO
  15. int currentLight;
  16. int currentTemp;
  17. int currentWater;
  18.  
  19. //PRIME PLANT INFO
  20. int waterGood;
  21. int lightGood;
  22. int tempGood;
  23.  
  24. //LOOKING FOR ANY PROBLEMS
  25. bool lightProb = false;
  26. bool tempProb = false;
  27. bool moisProb = false;
  28.  
  29. void setup() {
  30. //Initializing pins
  31. pinMode(r, OUTPUT);
  32. pinMode(g, OUTPUT);
  33. pinMode(b, OUTPUT);
  34. pinMode(speaker, OUTPUT);
  35.  
  36. Serial.begin(9600);
  37.  
  38. //wait for the person to move away from all sensors
  39. delay(1000);
  40. //set the current values as the optimum.
  41. tempGood = analogRead(temperature);
  42. lightGood = analogRead(photoResistor);
  43. waterGood = analogRead(moisture);
  44. }
  45.  
  46. void loop() {
  47. if (digitalRead(button) == 1) { //if the user presses the button the set the current values as the optimum values
  48. tempGood = analogRead(temperature);
  49. lightGood = analogRead(photoResistor);
  50. waterGood = analogRead(moisture);
  51. }
  52. currentTemp = analogRead(temperature); //read the temp and set to a variable
  53. if (currentTemp > (tempGood + 5)) { //to hot
  54. orange(); //orange light with delay and alarm
  55.  
  56. noTone(speaker); //stop the speaker and wait
  57. delay(300);
  58.  
  59. red(); //red light with delay and alarm
  60.  
  61. noTone(speaker);
  62. delay(300);
  63. tempProb = true; //Let the program know there is a problem
  64. }
  65. else if (currentTemp < (tempGood - 5)) { //if its too cold
  66. orange(); //orange light with delay and alarm
  67.  
  68. noTone(speaker); //stop the speaker and wait
  69. delay(300);
  70.  
  71. blue(); //blue light with delay and alarm
  72.  
  73. noTone(speaker); //stop the speaker and wait
  74. delay(300);
  75. tempProb = true; //let the program know there is a problem
  76. }
  77. else { //if it's not too hot, nor too cold
  78. tempProb = false; //then there is no problem
  79. }
  80.  
  81. currentLight = analogRead(photoResistor); //read the photoresistor and set it to a bariable
  82. if (currentLight > (lightGood + 100)) { //too bright
  83. yellow(); //yellow light
  84.  
  85. noTone(speaker); //stop speaker
  86. delay(300);
  87.  
  88. red(); //red light
  89.  
  90. noTone(speaker); //stop speaker
  91. delay(300);
  92. lightProb = true; //problem
  93. }
  94.  
  95. else if (currentLight < (lightGood - 100)) { //too dark
  96. yellow(); //yellow light
  97.  
  98. noTone(speaker); //stop speaker
  99. delay(300);
  100.  
  101. green(); //green light
  102.  
  103. noTone(speaker); //stop speaker
  104. delay(300);
  105. lightProb = true; //problem
  106. }
  107. else { //good light level
  108. lightProb = false; //no problem
  109. }
  110.  
  111. currentWater = analogRead(moisture); //read the moisture sensor
  112. if (currentWater > (waterGood + 100)) { //too much water
  113. blue(); //blue light
  114.  
  115. noTone(speaker); //stop speaker
  116. delay(300);
  117.  
  118. red(); //red light
  119.  
  120. noTone(speaker); //stop speaker
  121. delay(300);
  122. moisProb = true; //problem
  123. }
  124.  
  125. else if (currentWater < (waterGood - 100)) { //too little water
  126. blue(); //blue light
  127.  
  128. noTone(speaker); //stop speaker
  129. delay(300);
  130.  
  131. green(); //green light
  132.  
  133. noTone(speaker); //stop speaker
  134. delay(300);
  135. moisProb = true; //problem
  136. }
  137. else { //if the water level is good
  138. moisProb = false; //no problem
  139. }
  140.  
  141. if (!moisProb && !lightProb && !tempProb) { //if there are no problems and everthing is dandy
  142. //turn on the green light
  143. analogWrite(r, 255);
  144. analogWrite(g, 0);
  145. analogWrite(b, 255);
  146. }
  147. }
  148.  
  149. void red() { //set up red light
  150. tone(speaker, 523); //turn the speaker on
  151.  
  152. analogWrite(r, 0); //turn the red led on
  153. analogWrite(g, 255);
  154. analogWrite(b, 255);
  155.  
  156. delay(750); //wait 750 milliseconds
  157. }
  158. void green() { //set up green light
  159. tone(speaker, 523); //turn on the speaker
  160.  
  161. analogWrite(r, 255);
  162. analogWrite(g, 0); //turn on green light
  163. analogWrite(b, 255);
  164.  
  165. delay(750); //wait 750 milliseconds
  166. }
  167. void blue() { //set up blue light
  168. tone(speaker, 523); //turn on the speaker
  169.  
  170. analogWrite(r, 255);
  171. analogWrite(g, 255);
  172. analogWrite(b, 0); //turn on the blue light
  173.  
  174. delay(750); //wait 750 milliseconds
  175. }
  176. void orange() { //set up the orange light
  177. tone(speaker, 523); //turn on the speaker
  178.  
  179. analogWrite(r, 0); //turn on the red light
  180. analogWrite(g, 200); //slightly turn on the green to blend to orange
  181. analogWrite(b, 255);
  182.  
  183. delay(750); //wait 750 milliseconds
  184. }
  185. void yellow() { //set up the yellow light
  186. tone(speaker, 523); //turn on the speaker
  187.  
  188. analogWrite(r, 0); //turn the the red light
  189. analogWrite(g, 0); //turn on the green light and they will blend into yellow
  190. analogWrite(b, 255);
  191.  
  192. delay(750); //wait 750 milliseconds
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement