Created
August 5, 2020 18:05
-
-
Save squidbits/6929b611f4e5318b9c56f7e829c35ba3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <FastLED.h> | |
#define CHECK_TIME 2000 | |
#define PUMP_1 D7 | |
#define PUMP_2 D6 | |
#define PUMP_3 D5 | |
#define PUMP_4 D0 | |
#define LED D4 | |
#define SOIL_SENSOR_INPUT A0 | |
#define SOIL_SENSOR_1 D2 | |
#define SOIL_SENSOR_2 D3 | |
#define NUM_LEDS 4 | |
#define BRIGHTNESS 32 | |
#define PUMP_ON HIGH | |
#define PUMP_OFF LOW | |
#define MOISTURE_LIMIT 300 | |
unsigned int lastMillis = 0; | |
unsigned int lastSoilMillis = 0; | |
int pumps = 4; | |
bool pumpState[] = { LOW, LOW, LOW, LOW }; | |
char pumpPin[] = { PUMP_1, PUMP_2, PUMP_3, PUMP_4 }; | |
int sensors = 2; | |
char sensorPin[] = { SOIL_SENSOR_1, SOIL_SENSOR_2 }; | |
int soilMoisture[] = { 0, 0 }; | |
CRGB leds[NUM_LEDS]; | |
void setup() { | |
Serial.begin(9600); // open serial port, set the baud rate as 9600 bps | |
Serial.println("Init"); | |
pinMode(PUMP_1, OUTPUT); | |
pinMode(PUMP_2, OUTPUT); | |
pinMode(PUMP_3, OUTPUT); | |
pinMode(PUMP_4, OUTPUT); | |
pinMode(SOIL_SENSOR_1, OUTPUT); | |
pinMode(SOIL_SENSOR_2, OUTPUT); | |
// set sensor pins low. | |
digitalWrite(SOIL_SENSOR_1, LOW); | |
digitalWrite(SOIL_SENSOR_2, LOW); | |
for (int i = 0; i < pumps; i++) { | |
change_pump_state(i); | |
} | |
FastLED.addLeds<WS2812B, LED, GRB>(leds, NUM_LEDS); | |
FastLED.setBrightness(BRIGHTNESS); | |
for (int i = 0; i < NUM_LEDS; i++) { | |
leds[i] = CRGB::Black; | |
} | |
FastLED.show(); | |
delay(1000); //give it a second | |
} | |
void loop() { | |
unsigned long currentMillis = millis(); | |
if (currentMillis - lastSoilMillis > CHECK_TIME) { | |
for (int i = 0; i < sensors; i++) { | |
soilMoisture[i] = check_soil_moisture(i); | |
} | |
lastSoilMillis = currentMillis; | |
} | |
for (int i = 0; i < sensors; i++) { | |
if (soilMoisture[i] <= MOISTURE_LIMIT && pumpState[i] == PUMP_OFF) { | |
change_pump_state(i); | |
} | |
if (soilMoisture[i] > MOISTURE_LIMIT && pumpState[i] == PUMP_ON) { | |
change_pump_state(i); | |
} | |
} | |
FastLED.show(); | |
} | |
int check_soil_moisture(int sensor) { | |
digitalWrite(sensorPin[sensor], HIGH); | |
delay(20); //let the thing power on | |
int value = analogRead(SOIL_SENSOR_INPUT); | |
digitalWrite(sensorPin[sensor], LOW); | |
delay(20 ); //let the thing clear | |
return value; | |
} | |
void change_pump_state(int pump) { | |
pumpState[pump] = !pumpState[pump]; | |
digitalWrite(pumpPin[pump], pumpState[pump]); | |
if (pumpState[pump] == PUMP_ON) { | |
leds[pump] = CRGB::White; | |
} else { | |
leds[pump] = CRGB::Red; | |
} | |
FastLED.show(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment