Ping sensor + Servo Motor problem

Hi everyone,

I have been trying to make a very simple project to get into the Arduino. There is PING sensor on the servo motor, I want servo motor to turn 0-45-90-135-190 degrees and check the distance and I want to read it on the serial screen. However I cant do it and cant figure it out what the problem is..

I use 022 Ide, Arduino Mega,PING sensor and standard servo motor.

When i use them separetly, they work well but i cant make them work together..

And this is the program which doesnt work. (Servo turns but i cant read the distance.)

#include <Servo.h> 

Servo Head;
int pingPin = 22; // ultrasonic sensor pin

void setup()
{
  Head.attach(3);
  Serial.begin(115200);
}

void Check_degree0()
{
  Head.write(15);
  delay(400);
  return ;
}

void Check_degree45()
{
  Head.write(35);
  delay(400);
  return ;
}

void Check_degree90()
{
  Head.write(80);
  delay(400);
  return ;
}

void Check_degree135()
{
  Head.write(125);
  delay(400);
  return ;
}

void Check_degree180()
{
   Head.write(175);
   delay(400);
   return ;
}

long Distance()
{
  long duration, cm;
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH); 
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW); 
  pinMode(pingPin, INPUT); 
  duration = pulseIn(pingPin, HIGH); 
  cm=duration/29/2;
  digitalWrite(pingPin, LOW);
  return cm;
}

void loop()

{
  Check_degree0();
  delay(1000);
  Serial.print(Distance());
  delay(100);
  Serial.print("\n");
  delay(100);
  
  Check_degree45();
  delay(1000);
  Serial.print(Distance());
  delay(100);
  Serial.print("\n");
  delay(100);
 
  Check_degree90();
  delay(1000);
  Serial.print(Distance());
  delay(100);
  Serial.print("\n");
  delay(100);
}

I hope someone can help me out..

Thanks

Moderator edit: Quote box exchanged for code box

Try this:

long Distance()
{
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH); 
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW); 
  pinMode(pingPin, INPUT); 
  return  pulseIn(pingPin, HIGH) / 29 /2; 
}

All the "return" statements in your head move functions are redundant; you may as well get rid of them.

Thanks

I tried it but still reading wrong values on serial screen. At the very beginning, for once I read the distance but then i start to read only "0-17-0-1-21-0" like this. Still it doesn't work.

By the way I also use sensor shield , can it cause problem?

PS: I deleted the return statements..

How are you powering the servo?

They both take power from the board. I mean Arduino..

That really isn't a good idea - try powering the servo from four AAs.
Don't forget to connect the grounds.

Edit: On second thoughts, simply disconnect the servo completely and run the sketch and see if you get consistent results.

When i disconnect the servo , I receive the correct results.. ( I think program works properly at least without servo :slight_smile: )

But something is really weird and I have no clue, I dont have 4 AAs therefore I just powered on another Arduino board and used +5v and ground pins to power up the servo, servo data pin hooked up the correct board as well. Now servo isnt working. I checked the second arduino pins by voltmeter, it looks normal .