"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
Metal detecting robot sketch
1. long obstacle = 15;
const int pingPin = 11 ;
const int echoPin = 12;
#include <Servo.h>
long angle[5];
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
int in1 = 6;
int in2 = 7;
int in3 = 9;
int in4 = 10;
void setup() {
// put your setup code here, to run once:
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
forward();
scan();
}
void forward()
{
digitalWrite(in1, HIGH);//in1, in2 for left dc motor
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);//in3, in4 for right dc motor
digitalWrite(in4, LOW );
delay(1000);
}
void right()
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW );
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH );
delay(1000);
}
void left()
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW );
delay(1500);
}
void backward()
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
2. digitalWrite(in3, LOW);
digitalWrite(in4,HIGH );
delay(2000);
}
void scan()
{
int i=0;
for (pos = 0; pos <= 180; pos += 45)
{ // goes from 0 degrees to 180 degrees in
steps of 45 degree
myservo.write(pos); // tell servo to go to position in
variable 'pos'
angle[i]= look();
i++;
delay(500); // waits 15ms for the servo to reach the
position
}
for( i=0; i < 5; i++) // detecting obstacles
{
if(i < 2)
{
if (angle[i] <= obstacle)
right ();
}
if(i == 2)
{
if (angle[i] <= obstacle)
{
backward ();
delay(100);
right();
}
}
if(i>2)
{
if ( angle[i] <= obstacle)
left();
}
}
delay(500);
for (pos = 180; pos >= 0; pos -= 1)
{
myservo.write(pos); // tell servo to go to position in variable
'pos'
delay(30); // waits 15ms for the servo to reach the
position
}
}
long look()
{
long duration, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
3. pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin , INPUT);
duration = pulseIn(echoPin , HIGH);
// convert the time into a distance
cm = duration / 29 / 2;
Serial.print(cm);
Serial.print("cm");
Serial.println();
return cm;
}