Back to Parent

/*
servo Spin
*/

int servoPin = A4;
int servoPosition = 0;// starting state
//int servoTarget = 0; //ending state
Servo servo;

void setup()
{
  Serial.begin( 9600 ); //open serial connection for debugging
  Particle.function( "SetPosition", setPosition );
  Particle.variable("Position", servoPosition);
  servo.attach(servoPin);
Particle.subscribe("Alert", StepCounter);
Particle.publish("P2_LOG","START");
}

void loop()
{

  delay(100);

}

int setPosition( String command ){
  int servoTarget = command.toInt();
  servoTarget = constrain(servoTarget, 5, 179);
  servo.write(servoTarget);

  servoPosition = 180 - servoTarget;
  return 1;

}

void StepCounter(const char *event, const char *data)
{
  String angle = String(data);
  Particle.publish("P2_LOG_1",angle);
  int servoTarget = angle.toInt();
  Particle.publish("P2_LOG_2",String(servoTarget));
  servoTarget = constrain(servoTarget, 5, 179);
  servo.write(servoTarget);
  servoPosition = 180 - servoTarget;
  Particle.publish("degree", String(servoPosition));
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0