1#include <FastLED.h>
2
3#define LED_PIN 6
4#define NUM_LEDS 15
5#define BRIGHTNESS 30
6#define LED_TYPE WS2812
7#define COLOR_ORDER GRB
8CRGB leds[NUM_LEDS];
9
10#define UPDATES_PER_SECOND 100
11
12int analogPin = A0;
13int moisture;
14int percentage;
15
16int map_low = 1023;
17int map_high = 350;
18
19void setup() {
20 Serial.begin(9600);
21 delay( 3000 );
22 FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
23 FastLED.setBrightness( BRIGHTNESS );
24
25 for (int i = 0; i < NUM_LEDS; i++) {
26 leds[i] = CRGB::Black;
27 FastLED.show();
28 }
29}
30
31void setColor(int perc) {
32
33 int num = ((NUM_LEDS * perc) / 100) + 1;
34
35
36 for (int i = NUM_LEDS; i > num - 1; i--) {
37 leds[i] = CRGB::Black;
38 FastLED.show();
39 }
40
41 if (perc >= 51) {
42 for (int i = 0; i < num; i++) {
43 leds[i] = CRGB::Green;
44 FastLED.show();
45 }
46 } else if (perc >= 21 && perc <= 50) {
47 for (int i = 0; i < num; i++) {
48 leds[i] = CRGB::Yellow;
49 FastLED.show();
50 }
51 } else if (perc <= 20) {
52 for (int i = 0; i < num; i++) {
53 leds[i] = CRGB::Red;
54 FastLED.show();
55 }
56 }
57
58}
59
60void loop() {
61
62 moisture = analogRead(analogPin);
63 Serial.print("Raw: ");
64 Serial.print(moisture);
65
66 percentage = map(moisture, map_low, map_high, 0, 100);
67
68 Serial.print(" | Percentage: ");
69 Serial.print(percentage);
70
71 Serial.println("%");
72
73 setColor(percentage);
74 delay(1000);
75}
76