Controling servo speed by slider

Hi. I want to control servo speed by the slider in android app. I want to set "delay1" in the code to the thumbposition number that is received from Bluetooth device. How can I add it in the code?

#include <SoftwareSerial.h>
#include <Servo.h>
#include "HCPCA9685.h"

SoftwareSerial BT(13,12);  //RX , TX pins

char state=0;
/* I2C slave address for the device/module. For the HCMODU0097 the default I2C address
is 0x40 */ 
#define  I2CAdd_1 0x40
#define  I2CAdd_2 0x41

int pos1 = 200;
int pos2 = 220;
int pos3 = 180;
int pos4 = 0;
int pos5 = 130;
int pos6 = 100;
int pos7=50;
int pos8=260;
int pos9=140;
int delay1;
int delay2 = 500;

/* Create an instance of the library */
HCPCA9685 HCPCA9685_1(I2CAdd_1);
HCPCA9685 HCPCA9685_2(I2CAdd_2);

void setup() {
  /* Initialise the library and set it to 'servo mode' */

  // Initialise both modules
  HCPCA9685_1.Init(SERVO_MODE);
  HCPCA9685_2.Init(SERVO_MODE);

  // Wake both devices up
  HCPCA9685_1.Sleep(false);
  HCPCA9685_2.Sleep(false);

  unsigned int Pos;
  
Serial.begin(9600);
BT.begin(9600);
/////  
/* standing */
  HCPCA9685_1.Servo(2, pos4);
  HCPCA9685_1.Servo(5, pos4);
  HCPCA9685_1.Servo(8, pos4);
  HCPCA9685_1.Servo(11, pos4);
  HCPCA9685_1.Servo(14, pos4);
  HCPCA9685_2.Servo(1, pos4);

  HCPCA9685_1.Servo(1, pos4);
  HCPCA9685_1.Servo(4, pos4);
  HCPCA9685_1.Servo(7, pos4);
  HCPCA9685_1.Servo(10, pos4);
  HCPCA9685_1.Servo(13, pos4);
  HCPCA9685_2.Servo(0, pos4);

  HCPCA9685_1.Servo(0, pos2);
  HCPCA9685_1.Servo(3, pos3);
  HCPCA9685_1.Servo(6, pos2);
  HCPCA9685_1.Servo(9, pos2);
  HCPCA9685_1.Servo(12, pos3);
  HCPCA9685_1.Servo(15, pos2);

  HCPCA9685_2.Servo(2, 200);  // sensor movement

  delay(delay2);

  for (int Pos = pos4; Pos < pos5; Pos++)
  {
    HCPCA9685_1.Servo(1, Pos);
    HCPCA9685_1.Servo(4, Pos);
    HCPCA9685_1.Servo(7, Pos);
    HCPCA9685_1.Servo(10, Pos);
    HCPCA9685_1.Servo(13, Pos);
    HCPCA9685_2.Servo(0, Pos);
    delay(delay1);
  }

  delay(delay2);

  for (int Pos = pos4; Pos < pos6; Pos++)
  {
    HCPCA9685_1.Servo(2, Pos);
    HCPCA9685_1.Servo(5, Pos);
    HCPCA9685_1.Servo(8, Pos);
    HCPCA9685_1.Servo(11, Pos);
    HCPCA9685_1.Servo(14, Pos);
    HCPCA9685_2.Servo(1, Pos);
    delay(1);
  }
}

/* Move Forward */
void move_forward() {
  for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
    HCPCA9685_1.Servo(1, Pos);
    delay(delay1);
  }

      for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
    HCPCA9685_1.Servo(1, Pos);
    delay(delay1);
  }
}

/* Move Backward */
void move_backward() {
  for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
    HCPCA9685_1.Servo(4, Pos);
    delay(delay1);
  }

    for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
    HCPCA9685_1.Servo(4, Pos);
    delay(delay1);
  }
}

/* Turn Right */
void turn_right() {
  for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
    HCPCA9685_1.Servo(7, Pos);
    delay(delay1);
  }

      for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
    HCPCA9685_1.Servo(7, Pos);
    delay(delay1);
  }
}

/* Turn Left */
void turn_left() {
/////
  for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
    HCPCA9685_1.Servo(10, Pos);
    delay(delay1);
  }

      for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
    HCPCA9685_1.Servo(10, Pos);
    delay(delay1);
  }
}

/* Stop Move */
void move_stop() {
}

void loop() {
  if (BT.available() >0) {
    state = BT.read();
    Serial.print(state);

    if(state == 'F'){
      move_forward();
//      state="";
  }
  
    if(state == 'f'){
      move_stop();
//      state="";
  }
  
    if(state == 'B'){
      move_backward();
//      state="";
  }

    if(state == 'b'){
      move_stop();
//      state="";
  }

    if(state == 'R'){
      turn_right();
//      state="";
  }

    if(state == 'r'){
      move_stop();
//      state="";
  }
  
    if(state == 'L'){
      turn_left();
//      state="";
  }

    if(state == 'l'){
      move_stop();
//      state="";
  }
//    else if (state == "S") {
//      move_stop();
//  } 
  }
}

and this is the blocks:

As your topic does not relate directly to the installation or operation of the IDE it has been moved to the Programming Questions category of the forum

instead of just setting a servo to a new position, you need to incrementally change the position until the target postion is reached with a delay between each increment.

think about it

Good idea But it is hard to do. Because there are many for loops and servos.

Does the "thumbposition number that is received from Bluetooth device" come in as digits of a number?

If so, maybe you need to parse serial into a number per something like one of these:

or

The critical tricks are collecting the digits into a number, and doing something with the number once you recognize you aren't getting digits any more. Some snippets:

    case '0' ... '9': // digits -- collect a series of them
      numberMode = true;
      currentValue *= 10;
      currentValue += c - '0';
      break;
  if ( ! ((c >= '0') && (c <= '9')) ) {
    completeNumber();
  }
void completeNumber() {
  if (numberMode) {
    Serial.print(currentValue);
    numberMode = false;
  }
}

But it really depends on how all the Bluetooth slider stuff sends the number.

1 Like

a target can be set for each servo and a sub-function can be periodically called that checks if the current and target positions are not the same and increments the current position.

1 Like

My problem is exactly what you said. After I move the slider Bluetooth doesn't receive any characters or integers.

Maybe it is receiving characters but isn't doing anything interesting with them.

Arduino doesn't seem to document them well, but libc has a number of string functions that you could use to tell if one of your commands was received or not. For example strchr()

    if (BT.available() > 0) {
    state = BT.read();
    Serial.print(state);
    if(strchr("FfBbRrLl",state){ // one of the command characters
     ...
    } else {
      ... // act on other characters....      
    }
  ...
}
1 Like

This has worked for me in the past

2 Likes

I tried this but it doesn't work when I have 'state' char

Thanks

I am not clear what you mean by that

Create a small app that simply reads a slider and sends the thumb position by Bluetooth. On the Arduino, print what is received. Does that work ?

1 Like

Well, you might change to another protocol. An Arduino can handle both numbers and non-numbers:

(copying advice from your other thread)

I want to it read both slider and buttons. so I need two variables to read.

state = BT.read();
state2 = BT.read();

state for buttons and state2 for slider.

So what is the problem ?
You can send a series of values as in the example in post #9

It would be a good idea to attach an identifier to the value so that you know what it refers to when received

1 Like

Hi. I want to control a robot with sending two integers from HCO5 bluetooth module.
The integers is received by esp8266 successfully and the serial monitor displays them. But the servos don't work at all. It is needed to say when I use one integer (state), the servos move correctly. But when I use two integers (state and state1), they don't move. I need use two integers for other project. How can I fix this problem?
This is the code:

#include <SoftwareSerial.h>
#include <Servo.h>
#include "HCPCA9685.h"

SoftwareSerial BT(13,12);  //RX , TX pins

/* I2C slave address for the device/module. For the HCMODU0097 the default I2C address
is 0x40 */ 
#define  I2CAdd_1 0x40
#define  I2CAdd_2 0x41

int state;
int pos1 = 200;
int pos2 = 220;
int pos3 = 180;
int pos4 = 0;
int pos5 = 130;
int pos6 = 100;
int pos7=50;
int pos8=260;
int pos9=140;
int delay1;
int delay2 = 500;

/* Create an instance of the library */
HCPCA9685 HCPCA9685_1(I2CAdd_1);
HCPCA9685 HCPCA9685_2(I2CAdd_2);

void setup() {
  /* Initialise the library and set it to 'servo mode' */

  // Initialise both modules
  HCPCA9685_1.Init(SERVO_MODE);
  HCPCA9685_2.Init(SERVO_MODE);

  // Wake both devices up
  HCPCA9685_1.Sleep(false);
  HCPCA9685_2.Sleep(false);

  unsigned int Pos;
  
Serial.begin(9600);
BT.begin(9600);
}

/* Move Forward */
void move_forward() {
  for (int PosC = pos2 , PosD = pos3 ; PosC >= pos3 || PosD <= pos2 ; PosC-- , PosD++) {
    HCPCA9685_1.Servo(0, PosC);
    HCPCA9685_1.Servo(3, PosD);
    HCPCA9685_1.Servo(6, PosC);
    HCPCA9685_1.Servo(9, PosC);
    HCPCA9685_1.Servo(12, PosD);
    HCPCA9685_1.Servo(15, PosC);
    delay(delay1);
  }
}

/* Move Backward */
void move_backward() {
  for (int Pos = pos5 ; Pos >= pos7 ; Pos--) {
    HCPCA9685_1.Servo(4, Pos);
    HCPCA9685_1.Servo(10, Pos);
    HCPCA9685_2.Servo(0, Pos);
    delay(delay1);
  }
}

/* Turn Right */
void turn_right() {
  for (int PosC = pos2 , PosD = pos3 ; PosC >= pos3 || PosD <= pos2 ; PosC-- , PosD++) {
    HCPCA9685_1.Servo(0, PosD);
    HCPCA9685_1.Servo(6, PosD);
    HCPCA9685_1.Servo(12, PosD);
    delay(delay1);
  }  
}

/* Turn Left */
void turn_left() {
  for (int Pos = pos7 ; Pos <= pos5 ; Pos++) {
    HCPCA9685_1.Servo(4, Pos);
    HCPCA9685_1.Servo(10, Pos);
    HCPCA9685_2.Servo(0, Pos);
    delay(1);
  }    
}

/* Stop Move */
void move_stop() {
}

void loop() {
    if (BT.available() >0) {

    int state2 = BT.read();
    Serial.println(state2);

    state = BT.read();
    Serial.println(state);

    if(state2 == 2){
      move_forward();
  }
  
    if(state2 == 3){
      move_stop();
  }
    
   else if(state == 4){
      move_backward();
  }

   else if(state == 5){
      move_stop();
  }

   else if(state == 6){
      turn_right();
  }

   else if(state == 7){
      move_stop();
  }
  
   else if(state == 8){
      turn_left();
  }

   else if(state == 9){
      move_stop();
  }

      
  } 
}

Topic moved. Please stop posting in IDE 1.x if the problem is not related to IDE 1.x; you can compile, you can upload so it's not a problem with IDE 1.x.

These are your two integers?

Maybe you want to make sure there are two bytes available to read before you try to read them:

    if (BT.available() >1) {

If it is more complicated than that, you should look into these:

1 Like

Thank you

I studied it but there aren't any topics about reading several integers or characters. Or I couldn't understand them. Can you edit my code here if you have a suggestion?