Hi. I have a problem with slider. It has a value between 20 and 60.I defined that for example if value is between 25 and 35 it should send 30 to arduino. I need to it send 30 value just one time but it sends 7 times. and my void is executed 7 times while I want to it is executed just one time. How can I fix the problem? these are blocks:
test_bluetooth_servo_continiuesmotion_2sliders.aia (6.2 KB)
here is the serial monitor:
this is the sketch:
#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 ch[]={0,1,2,3,4,5,6,7,8,9,10,11,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60};
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;
int theta;
/* 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 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 arm2_setting() {
pos5=theta+110;
pos7=theta+30;
for (int Pos = pos7; 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);
}
}
void loop() {
if (BT.available() >0) {
// Serial.print(state);
int ch = BT.read();
if( ch <=3 ) {
Serial.print(ch);
delay1=ch;
}
else
{
switch(ch){
case 20: theta=ch; arm2_setting(); break;
case 30: theta=ch; arm2_setting(); break;
case 40: theta=ch; arm2_setting(); break;
case 50: theta=ch; arm2_setting(); break;
case 60: theta=ch; arm2_setting(); break;
case 4: move_forward(); break;
case 5: move_stop(); break;
case 6: move_backward(); break;
case 7: move_stop(); break;
case 8: turn_right(); break;
case 9: move_stop(); break;
case 10: turn_left(); break;
case 11: move_stop(); break;
// default: theta=ch; arm2_setting(); break;
}
Serial.print(ch);
}
}
}