自動酒精噴霧器 sprayer 2 (arduino)














































#include <Servo.h>

long  i;
float ultrasonic_distance_5_6() {
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  delayMicroseconds(5);
  digitalWrite(5, HIGH);
  delayMicroseconds(10);
  digitalWrite(5, LOW);
  unsigned long sonic_duration = pulseIn(6, HIGH);
  float distance_cm = (sonic_duration / 2.0) / 29.1;
  return distance_cm;
}

Servo servo_7;

void setup()
{
    i = 0;

  pinMode( 5 , OUTPUT);
  pinMode( 6 , INPUT);
  servo_7.attach(7);

}


void loop()
{
    i = ultrasonic_distance_5_6( );
    servo_7.write(0);
    delay(0);
    if (i < 18) {
      servo_7.write(180);
      delay(500);
      servo_7.write(0);
      delay(1000);
    }

}