Go Down

Topic: Stuttering Servo Motors (Read 332 times) previous topic - next topic

Linky132

Hi,

Background
I'm building a simple, wheeled, obstacle avoiding Arduino robot. It consists of a wooden base with an Arduino Uno R3 and an Arduino sensor shield v.4.0 mounted on it. It's powered by 4 AA batteries in a case. The servo motors are FS90R's (plugged into 6 and 8 on the shield) and there is a Sharp GP2Y0A21YK0F Reflective (IR) distance Sensor mounted on the front.

Schematic

https://www.screencast.com/t/yvsozNTb

Code
Code: [Select]
#include <Servo.h>// Include servo library

Servo servoLeft; // Declare left servo
Servo servoRight; // Declare right servo

int IRpin = 0; // Analog pin for reading the IR sensor
float IRread; // Floating point number to hold converted voltage as distance

void setup() {
  servoLeft.attach(6); // Attach left signal to P6
  servoRight.attach(8); // Attach right signal to P8
}

void loop() {
  getDistance();

  if (IRread < 299) // NO OBSTACLE
  {
    forward();
  }

  else
  {
    dontGo();
  }
}
void getDistance() {
  IRread = analogRead(IRpin); // Reads the value of the sharp sensor
  delay(250);
}

void forward() { // Create a forward subroutine
  servoLeft.write(180);
  servoRight.write(25);
}

void dontGo() {
  servoLeft.write(90);
  servoRight.write(90);
}

void leftTurn() {
  servoLeft.write(90);
  servoRight.write(0);
}


Problem
The motors work fine without the IR distance sensor but when I add the IR distance sensor code the motors start and stop at very short time intervals with a stuttering motion. It seems as if the Arduino can't do two things at once.

I've tried using millis() as an alternative to delay() but the motors still stutter. Removing the delay() completely makes the motors stutter at really short intervals.

Any help or ideas would be much appreciated, please. Many thanks!

Snoopybg

If you print the IRread value, does it stay stably below 299 if there is no obstacle?

slipstick

#2
Mar 01, 2019, 01:05 pm Last Edit: Mar 01, 2019, 01:07 pm by slipstick
Only write to the motors when you want to change what they're doing. So if they're already going forward don't keep on telling them to go forward just leave them. You can do something like set a "motorState" variable in forward() and the other motor routines and only call the routine again if it isn't already working.

And make sure you have enough power. NiMH rechargeable batteries are better than alkalines.

Steve

vinceherman

First post.  Proper use of code tags.  Karma++

MarkT

You should use separate power for the servos to the Arduino, servos are current hungry and very bursty in
their current demands - this usually results in random-resetting problems unless the Arduino has a separate table supply.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up