In the previous section, we verified the following:
- Whether the Arduino can connect to the internet
- Whether the Arduino can send and receive MQTT messages via shiftr.io
- Whether the motor is working
Now, let's create an interface to control the servo from the Serial Monitor. Create a new sketch and save it as ch5_01_servo_serial.
Delete the boilerplate code that is automatically added to new sketches (the empty setup and loop functions) and replace the contents of your editor with the following code:
#include <Servo.h>
Servo myservo;
void setup() {
Serial.begin(9600);
myservo.attach(9);
}
void loop() {
while (Serial.available() > 0) {
int inputValue = Serial.parseInt();
if (inputValue == 1) {
myservo.write(180);
} else {
myservo.write(0);
}
}
}
Let's go through the code to make sure...