sakuragi93
Published © GPL3+

Keep Your Plant Under Control

Use an addressable LED strip to keep your plant under control.

BeginnerFull instructions provided3,145
Keep Your Plant Under Control

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
SparkFun Soil Moisture Sensor (with Screw Terminals)
SparkFun Electronics SparkFun Soil Moisture Sensor (with Screw Terminals)
×1
9V battery (generic)
9V battery (generic)
×1
9V Battery Clip
9V Battery Clip
×1
WS2812 Led Strip
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

SOIL_METER

Code

SOIL METER

C/C++
#include <FastLED.h>

#define LED_PIN     6
#define NUM_LEDS    15
#define BRIGHTNESS  30
#define LED_TYPE    WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];

#define UPDATES_PER_SECOND 100

int analogPin = A0;
int moisture;
int percentage;
//map the max a min values
int map_low = 1023;
int map_high = 350;

void setup() {
  Serial.begin(9600);
  delay( 3000 ); // power-up safety delay
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(  BRIGHTNESS );
  //reset the led strip color
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] =  CRGB::Black;
    FastLED.show();
  }
}

void setColor(int perc) {
  //evaluate the number of leds to turn on (at least 1 for 0%)
  int num = ((NUM_LEDS * perc) / 100) + 1;
  //turn off the leds that are not needed, so the right percentage
  //of the leds will be on
  for (int i = NUM_LEDS; i > num - 1; i--) {
    leds[i] =  CRGB::Black;
    FastLED.show();
  }
  //here's the green 
  if (perc >= 51) {
    for (int i = 0; i < num; i++) {
      leds[i] =  CRGB::Green;
      FastLED.show();
    }
  } else if (perc >= 21 && perc <= 50) {//the yellow
    for (int i = 0; i < num; i++) {
      leds[i] =  CRGB::Yellow;
      FastLED.show();
    }
  } else if (perc <= 20) {
    for (int i = 0; i < num; i++) {//the red
      leds[i] =  CRGB::Red;
      FastLED.show();
    }
  }

}

void loop() {
  //we read raw value to evaluate the percentage
  moisture = analogRead(analogPin);
  Serial.print("Raw: ");
  Serial.print(moisture);

  percentage = map(moisture, map_low, map_high, 0, 100);

  Serial.print(" | Percentage: ");
  Serial.print(percentage);

  Serial.println("%");
  //then we set the color based on the percentage
  setColor(percentage);
  delay(1000);
}

Credits

sakuragi93
0 projects • 1 follower

Comments