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: