Pages: [1]   Go Down
Author Topic: Ping sensor + Servo Motor problem  (Read 1114 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.)

Code:
#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
« Last Edit: November 11, 2011, 06:22:43 am by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24353
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this:
Code:
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.
« Last Edit: November 11, 2011, 06:30:17 am by AWOL » Logged

"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.

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

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..
« Last Edit: November 11, 2011, 06:41:21 am by Tugsan » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24353
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How are you powering the servo?
Logged

"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.

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

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



Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24353
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

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

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

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 .





Logged

Pages: [1]   Go Up
Jump to: