Finally, we need to alter the generated code a bit so that the motor is properly triggered by the Choreo response. To include the servo library and initialize the variables you’ll need for the motor, add the following to the beginning of your sketch:
#include <Servo.h> Servo servo;
Under void setup(), add:
servo.attach(outputPin);
Under void loop(), replace
digitalWrite(outputPin, HIGH);
with
servo.write(180); delay(5000); servo.write(0); delay(1000);
That will turn the motor 180 degrees to open the valve, pause for 5 seconds to allow the water to flow out, and then turn back 180 degrees to close the valve. Of course, if you don’t want the valve to be turned 180 degrees or don’t want the delay between opening and closing to last for 5 seconds, feel free to change the values we used here.
Finally, note the line
delay(250);
at the end of void loop(). This puts a small default delay in place between Choreo calls, but for the purposes of this application, you'll probably want a longer one—if you do decide to water your plant, more than 250 milliseconds will be needed for the motor to open the valve and for the water to permeate the soil. Likewise, note that the code has a built-in 10 call Choreo execution limit; this is a useful safegaurd to leave in place while you're testing, but once you're satisfied with how things are working, you may want to remove it.
With that, your sketch is ready to go! You can continue to make changes if you’d like, but no further alterations are required. Save it, upload it to your LaunchPad, stick the moisture sensor in some soil, and open the serial monitor to begin monitoring your plant.
Page last edited March 08, 2024
Text editor powered by tinymce.