Pages: [1]   Go Down
Author Topic: Trouble with Ping))) Sensor PLEASE HELP!  (Read 447 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I am building a robot that uses a Ping))) sensor as the "eyes" of the robot. The problem occurs after I upload the code to the arduino. It seems that the arduino is executing fine, but my Ping))) sensor has some strange behavior. Whenever the arduino starts to execute the code, the Ping))) sensor flashes on then off. It only collects data for the first half-second.

Here is my setup:
Two Continuous Rotation Servo Motors Connected to 5V, ground, and pins 9 and 6.
Ping))) Sensor connected to 5V(Same as servos), pin 7, and Ground.
-There are no resistors, capacitors, transistors, or any other electronic parts. Just motors and the Ping))) sensor.

If this also helps, here is my code:
Start of Code:

#include <Servo.h>
int SensorPin = 7;//ping pin
Servo left_wheel; //right and left
Servo right_wheel;
unsigned long pulseDuration=0;
int distance;
int state=0;

void setup() {
  left_wheel.attach(9);
  right_wheel.attach(6); //pin that servo is plugged into
}

void loop() {
  if (state == 0) {
    state=state+1;
  } else {
    state=state-1;
  }
   //The State stuff is to give the servos and the Ping))) sensor "turns"
  if (state==0){
    pinMode(SensorPin, OUTPUT); //sends Ping
    digitalWrite(SensorPin, HIGH);
    delayMicroseconds(5); //wait...
    digitalWrite(SensorPin, LOW); //Sets up for receiving the ping
    
    pinMode(SensorPin, INPUT); //More setup for receiving ping
    pulseDuration=pulseIn(SensorPin, HIGH); //recieves ping
    pulseDuration=pulseDuration/2;
    distance = int(pulseDuration/29); //calculates distance in cm
  } else {
      if (distance < 13) {
        while (distance < 13); {
          left_wheel.write(0);
          right_wheel.write(180); //turns the robot
          pinMode(SensorPin, OUTPUT); //sends new Ping
          digitalWrite(SensorPin, HIGH);
          delayMicroseconds(5); //wait...
          digitalWrite(SensorPin, LOW); //Sets up for receiving the ping
      
          pinMode(SensorPin, INPUT); //More setup for receiving ping
          pulseDuration=pulseIn(SensorPin, HIGH); //recieves ping
          pulseDuration=pulseDuration/2;
          distance = int(pulseDuration/29); //calculates distance in cm
          delay(20);
        }
      } else {
        left_wheel.write(180); //FORWARD!
        right_wheel.write(180);
        delay(20);
      }
  }
}

End of Code:

Is there anything I could correct in my setup or code so that the Ping))) sensor works again?

Thanks,

I_Got_Pi
« Last Edit: March 16, 2013, 11:11:47 pm by I_Got_Pi » Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1271
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are powering the servo from the arduino then you should try using direct power. Servos draw too much power from the arduino pins.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, I'll try that.
Logged

Pages: [1]   Go Up
Jump to: