grymat10 Napisano Styczeń 11, 2018 Udostępnij Napisano Styczeń 11, 2018 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! Cytuj Link do komentarza Share on other sites More sharing options...
FlyingDutch Styczeń 13, 2018 Udostępnij Styczeń 13, 2018 Cześć, nie analizowałem dokładnie twojego kodu, to co nasuwa się bez dogłębnej analizy. Czy nie powinieneś mieć opóźnienia po odczycie z ADC? flex = analogRead(potpin); delay(100); Pozdrawiam Cytuj Link do komentarza Share on other sites More sharing options...
Pomocna odpowiedź
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!