2 This program is free software: you can redistribute it and/or modify
3 it under the terms of the GNU Affero General Public License as
4 published by the Free Software Foundation, either version 3 of the
5 License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU Affero General Public License for more details.
12 You should have received a copy of the GNU Affero General Public License
13 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // define which pins are connected to which components
17 int right_motor_forward_pin
= 3;
18 int right_motor_backward_pin
= 4;
19 int right_motor_speed_pin
= 5;
21 int right_echo_pin
= 6;
22 int right_trigger_pin
= 7;
24 int left_motor_forward_pin
= 8;
25 int left_motor_backward_pin
= 9;
26 int left_motor_speed_pin
= 10;
28 int left_echo_pin
= 11;
29 int left_trigger_pin
= 12;
32 digitalWrite(pin
, HIGH
);}
35 digitalWrite(pin
, LOW
);}
37 void set_motor(int speed_pin
,
51 // since speed has been set positive, no need to check if speed < -255.
54 analogWrite(speed_pin
, speed
);}
56 void go(int left_motor_speed
, int right_motor_speed
){
57 set_motor(left_motor_speed_pin
,
58 left_motor_forward_pin
,
59 left_motor_backward_pin
,
61 set_motor(right_motor_speed_pin
,
62 right_motor_forward_pin
,
63 right_motor_backward_pin
,
66 int ping(int trigger
, int echo
){
71 // turn on the trigger and leave it on long enough for the
72 // sonar sensor to notice
74 delayMicroseconds(10);
76 ping_time
= pulseIn(echo
, HIGH
);
79 // sonar needs some time to recover before pinging again,
80 // so make sure it gets enough sleep right here. 50 milliseconds
84 void backup(int backup_time
){
88 void turn_around(int turn_around_time
){
90 delay(turn_around_time
);}
95 pinMode(right_motor_speed_pin
, OUTPUT
);
96 pinMode(right_motor_forward_pin
, OUTPUT
);
97 pinMode(right_motor_backward_pin
, OUTPUT
);
99 pinMode(right_echo_pin
, INPUT
);
100 pinMode(right_trigger_pin
, OUTPUT
);
102 pinMode(left_motor_speed_pin
, OUTPUT
);
103 pinMode(left_motor_forward_pin
, OUTPUT
);
104 pinMode(left_motor_backward_pin
, OUTPUT
);
106 pinMode(left_echo_pin
, INPUT
);
107 pinMode(left_trigger_pin
, OUTPUT
);
109 off(left_motor_speed_pin
);
110 off(left_motor_forward_pin
);
111 off(left_motor_backward_pin
);
112 off(left_trigger_pin
);
114 off(right_motor_speed_pin
);
115 off(right_motor_forward_pin
);
116 off(right_motor_backward_pin
);
117 off(right_trigger_pin
);}
123 int forward_speed
= 250;
126 // adjust this number as necessary for your robot.
127 // it represents how far the table is from your sonar sensor.
128 // larger values mean larger distance. default is 800
129 int right_max_ping_time_over_table
= 800;
130 int left_max_ping_time_over_table
= 800;
131 int backup_time
= 2000;
132 // the exact amount of time for turning around might need
133 // twerking for your robot. the default value is 3200
134 int turn_around_time
= 3200;
136 int actual_left_ping_time
= ping(left_trigger_pin
, left_echo_pin
);
137 int actual_right_ping_time
= ping(right_trigger_pin
, right_echo_pin
);
139 Serial
.print("left ping = ");
140 Serial
.print(actual_left_ping_time
);
141 Serial
.print("\tright_ping = ");
142 Serial
.println(actual_right_ping_time
);
144 // if the left sonar senses a table, keep driving left side forward,
145 // otherwise, stop left side
146 if(actual_left_ping_time
< left_max_ping_time_over_table
){
147 left_speed
= forward_speed
;}
149 left_speed
= stop_speed
;}
150 // if the right sonar senses a table, keep driving right side forward,
151 // otherwise, stop right side
152 if(actual_right_ping_time
< right_max_ping_time_over_table
){
153 right_speed
= forward_speed
;}
155 right_speed
= stop_speed
;}
157 // if both sonars detect being off the table, backup and turn around
158 // otherwise, go the correct speed for each wheel
159 if(actual_left_ping_time
>= left_max_ping_time_over_table
160 && actual_right_ping_time
>= right_max_ping_time_over_table
){
161 Serial
.println("backing up");
163 Serial
.println("turning around");
164 turn_around(turn_around_time
);}
166 Serial
.println("going");
167 go(left_speed
, right_speed
);}}