progran arduino yg di gunakan antara lain sebagai berikut.
#include <Servo.h>
const int RForward = 45;
const int RBackward = 135;
const int LForward = RBackward;
const int LBackward = RForward;
const int RNeutral = 90;
const int LNeutral = 90;
const int pingPin = 7;
const int irPin = 0;
const int dangerThresh = 10;
int leftDistance, rightDistance;
Servo panMotor;
Servo leftMotor;
Servo rightMotor;
long duration;
void setup()
{
rightMotor.attach(11);
leftMotor.attach(10);
panMotor.attach(6);
panMotor.write(90);
}
void loop()
{
int distanceFwd = ping();
if (distanceFwd>dangerThresh)
{
leftMotor.write(LForward);
rightMotor.write(RForward);
}
else //if path is blocked
{
leftMotor.write(LNeutral);
rightMotor.write(RNeutral);
panMotor.write(0);
delay(500);
rightDistance = ping();
delay(500);
panMotor.write(180);
delay(700);
leftDistance = ping();
delay(500);
panMotor.write(90);
delay(100);
compareDistance();
}
}
void compareDistance()
{
if (leftDistance>rightDistance)
{
leftMotor.write(LBackward);
rightMotor.write(RForward);
delay(500);
}
else if (rightDistance>leftDistance)
{
leftMotor.write(LForward);
rightMotor.write(RBackward);
delay(500);
}
else
{
leftMotor.write(LForward);
rightMotor.write(RBackward);
delay(1000);
}
}
long ping()
{
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
return duration / 29 / 2;
}
demikian program yg digunakan.... setelah menyuntikkan program ini kedalam arduino maka robot pun mulai di rangkai sedemikian rupa sehingga menjadi seperti pada gambar di atas...
Tolong diberikan sumber acuan/referensinya. ok
BalasHapus