Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //include LED strip library used to control the LED strips
- #include <FastLED.h>
- //led strip connected to digital 8
- #define LED_PIN 8
- //total number of leds in all strips
- #define NUM_LEDS 240
- //button pin connected to digital 6
- const int buttonPin = 6;
- //Pins for all 4 rain sensors
- int RainSensor1 = A0;
- int RainDigital1 = 2;
- int RainSensor2 = A1;
- int RainDigital2 = 3;
- int RainSensor3 = A2;
- int RainDigital3 = 4;
- int RainSensor4 = A3;
- int RainDigital4 = 5;
- // button variables used to keep track of previous and current button state and number of button pushes
- int buttonPushCounter = 0; // counter for the number of button presses
- int buttonState = 0; // current state of the button
- int lastButtonState = 0; // previous state of the button
- //arrays with prechosen rgb values. Number of colours and colour values may be defined and changed by user
- int redVal[] = {255,255,150,0,0};
- int greenVal[] = {200,0,0,0,0};
- int blueVal[] = {20,0,255,255,0};
- int arrayLength = (sizeof(redVal)/sizeof(int)) - 1;
- //initial rgb values (turns off all LEDs because there is no such thing as black light)
- int red = 0;
- int green = 0;
- int blue = 0;
- //initialize average value calculated from each rain sensor value
- int RainVal;
- //different rain states printed as strings to serial monitor (for testing)
- String strRaining;
- //object representing a color in RGB space. Create this object for total number of LEDs in the strip
- CRGB leds[NUM_LEDS];
- void setup() {
- //define the type of LED strip, the pin for the LEDs and number of LEDs to be used in the sketch
- FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
- //set rain sensors digital pins to input
- pinMode(2,INPUT);
- pinMode(3,INPUT);
- pinMode(4,INPUT);
- pinMode(5,INPUT);
- //set button pin to input - use inbuilt resistor for button so don't have to connect external resistor
- pinMode(buttonPin, INPUT_PULLUP);
- //switching all leds each time the sketch is run/ before initial button press
- ledOFF();
- //begin the serial monitor
- Serial.begin(9600);
- }
- void loop() {
- //set the buttonState based on button pin reading.
- buttonState = digitalRead(buttonPin);
- //if buttonState is not equal to the previous buttonState and if button is pushed (value is LOW), increment through array.
- if (buttonState != lastButtonState) {
- if (buttonState == LOW) {
- //constrain button pushes between 0 and 4 because 4 is total number of colours in array
- int value = constrain(buttonPushCounter,0,4);
- //print the state of button and each r, g and b value to serial monitor for testing purposes
- Serial.println(“button on”);
- Serial.print("number of button pushes: “);
- Serial.println(buttonPushCounter);
- Serial.println(redVal[value]);
- Serial.println(greenVal[value]);
- Serial.println(blueVal[value]);
- //assign red, green and blue values based on index in array (changed with button press)
- red = redVal[value];
- green = greenVal[value];
- blue = blueVal[value];
- //increase the button push counter each time
- buttonPushCounter++;
- //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
- if(value == arrayLength){
- buttonPushCounter=0;
- }
- } else {
- //if button is not pushed (value is HIGH) print "button off" to serial monitor for testing purposes
- Serial.println(“button off");
- }
- //delay by 50 milliseconds between each button press to avoid floating
- delay(50);
- }
- //update the previous button state with current button state
- lastButtonState = buttonState;
- //reading values from all 4 rain sensors
- int RainVal1 = analogRead(RainSensor1);
- int RainVal2 = analogRead(RainSensor2);
- int RainVal3 = analogRead(RainSensor3);
- int RainVal4 = analogRead(RainSensor4);
- //calculating average
- RainVal=((RainVal1+RainVal2+RainVal3+RainVal4)/4);
- Serial.print(RainVal);
- //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
- if(RainVal>1000){
- ledOFF();
- strRaining = "Not Raining ";
- }
- else if (RainVal>600 && RainVal<1000){
- drizzle();
- strRaining = "Drizzling";
- }
- else if (RainVal>350 && RainVal<600){
- rain();
- strRaining = "Raining";
- }
- else if (RainVal<350){
- pour();
- strRaining = "Raining Heavily";
- }
- //Print current rain state to serial monitor for testing purposes
- Serial.print("Raining?: ");
- Serial.print(strRaining);
- Serial.print(" Moisture Level: ");
- Serial.println(RainVal);
- delay(5);
- }
- //drizzle function - less rain
- void drizzle(){
- //create two random LED positions each time the function is called
- int randomPos1 = random(0,240);
- int randomPos2 = random(0,240);
- //set the random LEDs colour values based on button push assignment above
- leds[randomPos1] = CRGB ( red, green, blue);
- leds[randomPos2] = CRGB ( red, green, blue);
- //turn on LEDs at these two positions
- FastLED.show();
- //keep LEDs on for 300 milliseconds
- delay(300);
- //turn off LEDs at these positions (set colour to black)
- leds[randomPos1] = CRGB ( 0, 0, 0);
- leds[randomPos2] = CRGB ( 0, 0, 0);
- FastLED.show();
- //turn LEDS off for 300 milliseconds
- delay(300);
- }
- //rain function- more rain
- void rain(){
- //create 6 random positions so more LEDs turn on for this function
- int randomPos1 = random(0,240);
- int randomPos2 = random(0,240);
- int randomPos3 = random(0,240);
- int randomPos4 = random(0,240);
- int randomPos5 = random(0,240);
- int randomPos6 = random(0,240);
- //set the random LEDs colour values based on button push assignment above
- leds[randomPos1] = CRGB ( red, green, blue);
- leds[randomPos2] = CRGB ( red, green, blue);
- leds[randomPos3] = CRGB ( red, green, blue);
- leds[randomPos4] = CRGB ( red, green, blue);
- leds[randomPos5] = CRGB ( red, green, blue);
- leds[randomPos6] = CRGB ( red, green, blue);
- //turn on LEDs at these six positions
- FastLED.show();
- //keep LEDS on for 100 milliseconds (shorter delay for more sporadic flashing)
- delay(100);
- //turn off LEDs at these positions (set colour to black)
- leds[randomPos1] = CRGB ( 0, 0, 0);
- leds[randomPos2] = CRGB ( 0, 0, 0);
- leds[randomPos3] = CRGB ( 0, 0, 0);
- leds[randomPos4] = CRGB ( 0, 0, 0);
- leds[randomPos5] = CRGB ( 0, 0, 0);
- leds[randomPos6] = CRGB ( 0, 0, 0);
- FastLED.show();
- //turn LEDS off for 100 milliseconds
- delay(100);
- }
- //pour function- most rain
- void pour(){
- //create 24 random positions so even more LEDs turn on for this function
- int randomPos1 = random(0,240);
- int randomPos2 = random(0,240);
- int randomPos3 = random(0,240);
- int randomPos4 = random(0,240);
- int randomPos5 = random(0,240);
- int randomPos6 = random(0,240);
- int randomPos7 = random(0,240);
- int randomPos8 = random(0,240);
- int randomPos9 = random(0,240);
- int randomPos10 = random(0,240);
- int randomPos11 = random(0,240);
- int randomPos12= random(0,240);
- int randomPos13 = random(0,240);
- int randomPos14 = random(0,240);
- int randomPos15 = random(0,240);
- int randomPos16 = random(0,240);
- int randomPos17= random(0,240);
- int randomPos18 = random(0,240);
- int randomPos19 = random(0,240);
- int randomPos20 = random(0,240);
- int randomPos21 = random(0,240);
- int randomPos22 = random(0,240);
- int randomPos23 = random(0,240);
- int randomPos24= random(0,240);
- //set the random LEDs colour values based on button push assignment above
- leds[randomPos1] = CRGB ( red, green, blue);
- leds[randomPos2] = CRGB ( red, green, blue);
- leds[randomPos3] = CRGB ( red, green, blue);
- leds[randomPos4] = CRGB ( red, green, blue);
- leds[randomPos5] = CRGB ( red, green, blue);
- leds[randomPos6] = CRGB ( red, green, blue);
- leds[randomPos7] = CRGB ( red, green, blue);
- leds[randomPos8] = CRGB ( red, green, blue);
- leds[randomPos9] = CRGB ( red, green, blue);
- leds[randomPos10] = CRGB ( red, green, blue);
- leds[randomPos11] = CRGB ( red, green, blue);
- leds[randomPos12] = CRGB ( red, green, blue);
- leds[randomPos13] = CRGB ( red, green, blue);
- leds[randomPos14] = CRGB ( red, green, blue);
- leds[randomPos15] = CRGB ( red, green, blue);
- leds[randomPos16] = CRGB ( red, green, blue);
- leds[randomPos17] = CRGB ( red, green, blue);
- leds[randomPos18] = CRGB ( red, green, blue);
- leds[randomPos19] = CRGB ( red, green, blue);
- leds[randomPos20] = CRGB ( red, green, blue);
- leds[randomPos21] = CRGB ( red, green, blue);
- leds[randomPos22] = CRGB ( red, green, blue);
- leds[randomPos23] = CRGB ( red, green, blue);
- leds[randomPos24] = CRGB ( red, green, blue);
- //turn on LEDs at these 24 positions
- FastLED.show();
- //keep LEDs on for 35 milliseconds (really short delay for really intense flashing)
- delay(35);
- //turn off LEDs at these positions (set colour to black)
- leds[randomPos1] = CRGB ( 0, 0, 0);
- leds[randomPos2] = CRGB ( 0, 0, 0);
- leds[randomPos3] = CRGB ( 0, 0, 0);
- leds[randomPos4] = CRGB ( 0, 0, 0);
- leds[randomPos5] = CRGB ( 0, 0, 0);
- leds[randomPos6] = CRGB ( 0, 0, 0);
- leds[randomPos7] = CRGB ( 0, 0, 0);
- leds[randomPos8] = CRGB ( 0, 0, 0);
- leds[randomPos9] = CRGB ( 0, 0, 0);
- leds[randomPos10] = CRGB ( 0, 0, 0);
- leds[randomPos11] = CRGB ( 0, 0, 0);
- leds[randomPos12] = CRGB ( 0, 0, 0);
- leds[randomPos13] = CRGB ( 0, 0, 0);
- leds[randomPos14] = CRGB ( 0, 0, 0);
- leds[randomPos15] = CRGB ( 0, 0, 0);
- leds[randomPos16] = CRGB ( 0, 0, 0);
- leds[randomPos17] = CRGB ( 0, 0, 0);
- leds[randomPos18] = CRGB ( 0, 0, 0);
- leds[randomPos19] = CRGB ( 0, 0, 0);
- leds[randomPos20] = CRGB ( 0, 0, 0);
- leds[randomPos21] = CRGB ( 0, 0, 0);
- leds[randomPos22] = CRGB ( 0, 0, 0);
- leds[randomPos23] = CRGB ( 0, 0, 0);
- leds[randomPos24] = CRGB ( 0, 0, 0);
- FastLED.show();
- //turn LEDS off for 35 milliseconds
- delay(35);
- }
- //switch off all leds function (set all r,g and b colours to 0)
- void ledOFF(){
- for (int i = 0; i <=239; i++){
- leds[i] = CRGB(0,0,0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment