Basically, it begins at recieving message from the serial then it stores from state variable, converting it into as string. Then, the string with s5 gets extract from s(number) to just the number then converting it from string to int again which goes through the for loop and now the for loop helps the speed of servo to move gradually.
Is the code wrong?
I'm using HC-05 and MG966R servo motor by the way that is connected to PCA9685 channel 5.
Probably.
The only place you print a line feed is where you have gotten the value of variable state but your output lines are of varying length and the only one starting with an 's5' is the first one.
So your expectation that the state variable is going to always start with an 's' is wrong.
Consider adding a \n behind that slider value you send, and adding some code to the Arduino to scan for that incoming \n to know where the current message ends.
From my limited Arduino coding knowledge, I would guess that the instructions contains the words ReadUntil.
P.S. AI2 Sliders shoot off EVents like a machine gun, probably explaining why you got lots of input messages in your buffer, all jammed together.
Some people just update a global variable from the Slider thumb, and send the global variable from a Clock Timer running at a slower rate.
I knew that part is somewhat wrong. But I think that's just the slider fault, everytime I move the slider the first number gets progressively change too.
Try this code:
#include <HCPCA9685.h>
#include <Wire.h>
#define I2CAdd 0x40
HCPCA9685 HCPCA9685(I2CAdd);
String state = "";
int servo5Pos;
int servo5PPos;
void setup() {
Wire.begin();
Serial.begin(4800);
HCPCA9685.Init(SERVO_MODE);
HCPCA9685.Sleep(false);
servo5PPos = 63;
}
void loop(){
if (Serial.available() > 0){
state = Serial.readStringUntil('\n'); // Read the data as string
Serial.print(state + "\n");
// If slider has changed value - Move Servo 5 to position
if (state.startsWith("s5")){
String stateS = state.substring(2, state.length()); // Extract only the number. E.g. from "s1120" to "120"
servo5Pos = stateS.toInt(); // Convert the string into integer
// We use for loops so we can control the speed of the servo
// If previous position is bigger then current position
Serial.print(servo5Pos);
if (servo5PPos > servo5Pos){
for (int pos = servo5PPos; pos >= servo5Pos; pos--) {
HCPCA9685.Servo(5, pos);
delay(20);
}
}
if (servo5PPos < servo5Pos){
for (int pos = servo5PPos; pos <= servo5Pos; pos++) {
HCPCA9685.Servo(5, pos);
delay(20);
}
}
servo5PPos = servo5Pos;
}
}
}
And for your message in blocks, append \n
at the end
And for your message in blocks, append
\n
at the end
I'm sorry fairly new to MIT, how do I do that?
And it's \n, not /n.
/n
is not the same as \n
But is the block correct? where's the blue button located?
Yes, it works this way too.
I can't use the blue it because it only uses number not a string, it wont let me attach.
This is just an example of what you need to do with this red block to get 3 inputs. I never wrote that you should use a blue block....
My bad. However, unfortunately the code didn't work but gave me this instead.