// Below an example message for the arduino to take
// {"spray":0,"angle":90,"pump":180}
#include <ArduinoJson.h>
#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
void setup() {
Serial.begin(9600);
while (!Serial) continue;
Serial.println("start");
servo1.attach(9);
servo2.attach(10);
servo3.attach(11);
}
void loop() {
// Check if the other Arduino is transmitting
if (Serial.available())
{
// Allocate the JSON document
// This one must be bigger than the sender's because it must store the strings
StaticJsonDocument<300> doc;
// Read the JSON document from the "link" serial port
DeserializationError err = deserializeJson(doc, Serial);
if (err == DeserializationError::Ok)
{
// (we must use as<T>() to resolve the ambiguity)
int spray = doc["spray"].as<int>();
int angle = doc["angle"].as<int>();
int pump = doc["pump"].as<int>();
// Print the values
/*
Serial.print("spray = ");
Serial.println(spray);
Serial.print("angle = ");
Serial.println(angle);
Serial.print("pump = ");
Serial.println(pump);
*/
//write angles to servos
servo1.write(spray);
servo2.write(angle);
servo3.write(pump);
}
else
{
// Print error to the "debug" serial port
Serial.print("deserializeJson() returned ");
Serial.println(err.c_str());
// Flush all bytes in the "link" serial port buffer
while (Serial.available() > 0)
Serial.read();
}
}
}