Go Down

Topic: Ping sensor + Servo Motor problem (Read 1 time) previous topic - next topic

Tugsan

Nov 11, 2011, 11:59 am Last Edit: Nov 11, 2011, 12:22 pm by AWOL Reason: 1
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: [Select]

#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

AWOL

#1
Nov 11, 2011, 12:25 pm Last Edit: Nov 11, 2011, 12:30 pm by AWOL Reason: 1
Try this:
Code: [Select]
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.
"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.

Tugsan

#2
Nov 11, 2011, 12:38 pm Last Edit: Nov 11, 2011, 12:41 pm by Tugsan Reason: 1
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..

AWOL

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

Tugsan

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




AWOL

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

Tugsan

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

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 .






Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy