Skocz do zawartości

Połączenie szeregowe dwóch arduino, UART


grymat10

Pomocna odpowiedź

Witam! Mam problem przy połączeniu dwóch arduino szeregowo i odbieraniu danych wysyłanych do tego pełniącego funkcje mastera. Mianowicie połączyłem układ podobnie jak pokazano na rysunku poniżej i chciałbym sterować serwem za pomocą potencjometra. Problem polega na odczytywaniu przez mastera oprócz przekonwertowanego kąta przy pomocy funkcji map() co chwilę wartości 0 i kilku randomowych.

Połączenie: (pominięto diody led)

Kod do arduino - master

const int potpin = 0; // analog pin used to connect the potentiometer
int flex = 0;  // variable to read the value from the analog pin
int inmsg = 0;  //messages back from the slave.

void setup()
{
 delay(7000);    //The 10 second delay to upload new programs.
 Serial.begin(9600);  //Begin Serial to talk to the Serial Monitor   
 Serial.println("Serial Monitor Connected"); 
 Serial.parseInt();     //clear any garbage in the buffer.  
}

void loop()
{
 //Read the flex sensor.  We'll keep this value raw.
 flex = analogRead(potpin);// reads the value of the potentiometer(value between 0 and 1023)
 Serial.println(flex); // Send the potentiometer value to the Monitor
 Serial.println("Send data");// keep an ear out for messages back from the Slave.  You know
 // complaints and the like.
 delay(200);  //This delay is added to give the Slave a chance to
 //return data

 if(Serial.available()) //while there is data available
 {
   inmsg = Serial.parseInt();
   Serial.println("Recived data");
   Serial.println(inmsg);        //Print message to the Serial Monitor
   delay(200);  //This delay is added to give the Slave a chance to
               //return data
 }

}

Kod arduino slave:

#include <Servo.h>
Servo myservo; // create servo object to control a servo
int recdMSG; //variable for received Messages
int val=90;  // Scaled setting to the servo
             //- default is neutral 90degrees

int inmsg = 0;  //This is the message flag from the Serial

void setup()
{
 delay(7000);    //The 10 second delay to upload new programs.
 Serial.begin(9600);  //Serial port for communications with Master
 myservo.attach(9);// attaches the servo servo on pin 9 to the servo object

}

void loop()
{
 if(Serial.available()) //while there is data available
 {
   recdMSG = Serial.parseInt();       //Reads integers as integer rather than ASCII
   Serial.println(recdMSG);
   val = map(recdMSG, 0, 1023, 0, 179);  //scale the value to an angle for the servo
   myservo.write(val);    //send this angle to the servo
   Serial.println(val);   //Send the new val back to the Tx
   delay(200); //10 millisecond delay
 }
}

Próbowałem rozwiązania na wiele sposobów, zorientowałem się, że problem może polegać na ilości wysyłanych i odbieranych bajtów, ale niestety jestem w tym totalnie zielony.

Oto informacje wysyłane podglądane przez szeregowy monitor arduino ide

A to dane odbierane przez arduino z serwomechanizmem, przez co nie działa on poprawnie

Z góry dziękuje za dobre rady!

Link do komentarza
Share on other sites

Dołącz do dyskusji, napisz odpowiedź!

Jeśli masz już konto to zaloguj się teraz, aby opublikować wiadomość jako Ty. Możesz też napisać teraz i zarejestrować się później.
Uwaga: wgrywanie zdjęć i załączników dostępne jest po zalogowaniu!

Anonim
Dołącz do dyskusji! Kliknij i zacznij pisać...

×   Wklejony jako tekst z formatowaniem.   Przywróć formatowanie

  Dozwolonych jest tylko 75 emoji.

×   Twój link będzie automatycznie osadzony.   Wyświetlać jako link

×   Twoja poprzednia zawartość została przywrócona.   Wyczyść edytor

×   Nie możesz wkleić zdjęć bezpośrednio. Prześlij lub wstaw obrazy z adresu URL.

×
×
  • Utwórz nowe...

Ważne informacje

Ta strona używa ciasteczek (cookies), dzięki którym może działać lepiej. Więcej na ten temat znajdziesz w Polityce Prywatności.