Skip to content

Instantly share code, notes, and snippets.

@itsabdelrahman
Last active April 28, 2017 15:02
Show Gist options
  • Save itsabdelrahman/cdc7718625922206e6db20613d7e13b9 to your computer and use it in GitHub Desktop.
Save itsabdelrahman/cdc7718625922206e6db20613d7e13b9 to your computer and use it in GitHub Desktop.
#include <NewPing.h>
#include <Servo.h>
#define SERIAL_BAUD 115200
#define TRIGGER_PIN 11
#define ECHO_PIN 12
#define MAX_DISTANCE 200
#define SERVO_PIN 9
#define VERTICAL_ANGLE 113
#define HORIZONTAL_ANGLE 25
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
Servo servo;
void setup() {
Serial.begin(SERIAL_BAUD);
servo.attach(SERVO_PIN);
}
void loop() {
delay(50);
Serial.print("Ping: ");
Serial.print(sonar.ping_cm());
Serial.println(" cm");
while(true) {
servoPointVertical();
delay(500);
servoPointHorizontal();
delay(500);
}
}
void servoPointVertical() {
servo.write(VERTICAL_ANGLE);
}
void servoPointHorizontal() {
servo.write(HORIZONTAL_ANGLE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment