Slider doesn't work on Servo - Help

Here's the block code:

and the Arduino 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.readString(); // 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;
}
}
}

I'm trying to communicate from the Slider with ''s5'' to the arduino, but it doesn't work.

What part works, and what part doesn't work?

Any error messages we should know about?

The code works. It just that the servo doesn't rotate when I move the sliders. Is my MIT block wrong or the code instruction?

It's giving me the right output when I print the serial output.

How does the Arduino know where one incoming message ends and the next incoming message begins?

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

1 Like

And for your message in blocks, append \n at the end

I'm sorry fairly new to MIT, how do I do that?

Like that?

You can add extra sockets to the JOIN block using the blue mutator button.

1 Like

And it's \n, not /n.

/n is not the same as \n

1 Like

But is the block correct? where's the blue button located?

https://appinventor.mit.edu/explore/ai2/support/concepts/mutators

Yes, it works this way too.