// Include Particle Device OS APIs
#include "Particle.h"
int servoPin = A2;
Servo servo;
int defaultPosition = 90; // Default position for the servo
int peekPosition = 180; // Peek position for the servo
// Let Device OS manage the connection to the Particle Cloud
SYSTEM_MODE(AUTOMATIC);
// Show system, cloud connectivity, and application logs over USB
SerialLogHandler logHandler(LOG_LEVEL_INFO);
// setup() runs once, when the device is first turned on
void setup() {
servo.attach(servoPin); // Attach the servo to the pin
servo.write(defaultPosition); // Set the servo to the default position
delay(500); // Allow the servo to settle
Serial.println("Servo initialized to 90 degrees");
}
// loop() runs over and over again, as quickly as it can execute.
void loop() {
// Keep the servo at 90 degrees by default
servo.write(defaultPosition);
delay(1000);
// Call the peek function for demonstration
peek(); // You can replace this with a condition or trigger
delay(5000); // Wait 5 seconds before next peek
}
// Function to move the servo to 180 degrees slowly
void peek() {
Serial.println("Peeking...");
for (int pos = defaultPosition; pos <= peekPosition; pos += 5) { // Move in steps of 5 degrees
servo.write(pos); // Set the servo position
delay(50); // Delay for smooth movement
}
Serial.println("Peek complete");
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .