Plant Monitor: Status Light
Part of the Projects section of Coddy's Arduino journey. Lesson 18 of 24.
Nobody reads a percent from across the room. A coloured light can be seen at a glance: red when the plant needs water, green when it is fine, blue when it has too much.
The status light is an RGB LED: three LEDs, red, green and blue, in one package, on pins 9, 10 and 6. Its common leg goes to ground, so a channel lights when its pin is HIGH. One colour at a time means one pin HIGH and the other two LOW.
Three ranges, three colours: the same if / else if / else shape as the parking sensor, starting from the bottom this time:
if (moisture < 30) {
// dry: red
} else if (moisture <= 70) {
// fine: green
} else {
// wet: blue
}Turn all three channels off first. Otherwise a red left over from before mixes with the new green, and the light turns yellow.
Challenge
EasyAdd the status light:
- pins 9 (red), 10 (green) and 6 (blue) are outputs
- under 30%: red only
- 30% to 70%: green only
- over 70%: blue only
Update it after every reading. Press Run, turn the knob, then Check.
Try it yourself
// The knob stands in for a soil moisture sensor
const int sensorPin = A0;
// The moisture in percent: 0 is bone dry, 100 is soaking wet.
int readMoisture() {
int reading = analogRead(sensorPin);
return map(reading, 0, 1023, 0, 100);
}
void setup() {
Serial.begin(9600);
}
void loop() {
int moisture = readMoisture();
Serial.print("Moisture: ");
Serial.print(moisture);
Serial.println("%");
delay(1000);
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Projects
2Reaction Game
Reaction Game: Random WaitReaction Game: TimerReaction Game: False StartReaction Game: Best Time5Plant Monitor
Plant Monitor: MoisturePlant Monitor: Status LightPlant Monitor: Dry AlarmPlant Monitor: ReportPractice on your own: Arduino playground