@@ -10,21 +10,32 @@ int main(void) {
10
10
printf ("There is no available servo\n" );
11
11
return -1 ;
12
12
}
13
-
13
+ returncode_t result = RETURNCODE_EOFF ;
14
14
uint16_t angle = 0 ;
15
+ uint16_t index = 0 ; // the first index available.
16
+
17
+ if (libtock_current_servo_angle (index , & angle ) == RETURNCODE_ENODEVICE ) {
18
+ printf ("\n The index number is bigger than the available servomotors\n" );
19
+ return -1 ;
20
+ }
15
21
16
22
// Changes the angle of the servomotor from 0 to 180 degrees (waiting 0.1 ms between every change).
17
23
for (int i = 0 ; i <= 180 ; i ++ ) {
18
- if (libtock_servo_angle (i ) == RETURNCODE_SUCCESS ) {
24
+ // `result` stores the returned code received from the driver.
25
+ result = libtock_servo_angle (index , i );
26
+ if (result == RETURNCODE_SUCCESS ) {
19
27
libtocksync_alarm_delay_ms (100 );
20
28
// Verifies if the function successfully returned the current angle.
21
- if (libtock_current_servo_angle (& angle ) == RETURNCODE_SUCCESS ) {
22
- printf ("The current angle is: %d\n " , angle );
29
+ if (libtock_current_servo_angle (index , & angle ) == RETURNCODE_SUCCESS ) {
30
+ printf ("\nThe current angle is: %d" , angle );
23
31
} else {
24
- printf ("\nThe servomotor is OFF \n" );
32
+ printf ("\n This servo cannot return it's angle. \n" );
25
33
}
26
- } else {
27
- printf ("\nThe angle could not be changed\n" );
34
+ } else if (result == RETURNCODE_EINVAL ){
35
+ printf ("\nThe angle you provided exceeds 360 degrees\n" );
36
+ return -1 ;
37
+ } else if (result == RETURNCODE_FAIL ){
38
+ printf ("\nThe angle could not be changed. The provided angle exceeds the servo's limit\n" );
28
39
return -1 ;
29
40
}
30
41
}
0 commit comments