1. Control of curtain need motor and distance sonar sensor for

HC-SR04 is suited for object avoidance in robotic applications. Simple fixed point calculations can provide distance approximations for use in further calculations, robotic mapping, or path planning. Utilizing sonar sensors can allow for simple object detection and collision avoidance.

A short ultrasonic pulse is transmitted at the time 0, reflected by an object. The senor receives this signal and converts it to an electric signal. A 10μs width trigger pulse is sent to the signal pin, the Ultrasonic module will output eight 40kHz ultrasonic signal and detect the echo back. If no obstacle is detected, the output pin will give a 38ms high level signal.

According to the sound speed in air of 340m/s, returned time divided by 2 to calculate the distance.

  temp = float(pulseIn(PIN_ECHO, HIGH));

  cm = (temp * 17 )/1000;

Here is the code,

#define PIN_TRIG 12
#define PIN_ECHO 11
  
float cm;
float temp;
  
void setup() {  
  Serial.begin(9600);  
  pinMode(PIN_TRIG, OUTPUT);  
  pinMode(PIN_ECHO, INPUT);  
}  
  
void loop() {
  digitalWrite(PIN_TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(PIN_TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(PIN_TRIG, LOW);
    
  temp = float(pulseIn(PIN_ECHO, HIGH));
  cm = (temp * 17 )/1000;
  
  Serial.print("Echo = ");  
  Serial.print(temp);
  Serial.print(",  Distance = ");  
  Serial.print(cm);
  Serial.println("cm");  
  delay(300);  
}

 

2. The servo can be used for library servo .

When the Servo.h is included, the control of servo become easy. Use servo.write() can adjust the turn angle of the servo.

 

#include <Servo.h>
Servo myServo; 
int const potPin = A0;
int angle;   
void setup() {
  myServo.attach(9); // attaches the servo on pin 9 to the servo object
  Serial.begin(9600); // open a serial connection to your computer
}
void loop() {
 angle = 0;
 myServo.write(angle);
  delay(15);
}