# Skill Dev IV- Hongyu Mao - a servo clock

In this exercise, I followed up with the tutorial and finally made a device could represent different time slot as a day using servo

Created: December 15th, 2021

0

## Outcome

In this exercise, I followed up with the tutorial and finally made a device could represent different time slot as a day using servo

0

## I first test my servo to see how it could work

0

### Exercise 1: Test the servo

My first step is try to test my servo to see if it could work.

0
``````nt servoPin = A4;
int servoPosition = 0;
Servo servo;
bool updatePosition = true;
void setup()
{
Serial.begin( 9600 );
Particle.function( "pos", setPosition );
servo.attach( servoPin );
}
void loop()
{

if( updatePosition == true ){
updatePosition = false;
}
}

int setPosition( String command ){
Serial.println( "setSpeed: " + command );
int value = command.toInt();
if( value < 0 ) return -1;
if( value > 180 ) return -1;
servoPosition = constrain( value, 0, 180 );
updatePosition = true;
return 1;
}``````
Click to Expand
0
skill dev 4.1
Hongyu Mao
0

### Exercise 2: A clock

then I implemented it on a servo clock to represent day time. the servo will shift certain angle to show exact time of a day.

0
``````nt servoPin = A4;
int servoPosition = 0;

Servo myServo;

void setup() {

// attaches the servo on the A3 pin to the servo object
myServo.attach( A4 );

//Register our Particle to control the servo
Particle.function("servo", servoControl);

// Keep a cloud variable for the current position
Particle.variable( "servoPosition" , &servoPosition , INT );

}

void loop() {

}

int servoControl(String command)
{
// Convert
int newPosition = command.toInt();

// Make sure it is in the right range And set the position
servoPosition = constrain( newPosition, 0 , 180);

// Set the servo
myServo.write( servoPosition );

// done
return 1;
}``````
Click to Expand
0
skill dev 4.1
Hongyu Mao