Back to Parent

Servo myServo;
int servoPin = A3;
int servoPos = 0;
int buttonPin = D3;
int buttonState;


void setup() {
    
    pinMode( buttonPin , INPUT_PULLUP); // sets pin as input
    
    myServo.attach(A3);

    //Register our Particle to control the servo
    Particle.function("servo", servoControl);
    // Keep a cloud variable for the current position
    Particle.variable(  "servoPos" , &servoPos , INT );

}

void loop() {
    int buttonState = digitalRead( buttonPin );
    
    if(buttonState == LOW){
        servoControl("0");
    }else{
        servoControl("0");
    }
   

}


// function that takes an integer to set servo position
int servoControl(String command){
    // Convert
    int newPos = command.toInt();
    // Make sure it is in the right range
    // And set the position
    servoPos = constrain(newPos, 0 , 100);
    // Set the servo
    myServo.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!

0