2017-03-05 2 views
1

Je dois envoyer 3 valeurs données par 3 potentiomètres, connectés par un Arduino Uno, et les envoyer à un autre Arduino Uno avec une communication série. Les valeurs reçues doivent être réparties dans 3 servomoteurs afin que chaque bouton puisse contrôler le mouvement du servomoteur. le problème avec ce programme est que les valeurs reçues ne sont pas réparties correctement (par exemple le cas où la valeur du potentiomètre 1 doit être lue par le servomoteur 3 ou d'autres cas). Je demande si je pourrais aider à synchroniser les données reçues avec la distribution de ceux-ci aux servomoteurs. Merci d'avance.distribuer les données reçues d'une communication série avec 2 arduino uno

arduino croquis avec potentiomètres:

#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); 
} 

arduino croquis avec servo-moteurs:

#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(); 
    } 
} 

Répondre

0

code maître (pas d'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); 


} 

code esclave (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(); 
} 
} 
+0

note: vous ne devriez pas laisser ce code ici, car il ne répond pas à votre question .. –