Tarantula3
Published

Birthday Surprise With Arduino

I am going to give a small surprise to my partner on her birthday.

BeginnerProtip1 hour1,268
Birthday Surprise With Arduino

Things used in this project

Story

Read more

Schematics

sketch_jLhL8KoN3b.fzz

Code

Untitled file

Arduino
#include <Servo.h>

Servo   servo;           // Create a servo object to control the servo
#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 SERVO_PIN     9  // Arduino pin tied to the servo
long duration, distance;

void setup() {
  Serial.begin(9600);
  servo.attach(SERVO_PIN);      // Attach the servo pin
  servo.write(0);               // Reset the servo to the starting point
  pinMode(TRIGGER_PIN, OUTPUT); // Set the pinmode 
  pinMode(ECHO_PIN, INPUT);     // Set the pinmode 
}

void loop() {
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse
  digitalWrite(TRIGGER_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(15);
  digitalWrite(TRIGGER_PIN, LOW);
  
  // Convert the time into a distance
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = duration*0.034/2;   // The speed of sound is 340 m/s or 29 microseconds per centimeter
  
  // Servo controller
  if( distance < 100 ) servo.write(85); // If the distance is less than 100cm then open up the flap
  Serial.print(distance); Serial.println("cm");
  
  delay(100);                   // Wait for 100ms
}

Credits

Tarantula3
77 projects • 91 followers
There were 1000+ sperms but I was the fastest one..

Comments