Search Algorithms-2
// SearchAlgorithm
// this code is starter code for doing a search
// it uses a servo with an Ultrasonic distance sensor mounted on it
//
// modify it to learn basic coding by creating print
// statements, variables, functions and loops
#include <Servo.h> // servo library from Arduino
Servo testServo; // define Servo name
#define SERVO_PIN 3
#define SPEAKER_PIN 12
#include <NewPing.h>
#define TRIGGER_PIN 4 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
int distance;
int servoAngle;
boolean turningright;
boolean found;
void setup()
{
Serial.begin(115200); //setup Serial Monitor
Serial.println("Servo test code starting…");
testServo.attach(SERVO_PIN); // set up servos on digital pins 8
pinMode(SPEAKER_PIN, OUTPUT);
} //end of setup
void loop()
{
turningright = true;
servoAngle = 0;
while (!found)
{
// Move servo to next position
Serial.print("servoAngle = "); Serial.println(servoAngle);
testServo.write(servoAngle);
delay (20);
if (turningright) servoAngle++;
else servoAngle --;
if (servoAngle == 180) turningright = false;
if (servoAngle == 0) turningright = true;
// get data from sensors
readDistanceSensor();
Serial.println(distance);
// logic when something is found
if ((10 < distance) && (distance < 30)) // any delay in this loop will slow down search
{
beep();
found = true;
}
}
} // end of loop()
void readDistanceSensor()
{
distance = sonar.ping_cm(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print (distance); // print result
Serial.println("cm");
}
void beep()
{
Serial.println("beep played");
digitalWrite(SPEAKER_PIN, HIGH);
delay(75);
digitalWrite(SPEAKER_PIN, LOW);
}
// this code is starter code for doing a search
// it uses a servo with an Ultrasonic distance sensor mounted on it
//
// modify it to learn basic coding by creating print
// statements, variables, functions and loops
#include <Servo.h> // servo library from Arduino
Servo testServo; // define Servo name
#define SERVO_PIN 3
#define SPEAKER_PIN 12
#include <NewPing.h>
#define TRIGGER_PIN 4 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 5 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
int distance;
int servoAngle;
boolean turningright;
boolean found;
void setup()
{
Serial.begin(115200); //setup Serial Monitor
Serial.println("Servo test code starting…");
testServo.attach(SERVO_PIN); // set up servos on digital pins 8
pinMode(SPEAKER_PIN, OUTPUT);
} //end of setup
void loop()
{
turningright = true;
servoAngle = 0;
while (!found)
{
// Move servo to next position
Serial.print("servoAngle = "); Serial.println(servoAngle);
testServo.write(servoAngle);
delay (20);
if (turningright) servoAngle++;
else servoAngle --;
if (servoAngle == 180) turningright = false;
if (servoAngle == 0) turningright = true;
// get data from sensors
readDistanceSensor();
Serial.println(distance);
// logic when something is found
if ((10 < distance) && (distance < 30)) // any delay in this loop will slow down search
{
beep();
found = true;
}
}
} // end of loop()
void readDistanceSensor()
{
distance = sonar.ping_cm(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print (distance); // print result
Serial.println("cm");
}
void beep()
{
Serial.println("beep played");
digitalWrite(SPEAKER_PIN, HIGH);
delay(75);
digitalWrite(SPEAKER_PIN, LOW);
}