Go Down

Topic: First time programming anything here. (Read 935 times) previous topic - next topic

digiwolff

Hi guys,

This is my first post. Im just getting into the arduino world and decided to try to build a robot. I am using an arduino uno, a ping ultrasonic sensor, and two springrc continuous rotation servos for my wheels.

I have used a program to control the servos successfully. I have used a program to use the ping successfully. The problem develops when I integrate the two. When the servos and ping are both in the program, the ping stops reading and shows 0's in the serial monitor. If i disconnect the servos' signal wires from the arduino then the ping works again fine, but as soon as one is plugged in, it no longer functions properly. I am unsure if the problem is something electrical or a problem with my program, so I am turning to you guys for help! Here is my code, its not done but it should be enough to identify a problem in the code if one exists

Code: [Select]


const int pingPin = 11;
const int servoR = 9;
const int servoL = 10;

long duration, inches, cm;




void setup()
{
   // initialize serial communication:
  Serial.begin(9600);
  pinMode (servoR,OUTPUT);
  pinMode (servoL,OUTPUT);
}


void go() {
  if (inches > 2){
  analogWrite(servoR, 170);
  analogWrite(servoL, 170);
  }
  else
  {
 
    // BLAH BLAH havent worked out the turning routine yet but it will go here
   
   
  }
}

void ping() {
  {
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
 

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  delay(1000);
  }
}


void loop() {
  ping();
  go();
 
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}


long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

AWOL

#1
Feb 25, 2011, 08:59 am Last Edit: Feb 25, 2011, 09:00 am by AWOL Reason: 1
How are you powering all this?
Code: [Select]
analogWrite(servoR, 170);

Don't do this - use the servo library.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

digiwolff

originally I used a 9v batter to power the arduino. The 9 volts also went through a +5v low dropout voltage regulator. the +5v was used to power the servos. I ran into a problem when the servos wouldnt respond to the arduino signal. I realized that to properly work it seemed that to read the arduino signal properly the servo needed to be grounded to the on board ground, and not the ground in the voltage regulator circuit. So as it currently stands, the servo and ping are both being powered by the arduino boards onboard supply. How can I use an alternate power supply for the servos when they have different commons?

digiwolff

I will give the servo library a shot tomorrow, its gettin late and my head hurts from trying to pick up on all this just today haha. I will give it a shot and let you know how it goes. I feel it is unlikely this will solve my problem though.

AWOL

Quote
originally I used a 9v batter to power the arduino.

A common PPs 9V battery simply doesn't have enough poke to drive two servos, and you shouldn't be driving the servos off the Arduino's regulator.
Four AAs with a common ground will do a much better job.
I normally budget at least 500mA per servo, more if the servo is heavily loaded or making a lot of rapid movements.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up