Skip to content

Instantly share code, notes, and snippets.

@HectorTorres
Created July 22, 2020 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HectorTorres/6a4024b4dfd2055fd81ac0681ac4d752 to your computer and use it in GitHub Desktop.
Save HectorTorres/6a4024b4dfd2055fd81ac0681ac4d752 to your computer and use it in GitHub Desktop.
#include <Servo.h>
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // 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.
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int Pinled=2;
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(Pinled, OUTPUT);
}
void loop (){
if (sonar.ping_cm() <= 20)
{
val=180;
myservo.write(val);
digitalWrite(Pinled, HIGH);
}
else {
val=0;
myservo.write(val);
digitalWrite(Pinled, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment