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