This wonderful invention is a box that launches a plane once you get too close to it. Whoosh.
The plane is connected to a servo via rubber band and an ultrasonic sensor is used to detect the distance. Because of the immense force required to keep the rubber band in place, I had to hold down the servo by hand. Everything else is automatic.
Here’s the code:
// Import libraries.
#include
// ================================================================================
// Definitions of constant values.
// The wiring assignment.
const int SERVO_PIN = 9;
#define TRIG_PIN 12
#define ECHO_PIN 11
// ================================================================================
// Global variable declarations.
// Create an object to control the servo by declaring it. The Servo C++ class
// is defined in the Servo librar11y.
Servo wiggling_servo;
// ================================================================================
// Configure the hardware once after booting up. This runs once after pressing
// reset or powering up the board.
void setup()
{
// Initialize the serial UART at 9600 bits per second.
Serial.begin(9600);
// Initialize the Servo object to use the given pin for output.
wiggling_servo.attach(SERVO_PIN);
// Initialize the trigger pin for output.
pinMode(TRIG_PIN, OUTPUT);
digitalWrite(TRIG_PIN, LOW);
// Initialize the echo pin for input.
pinMode(ECHO_PIN, INPUT);
}
float sonar_distance(void)
{
// Generate a short trigger pulse.
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Measure the pulse length
// long duration = pulseIn(ECHO_PIN, HIGH, TIMEOUT);
// return (duration * 1e-6 * SOUND_SPEED) / 2;
long time=pulseIn(ECHO_PIN,HIGH);
return time*340/20000;
}
// ================================================================================
// Run one iteration of the main event loop. The Arduino system will call this
// function over and over forever.
void loop()
{
float d1 = sonar_distance();
Serial.println(d1);
if (d1 < 40) {
wiggling_servo.write(90);
}
}
Leave a Reply
You must be logged in to post a comment.