Back to Parent

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

int redPin = A4;    // RED pin of the LED to PWM pin **A4**
int greenPin = D0;  // GREEN pin of the LED to PWM pin **D0**
int bluePin = D1;   // BLUE pin of the LED to PWM pin **D1**
int redValue = 255; // Full brightness for an Cathode RGB LED is 0, and off 255
int greenValue = 255; // Full brightness for an Cathode RGB LED is 0, and off 255
int blueValue = 255;

int pos = 10;
int SleepLost;

void setup() {
  // attaches the servo on the A7 pin to the servo object
  myServo.attach( A5 );
  //Register our Particle to control the servo
  Particle.function("servo", servoControl);
  // Keep a cloud variable for the current position
  //Particle.variable(  "servoPos" , &servoPos , INT );
  Particle.variable(  "SleepLost" , &SleepLost , INT );
  pinMode( redPin, OUTPUT);
  pinMode( greenPin, OUTPUT);
  pinMode( bluePin, OUTPUT);
  }
void loop() {
 }

int servoControl( String command )
{
    // Convert
   int hours = command.toInt();
   if ( hours > 8)
   {
     pos = 83;
     servoWrite ( pos );
     blueValue = 255;
 		 analogWrite(bluePin, blueValue);
   }
   else if ( hours > 6)
   {
     pos = 75;
     servoWrite ( pos );
     blueValue = 204;
 		 analogWrite( bluePin, blueValue);
   }
   else if( hours > 4 )
   {
     pos = 60;
     servoWrite ( pos );
     blueValue = 153;
 		 analogWrite( bluePin, blueValue);
   }
   else if( hours > 2)
   {
     pos = 45;
     servoWrite ( pos );
     blueValue = 102;
 		 analogWrite( bluePin, blueValue);
   }
   else
   {
     pos = 20;
     servoWrite ( pos );
     blueValue = 0;
 		 analogWrite( bluePin, blueValue);
   }
   return 1;
}

void servoWrite( int a ){
  // Convert
  int newPos = a;
  // Make sure it is in the right range
  // And set the position
  servoPos = constrain( newPos, 0 , 180);
  // Set the servo
  myServo.write( servoPos );
  // done
}
Click to Expand

Content Rating

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

0