int servoPin = A4;
Servo akshayServo;
int servoPos = 0;
int lastPrice = -1;
int percChange;
void setup() {
akshayServo.attach(A4);
Particle.function("servo", servoControl);
Particle.variable("servoPos", &servoPos, INT);
}
void loop() {
}
int servoControl(String command){
// Convert
int newPrice = command.toInt();
// Make sure it is in the right range
// And set the position
if(lastPrice == -1){
servoPos = 90;
}
else{
percChange = (newPrice - lastPrice) / lastPrice;
servoPos = constrain((percChange*10) + 90, 0 , 180);
}
// Set the servo
akshayServo.write(servoPos);
lastPrice = newPrice;
// done
return 1;
}
Click to Expand