33#define PWM_ECHO 10
44#define PWM_SERVO 11
55#define DELIMITER " \n " // used to separate each message
6-
7- const int PI_proportion = 90 ; // Number of PI divisions where the servo stops
8- const int PI_delay = 10 ; // Delay (ms) given to the servo to accomplish a PI/PI_proportion angle
6+ #define MEASURE_DELAY 2 // Delay (ms) given to the servo to accomplish a distance measure
97
108long duration;
119int distance;
@@ -29,6 +27,13 @@ int measureDistance(){
2927 return duration*0.034 /2 ; // in cm;
3028}
3129
30+ void getMeasure (int angle){
31+ servo.write (angle);
32+ delay (MEASURE_DELAY);
33+ distance = measureDistance ();
34+ sendData (angle, distance);
35+ }
36+
3237void sendData (int a, int d){
3338 Serial.print (a);
3439 Serial.print (" ," );
@@ -53,19 +58,11 @@ void setup() {
5358
5459void loop () {
5560
56- for (int piRatio = 0 ; piRatio <= PI_proportion; piRatio++){
57- angle = map (piRatio, 0 , PI_proportion, 0 , 180 );
58- servo.write (angle);
59- delay (PI_delay);
60- distance = measureDistance ();
61- sendData (angle, distance);
61+ for (int angle = 0 ; angle <= 180 ; angle++){
62+ getMeasure (angle);
6263 }
63- for (int piRatio = 0 ; piRatio <= PI_proportion; piRatio++){
64- angle = map (piRatio, 0 , PI_proportion, 180 , 0 );
65- servo.write (angle);
66- delay (PI_delay);
67- distance = measureDistance ();
68- sendData (angle, distance);
64+ for (int angle = 180 ; angle >= 0 ; angle--){
65+ getMeasure (angle);
6966 }
7067
7168}
0 commit comments