// https://hackaday.io/project/187372-octoplant-soil-moisture-sensor
#include <Adafruit_NeoPixel.h>
#define pixel_pin 4
#define moisturePin A3
#define wetCal 750 //Wet and dry calibration limits
#define dryCal 1023
int measurements [10]; //Array to store the last 10 measurements
int counter = 0; //Counter to keep track of the current array position
//=======================================================================================
class Strip {
public:
uint8_t effect;
uint8_t effects;
uint16_t effStep;
unsigned long effStart;
Adafruit_NeoPixel strip;
Strip(uint16_t leds, uint8_t pin, uint8_t toteffects, uint16_t striptype) : strip(leds, pin, striptype) {
effect = -1;
effects = toteffects;
Reset();
}
void Reset() {
effStep = 0;
effect = (effect + 1) % effects;
effStart = millis();
}
};
//=======================================================================================
struct Loop {
uint8_t currentChild;
uint8_t childs;
bool timeBased;
uint16_t cycles;
uint16_t currentTime;
Loop(uint8_t totchilds, bool timebased, uint16_t tottime) {
currentTime = 0;
currentChild = 0;
childs = totchilds;
timeBased = timebased;
cycles = tottime;
}
};
//=======================================================================================
Strip strip_0(1, pixel_pin, 8, NEO_GRB + NEO_KHZ800);
struct Loop strip0loop0(7, false, 1);
//[GLOBAL_VARIABLES]
//=======================================================================================
void setup() {
for (int i=0; i<=9 ; i++) //Populate the array with starting readings from the sensor
measurements [i] = analogRead(moisturePin);
strip_0.strip.begin();
}
//=======================================================================================
void loop()
{
addData(analogRead(moisturePin)); //Take a new reading and add it to the array
int moistureLevel = getAverage(); //Get the new average of array readings
if(moistureLevel > dryCal) //Constrain reading between the high and low limits
moistureLevel = dryCal;
else if(moistureLevel < wetCal)
moistureLevel = wetCal;
int greenBrightness = map(moistureLevel, dryCal, wetCal, 0, 255); //Calculate the green and red LED brightness according to the reading
int redBrightness = map(moistureLevel, wetCal, dryCal, 0, 255);
//analogWrite(greenLED, greenBrightness); //Set the green and red LED brightness
//analogWrite(redLED, redBrightness);
strips_loop();
delay(2000);
}
//=======================================================================================
void addData(int num) //Function to add a reading to the array and increment the counter
{
measurements [counter] = num;
if (counter < 9)
counter++;
else
counter = 0;
}
//=======================================================================================
int getAverage() //Function to return the average of all readings in the array
{
int ave = 0;
for(int i=0; i<=9 ; i++)
ave = ave + measurements [i];
ave = ave/10;
return ave;
}
//=======================================================================================
void strips_loop() {
if (strip0_loop0() & 0x01)
strip_0.strip.show();
}
//=======================================================================================
uint8_t strip0_loop0() {
uint8_t ret = 0x00;
switch (strip0loop0.currentChild) {
case 0:
ret = strip0_loop0_eff0(); break;
case 1:
ret = strip0_loop0_eff1(); break;
case 2:
ret = strip0_loop0_eff2(); break;
case 3:
ret = strip0_loop0_eff3(); break;
case 4:
ret = strip0_loop0_eff4(); break;
case 5:
ret = strip0_loop0_eff5(); break;
case 6:
ret = strip0_loop0_eff6(); break;
}
if (ret & 0x02) {
ret &= 0xfd;
if (strip0loop0.currentChild + 1 >= strip0loop0.childs) {
strip0loop0.currentChild = 0;
if (++strip0loop0.currentTime >= strip0loop0.cycles) {
strip0loop0.currentTime = 0;
ret |= 0x02;
}
}
else {
strip0loop0.currentChild++;
}
};
return ret;
}
//=======================================================================================
uint8_t strip0_loop0_eff0() {
// Strip ID: 0 - Effect: Fade - LEDS: 8
// Steps: 99.3 - Delay: 10
// Colors: 2 (255.0.0, 0.255.0)
// Options: duration=993, every=1,
if (millis() - strip_0.effStart < 10 * (strip_0.effStep)) return 0x00;
uint8_t r, g, b;
double e;
e = (strip_0.effStep * 10) / (double)993;
r = ( e ) * 0 + 255 * ( 1.0 - e );
g = ( e ) * 255 + 0 * ( 1.0 - e );
b = ( e ) * 0 + 0 * ( 1.0 - e );
for (uint16_t j = 0; j < 8; j++) {
if ((j % 1) == 0)
strip_0.strip.setPixelColor(j, r, g, b);
else
strip_0.strip.setPixelColor(j, 0, 0, 0);
}
if (strip_0.effStep >= 99.3) {
strip_0.Reset();
return 0x03;
}
else strip_0.effStep++;
return 0x01;
}
//=======================================================================================
uint8_t strip0_loop0_eff1() {
// Strip ID: 0 - Effect: Fade - LEDS: 8
// Steps: 99.3 - Delay: 10
// Colors: 2 (0.255.0, 0.0.255)
// Options: duration=993, every=1,
if (millis() - strip_0.effStart < 10 * (strip_0.effStep)) return 0x00;
uint8_t r, g, b;
double e;
e = (strip_0.effStep * 10) / (double)993;
r = ( e ) * 0 + 0 * ( 1.0 - e );
g = ( e ) * 0 + 255 * ( 1.0 - e );
b = ( e ) * 255 + 0 * ( 1.0 - e );
for (uint16_t j = 0; j < 8; j++) {
if ((j % 1) == 0)
strip_0.strip.setPixelColor(j, r, g, b);
else
strip_0.strip.setPixelColor(j, 0, 0, 0);
}
if (strip_0.effStep >= 99.3) {
strip_0.Reset();
return 0x03;
}
else strip_0.effStep++;
return 0x01;
}
//=======================================================================================
uint8_t strip0_loop0_eff2() {
// Strip ID: 0 - Effect: Fade - LEDS: 8
// Steps: 99.6 - Delay: 10
// Colors: 2 (0.0.255, 255.255.0)
// Options: duration=996, every=1,
if (millis() - strip_0.effStart < 10 * (strip_0.effStep)) return 0x00;
uint8_t r, g, b;
double e;
e = (strip_0.effStep * 10) / (double)996;
r = ( e ) * 255 + 0 * ( 1.0 - e );
g = ( e ) * 255 + 0 * ( 1.0 - e );
b = ( e ) * 0 + 255 * ( 1.0 - e );
for (uint16_t j = 0; j < 8; j++) {
if ((j % 1) == 0)
strip_0.strip.setPixelColor(j, r, g, b);
else
strip_0.strip.setPixelColor(j, 0, 0, 0);
}
if (strip_0.effStep >= 99.6) {
strip_0.Reset();
return 0x03;
}
else strip_0.effStep++;
return 0x01;
}
//=======================================================================================
uint8_t strip0_loop0_eff3() {
// Strip ID: 0 - Effect: Fade - LEDS: 8
// Steps: 99.6 - Delay: 10
// Colors: 2 (255.255.0, 0.255.255)
// Options: duration=996, every=1,
if (millis() - strip_0.effStart < 10 * (strip_0.effStep)) return 0x00;
uint8_t r, g, b;
double e;
e = (strip_0.effStep * 10) / (double)996;
r = ( e ) * 0 + 255 * ( 1.0 - e );
g = ( e ) * 255 + 255 * ( 1.0 - e );
b = ( e ) * 255 + 0 * ( 1.0 - e );
for (uint16_t j = 0; j < 8; j++) {
if ((j % 1) == 0)
strip_0.strip.setPixelColor(j, r, g, b);
else
strip_0.strip.setPixelColor(j, 0, 0, 0);
}
if (strip_0.effStep >= 99.6) {
strip_0.Reset();
return 0x03;
}
else strip_0.effStep++;
return 0x01;
}
//=======================================================================================
uint8_t strip0_loop0_eff4() {
// Strip ID: 0 - Effect: Fade - LEDS: 8
// Steps: 99.6 - Delay: 10
// Colors: 2 (0.255.255, 255.0.255)
// Options: duration=996, every=1,
if (millis() - strip_0.effStart < 10 * (strip_0.effStep)) return 0x00;
uint8_t r, g, b;
double e;
e = (strip_0.effStep * 10) / (double)996;
r = ( e ) * 255 + 0 * ( 1.0 - e );
g = ( e ) * 0 + 255 * ( 1.0 - e );
b = ( e ) * 255 + 255 * ( 1.0 - e );
for (uint16_t j = 0; j < 8; j++) {
if ((j % 1) == 0)
strip_0.strip.setPixelColor(j, r, g, b);
else
strip_0.strip.setPixelColor(j, 0, 0, 0);
}
if (strip_0.effStep >= 99.6) {
strip_0.Reset();
return 0x03;
}
else strip_0.effStep++;
return 0x01;
}
//=======================================================================================
uint8_t strip0_loop0_eff5() {
// Strip ID: 0 - Effect: Fade - LEDS: 8
// Steps: 99.6 - Delay: 10
// Colors: 2 (255.0.255, 255.255.255)
// Options: duration=996, every=1,
if (millis() - strip_0.effStart < 10 * (strip_0.effStep)) return 0x00;
uint8_t r, g, b;
double e;
e = (strip_0.effStep * 10) / (double)996;
r = ( e ) * 255 + 255 * ( 1.0 - e );
g = ( e ) * 255 + 0 * ( 1.0 - e );
b = ( e ) * 255 + 255 * ( 1.0 - e );
for (uint16_t j = 0; j < 8; j++) {
if ((j % 1) == 0)
strip_0.strip.setPixelColor(j, r, g, b);
else
strip_0.strip.setPixelColor(j, 0, 0, 0);
}
if (strip_0.effStep >= 99.6) {
strip_0.Reset();
return 0x03;
}
else strip_0.effStep++;
return 0x01;
}
//=======================================================================================
uint8_t strip0_loop0_eff6() {
// Strip ID: 0 - Effect: Fade - LEDS: 8
// Steps: 99.6 - Delay: 10
// Colors: 2 (255.255.255, 255.0.0)
// Options: duration=996, every=1,
if (millis() - strip_0.effStart < 10 * (strip_0.effStep)) return 0x00;
uint8_t r, g, b;
double e;
e = (strip_0.effStep * 10) / (double)996;
r = ( e ) * 255 + 255 * ( 1.0 - e );
g = ( e ) * 0 + 255 * ( 1.0 - e );
b = ( e ) * 0 + 255 * ( 1.0 - e );
for (uint16_t j = 0; j < 8; j++) {
if ((j % 1) == 0)
strip_0.strip.setPixelColor(j, r, g, b);
else
strip_0.strip.setPixelColor(j, 0, 0, 0);
}
if (strip_0.effStep >= 99.6) {
strip_0.Reset();
return 0x03;
}
else strip_0.effStep++;
return 0x01;
}
//=======================================================================================