Hello,
I am kinda new so will need some help.
I want my servo to open (move 90 degrees) when my ultrasonic sensor reads anything between 1 and 10 centimers. And when the object is removed it want it to close again so move 90 degrees.
This is the code I have right now.
#include <NewPing.h>
#include <Servo.h>
const int Servopin = 11;
const int Triggerpin = 3;
const int Echopin = 2;
//100 = maxDistance
NewPing sonar(Triggerpin, Echopin, 100);
Servo servo;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
servo.attach(Servopin);
}
void loop() {
int cm = sonar.ping_cm();
Serial.println (cm);
int angle = map(cm, 1, 10, 15, 90);
servo.write(angle);
delay(3000); // put your main code here, to run repeatedly:
}
Please let me know what i have to do since I am out of luck.
Thanks guys! ![]()