Hi. I control a robot via Bluetooth in app inventor. I have a problem.for example when I hold the forward button the servo rotates from position1 to position2 and then the servo rotation doesn't repeat again. I want the movement of the servo motor to repeat as long as I hold the button until I release the button. How can I do this? This is my 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;
const float delay1 = 0.5;
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 Pos = pos5 ; Pos >= pos7 ; Pos--) {
HCPCA9685_1.Servo(4, Pos);
HCPCA9685_1.Servo(10, Pos);
HCPCA9685_2.Servo(0, Pos);
delay(delay1);
}
}
/* Move Backward */
void move_backward() {
for (int PosC = pos3 , PosD = pos2 ; PosC <= pos2 || PosD >= pos3 ; 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);
}
}
/* 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) {
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();
// }
}
}
These are the blocks: