Repetition of the movement of the head motor when I hold the button

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:

I have a feeling that if you use this block instead of TouchDown it can work...

Try it!

image

Lito

@>-->---

I tried it But I didn't work too

Patryk_F or ABG maybe they can help you better than me...

Take a look at this topic »

Lito

@>-->---

1 Like

I understand the question better now.

What's needed is a repeating clock Timer and a global message text variable with the letters to keep sending.

Keep sending this message as long as connected.

On touchdown join the new letter to the message letters variable.

On touchup replace that letter with the empty text block, easy way to remove it from the message.

2 Likes

Thank you.Can you show blocks? I don't know how to design clock blocks and How to join new letter to message letters.

Canned Reply: ABG- Export & Upload .aia)
Export your .aia file and upload it here.

export_and_upload_aia

.

test_bluetooth_servo2.aia (3.4 KB)

Here's the Clock based repeating transmission code:


test_bluetooth_servo2 (1).aia (4.4 KB)

I disabled the Slider code because it is not handled in the sketch, and requires special logic to parse multi-byte slider values, compared to the simple single letter scheme you have for the buttons.

1 Like

Thank you very much. I tested your blocks. There is a problem. When I hold the button the servo movement is repeated continuously but When I take my hand off the button servo isn't stopped.

This might be due to messages piling up in the sketch's BlueTooth buffer because they are being sent from AI2 more frequently than the sketch is receiving them.

Try increasing the Clock interval, to slow it down.

Worst comes to worst, you might need to set up handshaking, where the next message is not sent until the previous message is acknowledged.

1 Like

I would probably just record and overwrite the "timestamp" every time a message arrives and check when did the last message arrived, in the receiver's (arduino or whatever) code before continue the operation of the motor.

Or better not cache the messages because the stop(touch up) sends different message.

so I should change my code not blocks. true?

Yes, if the delay is really lies where @ABG told it is.

what changes should I do in my code? Can you edit the code?

Try to flush the BT buffer. Something like this.

void loop() {
  if (BT.available() >0) {
    while(BT.available()){
      state = BT.read();
      }
.
.
.

Though the phone sending timer might be faster than the arduino. Well it depends on serial speed and other things but even the instruction speed of the mega328 is about 40msec if I remember correctly, so set your timer interval in your blocks to 50 too at first, and you will see how it goes.

1 Like

Thanks. I set it to 2000 and the problem is solved. And how about slider? If I enable it will it work? I want to control servo speed with the slider. I want to set "delay1" in to code to the thumbposition number. How can I add it in the code?

I set it to 2000 and it is working now.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.