Back to Parent

int servoPin = A3;
Servo myServo;
int servoPos = 0;

int photoCellPin = A5;
int photoCellReading = 0;

void setup() {
      // attaches the servo on the A3 pin to the servo object
  myServo.attach( servoPin );
  Particle.function("servo", servoControl);
  Particle.variable(  "servoPos" , &servoPos , INT );

}

void loop() {
    photoCellReading = analogRead(photoCellPin);
    servoPos = map(photoCellReading, 3000, 4095, 0, 180);
    myServo.write( servoPos);

}


int servoControl(String command)
{
    // Convert
   int newPos = command.toInt();
   if (newPos = (-1)) {
        servoPos = map(photoCellReading, 3000, 4095, 0, 180);
        myServo.write( servoPos);
       
   }
   else{
        // Make sure it is in the right range
        // And set the position
        servoPos = constrain( newPos, 0 , 180);
        // 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