controlling servo motor with pushbutton

Hi!
I'm working on a project that opens ther door (servo) if the pushbutton is pressed, and I'm using the ultrasonic sensor hc-sr04 to detect the distance of the person .
I press the pushbutton and the door opens.The door remains open until the object goes out of range of the sensor(e.g 20 cm) and in turn closes the door automatically. When the door opens and closes i want to make a sound using a buzzer ,and blink a led.
How can i make this? Any advice will be appreciated
That's what i have so far :

#include <Servo.h>
const int lockServo = 8; // Servo Drive Output Pin
const int sensorIn = 9; // Hall-Effect Sensor Input Pin
int sensorState = 0;
int directionState = 0;
Servo myservo;
int pos = 0;
const int trigPin = 5;
const int echoPin = 6;
float duration, distance;
int buzzer=4;
int led=11;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer,OUTPUT);
pinMode(led,OUTPUT);
Serial.begin(9600);
myservo.attach(8);
pinMode(sensorIn, INPUT_PULLUP);
digitalWrite(sensorIn,HIGH); // Enable Internal Pull-Up
}

void loop(){
sensorState = digitalRead(sensorIn);
if (directionState == 0){
if (sensorState == LOW) {
directionState = 1;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration*.0343)/2;
  Serial.print("Distance: ");
  Serial.println(distance);
  delay(100);
for(pos = 0; pos < 180; pos += 1 and distance<20)
{myservo.write(pos);
digitalWrite(buzzer,LOW);
digitalWrite(led,LOW);

delay(15); // Wait Time
}
}
} else if (directionState == 1) {
if (sensorState == LOW) {
directionState = 0;
for (pos = 180; pos>=1; pos -=1)
{myservo.write(pos);
digitalWrite(buzzer,HIGH);
digitalWrite(led,HIGH);

delay(15);
}
}
}
}

What you should have done before you even got this far is to measure the force needed to open and to close the door. Decide how you are going to mount the servo and connect it to the door and whatever else it will be connected to. Is your door sliding or is it swinging? Makes a big difference. If this project is more than a thought process, what safety precautions are needed?

once you know the forces necessary, then you can pick a suitable servo or what ever you want to power the door. Then you can pick the power supply necessary to operate your project.

Paul

Thanks for the advice but didn't intended to make a project that serious for now since I'm a beginner in arduino
I just wanted to do the simulation without considering these variabiles and wondering what the code should look like.

Perhaps look at drawing up a flow chart of what happens and when.
Much easier to see the complete picture of what you need the code to do.

You seem to have quite a lot of code already and you must have done some testing. So what does it do so far and what changes/additions do you need?

Steve

coffee34:
How can i make this?

A good starting point would be learning how to do several things at the same time.

slipstick:
You seem to have quite a lot of code already and you must have done some testing. So what does it do so far and what changes/additions do you need?

Steve

Yes ,I've done some testing and sometimes the servo don't rotate.So what I want the servo to do is this:When I press the button the servo rotates 180º ,the buzzer rings and the led is on ,meaning the door is open. So when the person who enters is it at a certain distance from the ultrasonic sensor the door lock(servo rotate 180º backwards)

So far when button gets pressed the servo rotates ,the led is on and the buzzer rings then if I press the button again the servo rotates backward and so on till the person is far than 20 cm from ultrasonic and I press again and the program closes.

Your code only checks the sensor while the button is being pressed and held. That isn't what you wanted is it? The way you describe it you should probably complete the servo move to 180 and after that for loop has finished THEN you check the sensor for distance.

Steve

I will try it ,thanks

slipstick:
The way you describe it you should probably complete the servo move to 180 and after that for loop has finished THEN you check the sensor for distance.

This is where the flowchart would help.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.