arduino-libraries / Servo

Servo Library for Arduino
http://arduino.cc/
GNU Lesser General Public License v2.1
252 stars 262 forks source link

Servo control or console output (serial) : with UNO WiFi Rev2, you must choice ! #22

Open danielrueil opened 5 years ago

danielrueil commented 5 years ago

Servo library allows to control a servo by sending pulses to the servo. Using the library with UNO R3 gives correct results : a constant pulse duration in myservo.write (int) gives a stable servo position. Using the library with UNO WiFi Rev2 gives correct duration in 95% of time but random duration in 5% of time. So, the servo does random position jumps.

After a couple of days of research, I understand the issue is linked to simultaneous usage of Serial and Servo. Uncomment Serial.begin(115200) makes the issue disappear. This issue is blocking.

My environnement :

test sketch :

#include <Wire.h>        // for communication with a Sharp device
#include <Servo.h>     
#define  POS_MIN 550 
#define  POS_MAX 2250 
#define  servo_pin  2    // servo pulses pin

Servo my_servo;

void setup() 
{
  my_servo.attach(servo_pin, POS_MIN, POS_MAX); 
  my_servo.write(POS_MIN); 

//********************************************************************

//  Serial.begin(115200); => uncomment this line makes the issue visible with a scope
/*
  delay(2000);
  Serial.println("   ");
  Serial.println("UnoWiFi_basic_test");
*/
//********************************************************************

  Wire.begin();
}
void loop() 
{                    
    process_test();  
}  
void process_test()       
{
    int angle = 675;
    mon_servo.write(angle); 
    Serial.print(" ");
    Serial.println (angle);
    delay(20); 
}   
tsetliff commented 5 years ago

I'm having about the same issue, I didn't test your code but just recently it seems someone is working on a fix with a loop in the serial code: https://github.com/arduino/ArduinoCore-megaavr/pull/42

It was a pain forcing my Win7 to update the code, I ended up turning on all the compile messages in the arduino IDE and saw it was reusing files in Users//AppData... even though I added them to the sketch and so for a quick hack I ended up just finding the UART.cpp file and removing the loop by hand and making sure it didn't use anything pre compiled on the next run and that seemed to work.