Isabel - Motor
Made by Isabel Fleck
Made by Isabel Fleck
Working with a servo motor to create a alarm system
Created: December 11th, 2024
Initially starting with the photon board. I began with a simple setup, using the servo only connected to the board. However, when I wrote a piece of code to test the movement of the servo, I kept running into an error about my library not being included, even though I added the Tinker-Servo library, it was still not being registered in my code.
However, despite this, I was also working on another project, using an arduino, and here I applied the same logic to the code and the circuit as I would with the photon, and was able to successfully get the servo working. Using the arduino, I began with a simply servo board circuit, and tested this first, to see if it would run. From here I added a distance module, and adjusted it so that when something was 10 cm away, the servo would rotate 90 degrees, stay here for 3 seconds and return. However, I found this difficult, as when setting the servo in my prototype, the origin point needed to be in a different position. So I then revisited the code, and set a new origin point of rotation. Finally, I added a strip of fairy lights, and connected these to be in response to the distance module, so the lights would slowly turn on, and turn off when the servo returned to its origin position.
#include <Servo.h> // Include the Servo library
Servo myServo; // Create a Servo object
// Define HC-SR04 pins
const int trigPin = 11; // Trigger pin connected to Arduino pin 11
const int echoPin = 12; // Echo pin connected to Arduino pin 12
// Define fairy lights pin
const int lightsPin = 3; // Connected to the PWM control pin of fairy lights
// Define the threshold distance (in cm)
const int distanceThreshold = 10;
// Fade settings
const int fadeDelay = 5; // Delay between brightness adjustments (in milliseconds)
void setup() {
// Initialize the servo
myServo.attach(9); // Attach the servo to pin 9
myServo.write(0); // Set the servo to 0 degrees initially
// Initialize the HC-SR04 pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize fairy lights pin
pinMode(lightsPin, OUTPUT);
analogWrite(lightsPin, 0); // Start with lights off
// Begin Serial communication for debugging (optional)
Serial.begin(9600);
}
void loop() {
int distance = measureDistance(); // Measure the distance
// Print the distance to Serial Monitor (optional)
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance > 0 && distance <= distanceThreshold) {
// Gradually fade in the lights and start the servo movement
fadeLights(true);
delay(500); // Short delay to sync the fade-in with the servo movement
// Move the servo to 90 degrees and hold for 5 seconds
myServo.write(90);
delay(5000);
// Return the servo to 0 degrees
myServo.write(0);
// Gradually fade out the lights
fadeLights(false);
}
delay(100); // Small delay for stability
}
// Function to measure distance using HC-SR04
int measureDistance() {
// Send a 10 microsecond HIGH pulse to the trigger pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the echo time (duration of the HIGH signal in microseconds)
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in cm (duration / 58 = distance in cm)
int distance = duration / 58;
// Return the distance
return distance;
}
// Function to gradually fade in or fade out the lights
void fadeLights(bool fadeIn) {
if (fadeIn) {
// Gradually increase brightness
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(lightsPin, brightness);
delay(fadeDelay); // Adjust fade speed
}
} else {
// Gradually decrease brightness
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(lightsPin, brightness);
delay(fadeDelay); // Adjust fade speed
}
}
}
Click to Expand
Working with a servo motor to create a alarm system