1
votes

I must send 3 values gave by 3 potentiometers ,connected by an Arduino Uno, and send them to another Arduino Uno whith a serial comunication. The received values must be distributed in 3 servo motors so that each knob able to control the servo motor movement. the problem with this program is that the values received are not distributed correctly (for example the case that the value of the potentiometer 1 is to be read by the servo motor 3 or other cases). I ask if I could help to synchronize data received with the distribution of them to the servo motors. Thanks in advance.

sketch arduino with potentiometers:

#include <SoftwareSerial.h>
#define RX 2 //Pin tx
#define TX 3 //Pin rx
#define POTPIN A0
#define POTPIN2 A1
#define POTPIN3 A2

SoftwareSerial BTserial(RX, TX);

int lettura_pot;
int lettura_pot2;
int lettura_pot3;
byte val_servo;
byte val_servo2;
byte val_servo3;

void setup()
{
    Serial.println("Inizializzazione seriale...");
    Serial.begin(9600);
    BTserial.begin(9600);
}

void loop()
{
    BTserial.write(255); /* synch symbol */

    lettura_pot = analogRead(POTPIN);
    val_servo=map(lettura_pot,0,1023,0,180);
    BTserial.write(val_servo);
    Serial.println(val_servo);

    lettura_pot2 = analogRead(POTPIN2);
    val_servo2=map(lettura_pot2,0,1023,0,180);
    BTserial.write(val_servo2);
    Serial.println(val_servo2);

    lettura_pot3 = analogRead(POTPIN3);
    val_servo3=map(lettura_pot3,0,1023,0,180);
    BTserial.write(val_servo3);
    Serial.println(val_servo3);
}

sketch arduino with servo motors:

#include <SoftwareSerial.h>
#include<Servo.h>
SoftwareSerial BTserial(2, 3);
Servo myservo, myservo2, myservo3;

byte val_servo,val_servo2,val_servo3,a;

void setup() {
    Serial.begin(9600);
    BTserial.begin(9600);
    myservo.attach(9);
    myservo2.attach(10);
    myservo3.attach(11);
}

void loop() {

    if (BTserial.available() > 0) {
        if (BTserial.available() == 255) { /* synch */
            val_servo = BTserial.read();
            val_servo2 = BTserial.read();
            val_servo3 = BTserial.read();
        }
        Serial.print("SERVO1:");
        Serial.println(val_servo);
        Serial.print("SERVO2:");
        Serial.println(val_servo2);
        Serial.print("SERVO3:");
        Serial.println(val_servo3);

        myservo.write(val_servo);
        myservo2.write(val_servo2);
        myservo3.write(val_servo3);
        BTserial.flush();
    }
}
1

1 Answers

0
votes

master code( no modificated):

#include <SoftwareSerial.h>
#define RX 2 //Pin tx
#define TX 3 //Pin rx
#define POTPIN A0
#define POTPIN2 A1
#define POTPIN3 A2

SoftwareSerial BTserial(RX, TX);

int lettura_pot;
int lettura_pot2;
int lettura_pot3;
byte val_servo;
byte val_servo2;
byte val_servo3;

void setup()
{
  Serial.println("Inizializzazione seriale...");
  Serial.begin(9600);
  BTserial.begin(9600);
}

void loop()
{
  BTserial.write(255);
  lettura_pot = analogRead(POTPIN);
  val_servo=map(lettura_pot,0,1023,0,180);
  BTserial.write(val_servo);
  Serial.println(val_servo);

  lettura_pot2 = analogRead(POTPIN2);
  val_servo2=map(lettura_pot2,0,1023,0,180);
  BTserial.write(val_servo2);
  Serial.println(val_servo2);

  lettura_pot3 = analogRead(POTPIN3);
  val_servo3=map(lettura_pot3,0,1023,0,180);
  BTserial.write(val_servo3);
  Serial.println(val_servo3);


 }

slave code( modificated):

#include <SoftwareSerial.h>
#include<Servo.h>
SoftwareSerial BTserial(2, 3);
Servo myservo, myservo2, myservo3;

byte val_servo,val_servo2,val_servo3,a;

 void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  BTserial.begin(9600);
  myservo.attach(9);
  myservo2.attach(10);
  myservo3.attach(11);
 }

 void loop() {
  // Read serial input:
   if (BTserial.available() > 0) {
   byte synch_symbol = BTserial.read();

    if (synch_symbol == 255) {
    while (BTserial.available() < 3) { }; /* wait for values */
    val_servo = BTserial.read();
    val_servo2 = BTserial.read();
    val_servo3 = BTserial.read();

    /* do something with values */
}

  Serial.print("SERVO1:");
  Serial.println(val_servo);
  Serial.print("SERVO2:");
  Serial.println(val_servo2);
  Serial.print("SERVO3:");
  Serial.println(val_servo3);

  myservo.write(val_servo);
  myservo2.write(val_servo2);
  myservo3.write(val_servo3);
  BTserial.flush();
}
}