Skip to content

Instantly share code, notes, and snippets.

@Sinanvop
Last active March 20, 2020 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sinanvop/933aacb95cb3a273ff8aea30c4696bd0 to your computer and use it in GitHub Desktop.
Save Sinanvop/933aacb95cb3a273ff8aea30c4696bd0 to your computer and use it in GitHub Desktop.
Arduino code
#include <Servo.h>
#include <Wire.h>
#include "rgb_lcd.h"
rgb_lcd lcd;
const int numberOfPlants = 4;
const int noPlant = -1; // index meaning no plant is being monitored
//controls the LCD backlight colour
const int plant1R = 0;
const int plant1G = 0;
const int plant1B = 255;
const int zeroR = 0;
const int zeroG = 0;
const int zeroB = 0;
const int plant2R = 0;
const int plant2G = 255;
const int plant2B = 0;
const int plant3R = 255;
const int plant3G = 0;
const int plant3B = 0;
const int plant4R = 255;
const int plant4G = 0;
const int plant4B = 255;
const int waterSensorPin = A1;
const int buttonPin = 2;
const int servoPin = 5;
const int ledPins[numberOfPlants] = {12, 11, 10, 9};
// Moisture levels for the different plants.
const int moistureMinima[numberOfPlants] = { 0, 0, 0, 0};
const int moistureMaxima[numberOfPlants] = {195, 682, 750, 950};
Servo indicator;
int sensorValue = 0; // value read from the Soil Moisture
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(waterSensorPin, INPUT);
pinMode(buttonPin, INPUT);
for (int ledNo = 0; ledNo < numberOfPlants; ledNo++) {
pinMode(ledPins[ledNo], OUTPUT);
}
indicator.attach(servoPin);
}
void loop() {
// Which plant am I currently monitoring?
static int whichPlant = noPlant;
sensorValue = analogRead(waterSensorPin);
// Switch to next plant on button press.
if (digitalRead(buttonPin) == HIGH) {
if (whichPlant != noPlant) {
digitalWrite(ledPins[whichPlant], LOW);
}
++whichPlant;
if (whichPlant == numberOfPlants) {
whichPlant = noPlant;
} else {
digitalWrite(ledPins[whichPlant], HIGH);
}
}
if (whichPlant == noPlant) {
digitalWrite(10, LOW);
digitalWrite(12, LOW);
lcd.clear();
lcd.noDisplay();
lcd.setRGB(zeroR, zeroG, zeroB);
}
if (whichPlant == 3) {
digitalWrite(10, HIGH);
digitalWrite(12, HIGH);
lcd.setRGB(plant4R, plant4G, plant4B);
lcd.clear();
lcd.display();
lcd.print("Swamp Plant");
sensorValue = map(sensorValue, 0, 950, 0, 100);
lcd.setCursor(0, 1);
lcd.print(sensorValue);
lcd.print("% Enough water");
Serial.println(sensorValue);
delay(500); // crude debouncing
}
if (whichPlant == 2) {
lcd.setRGB(plant3R, plant3G, plant3B);
lcd.clear();
lcd.display();
lcd.print("Orchid");
sensorValue = map(sensorValue, 0, 750, 0, 100);
lcd.setCursor(0, 1);
lcd.print(sensorValue);
lcd.print("% Enough water");
Serial.println(sensorValue);
}
if (whichPlant == 1) {
lcd.setRGB(plant2R, plant2G, plant2B);
lcd.clear();
lcd.display();
lcd.print("Sunflower");
sensorValue = map(sensorValue, 0, 682, 0, 100);
lcd.setCursor(0, 1);
lcd.print(sensorValue);
lcd.print("% Enough water");
Serial.println(sensorValue);
}
if (whichPlant == 0) {
lcd.setRGB(plant1R, plant1G, plant1B);
lcd.clear();
lcd.display();
lcd.print("Cactus");
sensorValue = map(sensorValue, 0, 195, 0, 100);
lcd.setCursor(0, 1);
lcd.print(sensorValue);
lcd.print("% Enough water");
Serial.println(sensorValue);
}
if (whichPlant != noPlant) {
int sensorValue = analogRead(waterSensorPin);
int position = map(sensorValue,
moistureMinima[whichPlant], moistureMaxima[whichPlant], 180, 0);
indicator.write(constrain(position, 0, 180));
Serial.println(sensorValue);
delay(500);
}
// added 14-02 to make sure servo pointer goes back to start after reading last reading from waterSensor
if (whichPlant == noPlant) {
int sensorValue = analogRead(waterSensorPin);
int position = map(sensorValue,
moistureMinima[whichPlant], moistureMaxima[whichPlant], 180, 0);
indicator.write(constrain(position, 180, 180));
delay(500);
}
//gemaakt door Sinan van Noppen
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment