SoftwareSerial-Servo interference? (confirmed)why?

Hello all

Edit:

Alright, my post got so large that I need to break this down to couple of main points (for those who dont want to read my whole rant). ;D

Problems
1. Using softwareserial connected ultrasonic sensor SRF01 same time with pan/tilt servos causes the servos to jerk time to time.

2. SoftwareSerial .begin needs to be set to 9750 to be able to communicate SRF01m which is supposed to be 9600 baud rate by default, why?

And here is the long version of the story:

This is about possible interference between SoftwareSerial and servo library.

Im working on a project that moves pan/tilt head in sweeps from left to right, then returns back to left, goes one line down and does the same all over again. on the top of the head, there is a ultrasonic/IR sensor attached.

This is how I have managed to build greyscale rasters from distance information sweeped using IR sensors. :sunglasses:

Just now I purchased ultrasonic sensor SRF01 and attached that to my pan/tilt this is when the problems started. :’(

Because of the SRF01 sensor, I needed to start using SoftwareSerial library. Now when I make the call to get the reading from the sensor, before moving to next pan/tilt location, the pan/tilt sometimes spasms. Even the tilt axis, which is not supposed to do anything for the most of the time!

It doesn’t even help if I make the call for distance reading, wait for a second and only then start moving the pantilt, it still goes crazy.

Do any of you guys have any history of problems with SoftwareSerial and Servo libraries or any clue what might cause this?

(If necessary I can add some code later to show what Im actually doing… This is just if somebody happens to know by heart what might be causing this)

Regards
Henry

Edit:

Here is the problematic part of the

     ServoX.write(x);             // gets the startingpoint information from the user
     ServoY.write(y); 

     for (int q = 0; q < yres; q++)         // yres value is defined by user earlier, it sets the Y resolution
     {
         for (int p = 0; p < xres; p++) // same for xres
         {
            eyecmd(eyeadd, getrange);          // Calls a function to get range from SRF01 (function below)
            
          byte highByte = USSerial.read();  
            byte lowByte = USSerial.read();
                  
            int range = ((highByte<<8)+lowByte);  

            Serial.println(range);            // writes the distance read from the sensor to serial
            delay(50);                      //somewhere around here the pan/tilt start to spasm.

            ServoX.write(x++);       //moves pan servo one right
            delay(50);
         }
     ServoY.write(y++);        //when the  read has been completed xres times, adds 1 to tilt/y-servo
     ServoX.write(StartPointX); // and takes the pan/x-servo back to beginning
     x = StartPointX;       
    delay(300);
    }
    
}


void eyecmd(byte Address, byte cmd){    // Function to send commands to the SRF01
  pinMode(eye, OUTPUT);                 // Set pin to output and send break by sending pin low, waiting 2ms and sending it high again for 1ms
  digitalWrite(eye, LOW);                              
  delay(2);                                               
  digitalWrite(eye, HIGH);                            
  delay(1);                                                
  USSerial.print(Address, BYTE);        // Send the address of the SRF01
  USSerial.print(cmd, BYTE);            // Send commnd byte to SRF01
  pinMode(eye, INPUT);                  // make input ready for Rx
}

Edit:

I also forgot to mention this. For some odd reason I need to give my sowtwareserial .begin baud rate of 9750 to get it to communicate at all, eventhough the ultrasonic chip is working with 9600!

I had a problem with servos moving while using the softwareserial library (well newsoftserial) and it was solved by detaching the servo when not used and re-attaching it before use. This was possible in my project but I'm not sure if it would be a suitable solution for your problem.

I am afraid I am not sure about your other issues though.

Mowcius

Thanks for your reply mate. That information was very valuable. I think there must be a way to do this project too with a attach-detach trickery. ;)

I'll get down to testing my possibilities with that work around. Any information related to the softwareSerial issues and any other ideas are still welcome.

mowcius

I got my project working even better than before with your suggestion. Now I’m able to scan 33,3 “dots”/distances every second, which means that my picturing process takes much less time.

For some odd reason, now it also works with the baud rate of 9600 as supposed. All I did I detach-reattach servos like this:

     ServoX.write(x);
     ServoY.write(y); 
     delay(100);
     for (int q = 0; q < yres; q++)
     {
         for (int p = 0; p < xres; p++)
         {
[glow]            ServoY.detach();
            ServoX.detach();[/glow]
           
            eyecmd(eyeadd, getrange);                 
            byte highByte = USSerial.read();  
            
            byte lowByte = USSerial.read();
                  
            int range = ((highByte<<8)+lowByte);  
            Serial.println(range);
            
[glow]            ServoY.attach(9);
            ServoX.attach(8);[/glow]
            
            ServoX.write(x++); 
            delay(30);
         }
     ServoY.write(y++);
     ServoX.write(StartPointX);
     x = StartPointX;       
    delay(300);
    }

Next stop: I’ll try to find what the reason for the softwareserial AND/OR newsoftserial not working right with the servos. A time might come where this gives us real headache.

And thanks again Mowcius! :smiley:

Regards
Henry

Next stop: I'll try to find what the reason for the softwareserial AND/OR newsoftserial not working right with the servos. A time might come where this gives us real headache.

Well from looking it up before I found that it was probably something to do with the interupt code used on them but becuase I sorted my issue I did not look into it further.

Mowcius