Guest User

Interactive Umbrella Code

a guest
Dec 12th, 2019
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.41 KB | None | 0 0
  1. //include LED strip library used to control the LED strips
  2. #include <FastLED.h>
  3.  
  4. //led strip connected to digital 8
  5. #define LED_PIN 8
  6.  
  7. //total number of leds in all strips
  8. #define NUM_LEDS 240
  9.  
  10. //button pin connected to digital 6
  11. const int buttonPin = 6;
  12.  
  13. //Pins for all 4 rain sensors
  14. int RainSensor1 = A0;
  15. int RainDigital1 = 2;
  16. int RainSensor2 = A1;
  17. int RainDigital2 = 3;
  18. int RainSensor3 = A2;
  19. int RainDigital3 = 4;
  20. int RainSensor4 = A3;
  21. int RainDigital4 = 5;
  22.  
  23. // button variables used to keep track of previous and current button state and number of button pushes
  24. int buttonPushCounter = 0; // counter for the number of button presses
  25. int buttonState = 0; // current state of the button
  26. int lastButtonState = 0; // previous state of the button
  27.  
  28. //arrays with prechosen rgb values. Number of colours and colour values may be defined and changed by user
  29. int redVal[] = {255,255,150,0,0};
  30. int greenVal[] = {200,0,0,0,0};
  31. int blueVal[] = {20,0,255,255,0};
  32.  
  33. int arrayLength = (sizeof(redVal)/sizeof(int)) - 1;
  34.  
  35. //initial rgb values (turns off all LEDs because there is no such thing as black light)
  36. int red = 0;
  37. int green = 0;
  38. int blue = 0;
  39.  
  40. //initialize average value calculated from each rain sensor value
  41. int RainVal;
  42.  
  43. //different rain states printed as strings to serial monitor (for testing)
  44. String strRaining;
  45.  
  46.  
  47. //object representing a color in RGB space. Create this object for total number of LEDs in the strip
  48. CRGB leds[NUM_LEDS];
  49.  
  50.  
  51. void setup() {
  52. //define the type of LED strip, the pin for the LEDs and number of LEDs to be used in the sketch
  53.  
  54. FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  55.  
  56. //set rain sensors digital pins to input
  57. pinMode(2,INPUT);
  58. pinMode(3,INPUT);
  59. pinMode(4,INPUT);
  60. pinMode(5,INPUT);
  61.  
  62. //set button pin to input - use inbuilt resistor for button so don't have to connect external resistor
  63. pinMode(buttonPin, INPUT_PULLUP);
  64.  
  65. //switching all leds each time the sketch is run/ before initial button press
  66. ledOFF();
  67.  
  68. //begin the serial monitor
  69. Serial.begin(9600);
  70.  
  71. }
  72.  
  73. void loop() {
  74.  
  75. //set the buttonState based on button pin reading.
  76. buttonState = digitalRead(buttonPin);
  77.  
  78. //if buttonState is not equal to the previous buttonState and if button is pushed (value is LOW), increment through array.
  79.  
  80. if (buttonState != lastButtonState) {
  81.  
  82. if (buttonState == LOW) {
  83. //constrain button pushes between 0 and 4 because 4 is total number of colours in array
  84. int value = constrain(buttonPushCounter,0,4);
  85.  
  86. //print the state of button and each r, g and b value to serial monitor for testing purposes
  87. Serial.println(“button on”);
  88. Serial.print("number of button pushes: “);
  89. Serial.println(buttonPushCounter);
  90.  
  91. Serial.println(redVal[value]);
  92. Serial.println(greenVal[value]);
  93. Serial.println(blueVal[value]);
  94.  
  95. //assign red, green and blue values based on index in array (changed with button press)
  96. red = redVal[value];
  97. green = greenVal[value];
  98. blue = blueVal[value];
  99.  
  100. //increase the button push counter each time
  101. buttonPushCounter++;
  102.  
  103. //if the index value is equal to the array length, set the button Push count back to 0 so we can increment through colour array again
  104. if(value == arrayLength){
  105. buttonPushCounter=0;
  106. }
  107. } else {
  108.  
  109. //if button is not pushed (value is HIGH) print "button off" to serial monitor for testing purposes
  110. Serial.println(“button off");
  111. }
  112. //delay by 50 milliseconds between each button press to avoid floating
  113. delay(50);
  114. }
  115.  
  116. //update the previous button state with current button state
  117. lastButtonState = buttonState;
  118.  
  119.  
  120.  
  121. //reading values from all 4 rain sensors
  122. int RainVal1 = analogRead(RainSensor1);
  123. int RainVal2 = analogRead(RainSensor2);
  124. int RainVal3 = analogRead(RainSensor3);
  125. int RainVal4 = analogRead(RainSensor4);
  126.  
  127. //calculating average
  128. RainVal=((RainVal1+RainVal2+RainVal3+RainVal4)/4);
  129.  
  130. Serial.print(RainVal);
  131.  
  132. //RainVal threshold values determined from testing rain sensor functionality. If calculated RainVal is within a certain range, print the rain state to serial monitor and call relevant function
  133. if(RainVal>1000){
  134. ledOFF();
  135. strRaining = "Not Raining ";
  136. }
  137. else if (RainVal>600 && RainVal<1000){
  138. drizzle();
  139. strRaining = "Drizzling";
  140. }
  141. else if (RainVal>350 && RainVal<600){
  142. rain();
  143. strRaining = "Raining";
  144. }
  145. else if (RainVal<350){
  146. pour();
  147. strRaining = "Raining Heavily";
  148. }
  149.  
  150. //Print current rain state to serial monitor for testing purposes
  151. Serial.print("Raining?: ");
  152. Serial.print(strRaining);
  153. Serial.print(" Moisture Level: ");
  154. Serial.println(RainVal);
  155.  
  156. delay(5);
  157.  
  158. }
  159.  
  160. //drizzle function - less rain
  161. void drizzle(){
  162.  
  163. //create two random LED positions each time the function is called
  164. int randomPos1 = random(0,240);
  165. int randomPos2 = random(0,240);
  166.  
  167. //set the random LEDs colour values based on button push assignment above
  168. leds[randomPos1] = CRGB ( red, green, blue);
  169. leds[randomPos2] = CRGB ( red, green, blue);
  170.  
  171. //turn on LEDs at these two positions
  172. FastLED.show();
  173.  
  174. //keep LEDs on for 300 milliseconds
  175. delay(300);
  176.  
  177. //turn off LEDs at these positions (set colour to black)
  178. leds[randomPos1] = CRGB ( 0, 0, 0);
  179. leds[randomPos2] = CRGB ( 0, 0, 0);
  180. FastLED.show();
  181.  
  182. //turn LEDS off for 300 milliseconds
  183. delay(300);
  184. }
  185.  
  186. //rain function- more rain
  187. void rain(){
  188.  
  189. //create 6 random positions so more LEDs turn on for this function
  190. int randomPos1 = random(0,240);
  191. int randomPos2 = random(0,240);
  192. int randomPos3 = random(0,240);
  193. int randomPos4 = random(0,240);
  194. int randomPos5 = random(0,240);
  195. int randomPos6 = random(0,240);
  196.  
  197. //set the random LEDs colour values based on button push assignment above
  198. leds[randomPos1] = CRGB ( red, green, blue);
  199. leds[randomPos2] = CRGB ( red, green, blue);
  200. leds[randomPos3] = CRGB ( red, green, blue);
  201. leds[randomPos4] = CRGB ( red, green, blue);
  202. leds[randomPos5] = CRGB ( red, green, blue);
  203. leds[randomPos6] = CRGB ( red, green, blue);
  204.  
  205. //turn on LEDs at these six positions
  206. FastLED.show();
  207.  
  208. //keep LEDS on for 100 milliseconds (shorter delay for more sporadic flashing)
  209. delay(100);
  210.  
  211. //turn off LEDs at these positions (set colour to black)
  212. leds[randomPos1] = CRGB ( 0, 0, 0);
  213. leds[randomPos2] = CRGB ( 0, 0, 0);
  214. leds[randomPos3] = CRGB ( 0, 0, 0);
  215. leds[randomPos4] = CRGB ( 0, 0, 0);
  216. leds[randomPos5] = CRGB ( 0, 0, 0);
  217. leds[randomPos6] = CRGB ( 0, 0, 0);
  218. FastLED.show();
  219.  
  220. //turn LEDS off for 100 milliseconds
  221. delay(100);
  222. }
  223.  
  224. //pour function- most rain
  225. void pour(){
  226.  
  227. //create 24 random positions so even more LEDs turn on for this function
  228. int randomPos1 = random(0,240);
  229. int randomPos2 = random(0,240);
  230. int randomPos3 = random(0,240);
  231. int randomPos4 = random(0,240);
  232. int randomPos5 = random(0,240);
  233. int randomPos6 = random(0,240);
  234. int randomPos7 = random(0,240);
  235. int randomPos8 = random(0,240);
  236. int randomPos9 = random(0,240);
  237. int randomPos10 = random(0,240);
  238. int randomPos11 = random(0,240);
  239. int randomPos12= random(0,240);
  240. int randomPos13 = random(0,240);
  241. int randomPos14 = random(0,240);
  242. int randomPos15 = random(0,240);
  243. int randomPos16 = random(0,240);
  244. int randomPos17= random(0,240);
  245. int randomPos18 = random(0,240);
  246. int randomPos19 = random(0,240);
  247. int randomPos20 = random(0,240);
  248. int randomPos21 = random(0,240);
  249. int randomPos22 = random(0,240);
  250. int randomPos23 = random(0,240);
  251. int randomPos24= random(0,240);
  252.  
  253. //set the random LEDs colour values based on button push assignment above
  254. leds[randomPos1] = CRGB ( red, green, blue);
  255. leds[randomPos2] = CRGB ( red, green, blue);
  256. leds[randomPos3] = CRGB ( red, green, blue);
  257. leds[randomPos4] = CRGB ( red, green, blue);
  258. leds[randomPos5] = CRGB ( red, green, blue);
  259. leds[randomPos6] = CRGB ( red, green, blue);
  260. leds[randomPos7] = CRGB ( red, green, blue);
  261. leds[randomPos8] = CRGB ( red, green, blue);
  262. leds[randomPos9] = CRGB ( red, green, blue);
  263. leds[randomPos10] = CRGB ( red, green, blue);
  264. leds[randomPos11] = CRGB ( red, green, blue);
  265. leds[randomPos12] = CRGB ( red, green, blue);
  266. leds[randomPos13] = CRGB ( red, green, blue);
  267. leds[randomPos14] = CRGB ( red, green, blue);
  268. leds[randomPos15] = CRGB ( red, green, blue);
  269. leds[randomPos16] = CRGB ( red, green, blue);
  270. leds[randomPos17] = CRGB ( red, green, blue);
  271. leds[randomPos18] = CRGB ( red, green, blue);
  272. leds[randomPos19] = CRGB ( red, green, blue);
  273. leds[randomPos20] = CRGB ( red, green, blue);
  274. leds[randomPos21] = CRGB ( red, green, blue);
  275. leds[randomPos22] = CRGB ( red, green, blue);
  276. leds[randomPos23] = CRGB ( red, green, blue);
  277. leds[randomPos24] = CRGB ( red, green, blue);
  278.  
  279. //turn on LEDs at these 24 positions
  280. FastLED.show();
  281.  
  282. //keep LEDs on for 35 milliseconds (really short delay for really intense flashing)
  283. delay(35);
  284.  
  285. //turn off LEDs at these positions (set colour to black)
  286. leds[randomPos1] = CRGB ( 0, 0, 0);
  287. leds[randomPos2] = CRGB ( 0, 0, 0);
  288. leds[randomPos3] = CRGB ( 0, 0, 0);
  289. leds[randomPos4] = CRGB ( 0, 0, 0);
  290. leds[randomPos5] = CRGB ( 0, 0, 0);
  291. leds[randomPos6] = CRGB ( 0, 0, 0);
  292. leds[randomPos7] = CRGB ( 0, 0, 0);
  293. leds[randomPos8] = CRGB ( 0, 0, 0);
  294. leds[randomPos9] = CRGB ( 0, 0, 0);
  295. leds[randomPos10] = CRGB ( 0, 0, 0);
  296. leds[randomPos11] = CRGB ( 0, 0, 0);
  297. leds[randomPos12] = CRGB ( 0, 0, 0);
  298. leds[randomPos13] = CRGB ( 0, 0, 0);
  299. leds[randomPos14] = CRGB ( 0, 0, 0);
  300. leds[randomPos15] = CRGB ( 0, 0, 0);
  301. leds[randomPos16] = CRGB ( 0, 0, 0);
  302. leds[randomPos17] = CRGB ( 0, 0, 0);
  303. leds[randomPos18] = CRGB ( 0, 0, 0);
  304. leds[randomPos19] = CRGB ( 0, 0, 0);
  305. leds[randomPos20] = CRGB ( 0, 0, 0);
  306. leds[randomPos21] = CRGB ( 0, 0, 0);
  307. leds[randomPos22] = CRGB ( 0, 0, 0);
  308. leds[randomPos23] = CRGB ( 0, 0, 0);
  309. leds[randomPos24] = CRGB ( 0, 0, 0);
  310. FastLED.show();
  311.  
  312. //turn LEDS off for 35 milliseconds
  313. delay(35);
  314. }
  315.  
  316. //switch off all leds function (set all r,g and b colours to 0)
  317. void ledOFF(){
  318. for (int i = 0; i <=239; i++){
  319. leds[i] = CRGB(0,0,0);
  320. }
  321. }
Advertisement
Add Comment
Please, Sign In to add comment