Reputation: 121
I'm making an application with xojo where you set the value of 3 sliders, which send the values as strings with json through the serial monitor, and I'm receiving the json values with Arduino to move the 3 servomotors by the slider value.
I tried playing around with this json, but since I'm pretty new, I don't know exactly how to use it. Basically the code works, it doesn't output an error, but it doesn't work, I'm very sure that the electronics part is completely right. can you help me?
This is basically my Xojo code:
var angles as new jsonitem
angles.value("servo1") = slideservo1.value
angles.value("servo2") = slideservo2.value
angles.value("servo3") = slideservo3.value
angles.Compact = True
Serialcontroller.write(angles.toString)
and this is is my Arduino Code:
#include <Servo.h>
#include <ArduinoJson.h>
Servo myServo1;
Servo myServo2;
Servo myServo3;
StaticJsonDocument<200> doc;
void setup() {
myServo1.attach(8);
myServo2.attach(9);
myServo3.attach(10);
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
deserializeJson (doc, Serial);
const char* angle1 = doc["servo1"];
const char* angle2 = doc["servo2"];
const char* angle3 = doc["servo3"];
myServo1.write(atoi(angle1));
myServo2.write(atoi(angle2));
myServo3.write(atoi(angle3));
delay(15);
}
}
Since the electronics part is fully right, and the code doesn't output an error, I don't understand what the error is. My goal is to move the sliders on Xojo, and the value is sent with json through the serial monitor, received by Arduino and used as the "angle" of moving of the servo. Thanks in advance.
Upvotes: 1
Views: 71
Reputation: 121
ok solved it by myself, you just have to write "doc" and the value direct in the my servo.write like:
myServo.write(doc["servo1"]);
Upvotes: 1