// Include Particle Device OS APIs
#include "Particle.h"
//by Adwoa Asare
//Started: 14 November 2024
//Last update: 14 November 2024
// Let Device OS manage the connection to the Particle Cloud
SYSTEM_MODE(AUTOMATIC);
// Show system, cloud connectivity, and application logs over USB
// View logs with CLI using 'particle serial monitor --follow'
SerialLogHandler logHandler(LOG_LEVEL_INFO);
#define SERVO_PIN A2
#define BUTTON_PIN D3
Servo swan;
int servoPos = 0;
int buttonState;
bool flying = FALSE;
int speed = 5;
bool opening = TRUE;
// setup() runs once, when the device is first turned on
void setup() {
//PARTICLE FUNCTIONS
Particle.function("fly",swan_flight);
Particle.function("land",swan_stop);
Particle.function("control",servo_control);
//INITALIZE PINS/MODES
swan.attach(SERVO_PIN);
pinMode( BUTTON_PIN , INPUT_PULLUP);
}
// loop() runs over and over again, as quickly as it can execute.
void loop() {
buttonState = digitalRead( BUTTON_PIN );
if (buttonState == LOW){
swan_flight("NORM");
}
swan.write(servoPos);
delay(20);
}
//function to make the wings move until limit reached and then reverse
int swan_flight(String command){
flying = TRUE;
if( command == "SLOW")
speed =2;
else if ( command == "NORM")
speed = 5;
else if ( command == "FAST")
speed = 10;
//movement
if (servoPos < 180 && opening)
servoPos += speed;
else if (servoPos >0 && !opening)
servoPos -= speed;
if (servoPos >= 180)
opening = FALSE;
else if (servoPos <= 0)
opening = TRUE;
swan.write(servoPos);
return 1;
}
int swan_stop(String command){
swan.write(0);
flying = FALSE;
return 1;
}
//function to control servo
int servo_control(String command)
{
// Convert
int newPos = command.toInt();
// Make sure it is in the right range
// And set the position
servoPos = constrain( newPos, 0 , 180);
// Set the servo
swan.write( servoPos );
// done
return 1;
}
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. .