This is a little school holiday project we did today. A little "plant clock" for Snappy, our venus fly trap.
Parts:
- Arduino uno
- little servo
- soil meter
- cardboard
- sellotape
We created the clock face by drawing around a dinner plate, onto a cardboard box. The arrow is from some lighter cardboard packaging.
Code:
#include <Servo.h>
const int led_pin = 11;
const int soil_pin = A5;
const int servo_pin = 9;
const int dry = 0;
const int waterme = 45;
const int ok = 90;
const int good = 115;
const int toomuch = 180;
Servo myservo;
void setup() {
Serial.begin(115200); // Debugging only
Serial.println("Starting plant clock");
myservo.attach(servo_pin); // attaches the servo on pin 9 to the servo object
}
void loop() {
myservo.attach(servo_pin); // attaches the servo on pin 9 to the servo object
float soil = analogRead(soil_pin);
Serial.println(soil);
int pos;
if(soil > 1000) {
go_to_position(toomuch);
}
else if (soil > 900) {
go_to_position(good);
}
else if (soil > 800) {
go_to_position(ok);
}
else if (soil > 500) {
go_to_position(waterme);
}
else {
go_to_position(dry);
}
myservo.detach();
delay(1000);
}
int current_pos = 0;
void go_to_position(int desired_pos) {
Serial.println("Current = " + (String)current_pos);
Serial.println("Desired= " + (String)desired_pos);
if (current_pos == desired_pos)
return;
if (desired_pos > current_pos) {
Serial.println("Increasing");
for (int pos = current_pos; pos <= desired_pos; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
Serial.println(pos);
myservo.write(pos); // tell servo to go to position in variable 'pos'
current_pos = pos;
delay(15); // waits 15ms for the servo to reach the position
}
}
else {
Serial.println("descreasing");
for (int pos = current_pos; pos >= desired_pos; pos -= 1) { // goes from 180 degrees to 0 degrees
Serial.println(pos);
myservo.write(pos); // tell servo to go to position in variable 'pos'
current_pos = pos;
delay(15); // waits 15ms for the servo to reach the position
}
}
Serial.println("Done");
}