+++ /dev/null
-/*
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-// define which pins are connected to which components
-int right_motor_speed_pin = 3;
-int right_motor_forward_pin = 4;
-int right_motor_backward_pin = 5;
-
-int left_motor_speed_pin = 8;
-int left_motor_forward_pin = 9;
-int left_motor_backward_pin = 10;
-
-void on(int pin){
- digitalWrite(pin, HIGH);}
-
-void off(int pin){
- digitalWrite(pin, LOW);}
-
-void setup(){
- // the arduino will change the voltage on these pins
- // therefore, these pins are OUTPUT pins
- pinMode(right_motor_speed_pin, OUTPUT);
- pinMode(right_motor_forward_pin, OUTPUT);
- pinMode(right_motor_backward_pin, OUTPUT);
-
- pinMode(left_motor_speed_pin, OUTPUT);
- pinMode(left_motor_forward_pin, OUTPUT);
- pinMode(left_motor_backward_pin, OUTPUT);}
-
-void loop(){
- off(right_motor_backward_pin);
- on(right_motor_forward_pin);
- analogWrite(right_motor_speed_pin, 255);
-
- off(left_motor_backward_pin);
- on(left_motor_forward_pin);
- analogWrite(left_motor_speed_pin, 255);}
+++ /dev/null
-/*
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-int right_echo_pin = 6;
-int right_trigger_pin = 7;
-
-int left_echo_pin = 11;
-int left_trigger_pin = 12;
-
-int right_ping_microseconds = 0;
-int left_ping_microseconds = 0;
-double sound_cm_per_microsecond_at_sea_level = 0.034029;
-int count = 0;
-
-void on(int pin){
- digitalWrite(pin, HIGH);}
-
-void off(int pin){
- digitalWrite(pin, LOW);}
-
-int ping(int trigger, int echo){
- int ping_time = 0;
- // turn off trigger
- off(trigger);
- delayMicroseconds(2);
- // turn on the trigger and leave it on long enough for the
- // sonar sensor to notice
- on(trigger);
- delayMicroseconds(10);
- off(trigger);
- ping_time = pulseIn(echo, HIGH);
- if(ping_time <= 0){
- ping_time = 3000;}
- // sonar needs some time to recover before pinging again,
- // so make sure it gets enough sleep right here. 50 milliseconds
- delay(50);
- return ping_time;}
-
-void setup(){
- Serial.begin(9600);
- pinMode(right_echo_pin, INPUT);
- pinMode(right_trigger_pin, OUTPUT);
- pinMode(left_echo_pin, INPUT);
- pinMode(left_trigger_pin, OUTPUT);}
-
-void loop(){
- right_ping_microseconds = ping(right_trigger_pin, right_echo_pin);
- left_ping_microseconds = ping(left_trigger_pin, left_echo_pin);
- // print out the pulse time
- Serial.print(right_ping_microseconds);
- Serial.print(" = right ping microseconds, ");
- Serial.print(left_ping_microseconds);
- Serial.print(" = left ping microseconds. #");
- Serial.println(count++);
-
- // wait so it's easier to read the serial monitor.
- // change delay to 0 for fullspeed.
- // default is 333, which is about 1/3 of a second
- delay(333);}
+++ /dev/null
-/*
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-// define which pins are connected to which components
-int right_motor_speed_pin = 3; // orange wire
-int right_motor_forward_pin = 4; // brown
-int right_motor_backward_pin = 5; // purple
-
-int right_echo_pin = 6; // white
-int right_trigger_pin = 7; // blue
-
-int left_motor_speed_pin = 8; // orange
-int left_motor_forward_pin = 9; // brown
-int left_motor_backward_pin = 10; // purple
-
-int left_echo_pin = 11; // white
-int left_trigger_pin = 12; // blue
-
-// you'll need to adjust these based on your sonar sensor's behavior
-int desired_right_ping_time = 800;
-int desired_left_ping_time = 800;
-
-void on(int pin){
- digitalWrite(pin, HIGH);}
-
-void off(int pin){
- digitalWrite(pin, LOW);}
-
-void set_motor(int speed_pin,
- int forward_pin,
- int backward_pin,
- int speed){
- if(speed > 0){
- off(backward_pin);
- on(forward_pin);}
- else if(speed < 0){
- off(forward_pin);
- on(backward_pin);
- speed = -speed;}
- else{ // speed is 0
- off(forward_pin);
- off(backward_pin);}
- // since speed has been set positive, no need to check if speed < -255.
- if(speed > 255){
- speed = 255;}
- analogWrite(speed_pin, speed);}
-
-void go(int left_motor_speed, int right_motor_speed){
- set_motor(left_motor_speed_pin,
- left_motor_forward_pin,
- left_motor_backward_pin,
- left_motor_speed);
- set_motor(right_motor_speed_pin,
- right_motor_forward_pin,
- right_motor_backward_pin,
- right_motor_speed);}
-
-int ping(int trigger, int echo){
- int ping_time = 0;
- // turn off trigger
- off(trigger);
- delayMicroseconds(2);
- // turn on the trigger and leave it on long enough for the
- // sonar sensor to notice
- on(trigger);
- delayMicroseconds(10);
- off(trigger);
- ping_time = pulseIn(echo, HIGH);
- if(ping_time <= 0){
- ping_time = 3000;}
- // sonar needs some time to recover before pinging again,
- // so make sure it gets enough sleep right here. 50 milliseconds
- delay(50);
- return ping_time;}
-
-void backup(int backup_time){
- go(-250, -250);
- delay(backup_time);}
-
-void turn_around(int turn_around_time){
- go(-250, 250);
- delay(turn_around_time);}
-
-void setup(){
- Serial.begin(9600);
-
- pinMode(right_motor_speed_pin, OUTPUT);
- pinMode(right_motor_forward_pin, OUTPUT);
- pinMode(right_motor_backward_pin, OUTPUT);
-
- pinMode(right_echo_pin, INPUT);
- pinMode(right_trigger_pin, OUTPUT);
-
- pinMode(left_motor_speed_pin, OUTPUT);
- pinMode(left_motor_forward_pin, OUTPUT);
- pinMode(left_motor_backward_pin, OUTPUT);
-
- pinMode(left_echo_pin, INPUT);
- pinMode(left_trigger_pin, OUTPUT);
-
- off(left_motor_speed_pin);
- off(left_motor_forward_pin);
- off(left_motor_backward_pin);
- off(left_trigger_pin);
-
- off(right_motor_speed_pin);
- off(right_motor_forward_pin);
- off(right_motor_backward_pin);
- off(right_trigger_pin);}
-
-void loop(){
- int left_speed;
- int right_speed;
-
- int forward_speed = 250;
- int stop_speed = 0;
-
- unsigned int actual_left_ping_time = ping(left_trigger_pin, left_echo_pin);
- unsigned int actual_right_ping_time = ping(right_trigger_pin, right_echo_pin);
-
- left_speed = actual_left_ping_time - desired_left_ping_time;
- right_speed = actual_right_ping_time - desired_right_ping_time;
-
- Serial.print("left: ping = ");
- Serial.print(actual_left_ping_time);
- Serial.print(" speed = ");
- Serial.print(left_speed);
- Serial.print(" right: ping = ");
- Serial.print(actual_right_ping_time);
- Serial.print(" speed = ");
- Serial.println(right_speed);
-
- go(left_speed, right_speed);}
+++ /dev/null
-(how to use this
- (connect your robot to your computer via usb cable)
- (open arduino ide)
- (in arduino ide, open motor.ino)
- (hit the upload button)
- (your robot's wheels should start moving forward slowly)
- (yaay!)
- (you can reverse the direction a motor spins by plugging it into
- the motor controller differently. take the wires that come out of
- the motor and swap where they plug into the motor controller)
- (yaay!))
+++ /dev/null
-/*
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-// define which pins are connected to which components
-int right_motor_speed_pin = 3;
-int right_motor_forward_pin = 4;
-int right_motor_backward_pin = 5;
-
-int left_motor_speed_pin = 8;
-int left_motor_forward_pin = 9;
-int left_motor_backward_pin = 10;
-
-void setup(){
- // the arduino will change the voltage on these pins
- // therefore, these pins are OUTPUT pins
- pinMode(left_motor_speed_pin, OUTPUT);
- pinMode(left_motor_forward_pin, OUTPUT);
- pinMode(left_motor_backward_pin, OUTPUT);
-
- pinMode(right_motor_speed_pin, OUTPUT);
- pinMode(right_motor_forward_pin, OUTPUT);
- pinMode(right_motor_backward_pin, OUTPUT);}
-
-void loop(){
- digitalWrite(left_motor_backward_pin, LOW);
- digitalWrite(left_motor_forward_pin, HIGH);
- analogWrite(left_motor_speed_pin, 128);
-
- digitalWrite(right_motor_backward_pin, LOW);
- digitalWrite(right_motor_forward_pin, HIGH);
- analogWrite(right_motor_speed_pin, 128);}
+++ /dev/null
-(how to use this
- (connect your robot to your computer via usb cable)
- (open arduino ide)
- (in arduino ide, open pushbutton.ino)
- (hit the upload button)
- (hit control+shift+m,
- or go to the menu item tools, and down to serial monitor)
- (in the bottom right, select "9600 baud")
- (connect the breadboard and pushbutton as shown in "pushed.jpg")
- (hit the upload button in the arduino ide)
- (press the button!)
- (the message "button state changed!" should appear,
- and the arduino's built in LED should light up)
- (yaaay!))
+++ /dev/null
-/*
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of
- the License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-// pin 13 controls the arduino's built-in LED
-int led_pin = 13;
-// connect the button to pin 2
-int button_pin = 2;
-// initially, the button is not pressed.
-// 0 means not pressed, 1 means pressed
-int button_value = 0;
-
-void setup(){
- Serial.begin(9600);}
-
-void loop(){
- // see if you can figure out how this logic works
- if(digitalRead(button_pin) != button_value){
- Serial.println("button state changed!");}
- // read the value of the button pin during this loop,
- // and save it in a box named "button_value"
- button_value = digitalRead(button_pin);
- // write the value to the led pin
- digitalWrite(led_pin, button_value);}
+++ /dev/null
-/*
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-// define which pins are connected to which components
-int right_motor_speed_pin = 3;
-int right_motor_forward_pin = 4;
-int right_motor_backward_pin = 5;
-
-void setup(){
- // the arduino will change the voltage on these pins
- // therefore, these pins are OUTPUT pins
- pinMode(right_motor_speed_pin, OUTPUT);
- pinMode(right_motor_forward_pin, OUTPUT);
- pinMode(right_motor_backward_pin, OUTPUT);}
-
-void loop(){
- digitalWrite(right_motor_backward_pin, LOW);
- digitalWrite(right_motor_forward_pin, HIGH);
- analogWrite(right_motor_speed_pin, 255);}
+++ /dev/null
-/*
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-int right_echo_pin = 6;
-int right_trigger_pin = 7;
-
-int ping_microseconds = 0;
-double sound_cm_per_microsecond_at_sea_level = 0.034029;
-int count = 0;
-
-void setup(){
- Serial.begin(9600);
- pinMode(right_echo_pin, INPUT);
- pinMode(right_trigger_pin, OUTPUT);}
-
-void loop(){
- // make sure trigger pin is off, a.k.a. LOW
- digitalWrite(right_trigger_pin, LOW);
- delayMicroseconds(2);
-
- // send a 10 microsecond HIGH pulse to the trigger pin
- digitalWrite(right_trigger_pin, HIGH);
- delayMicroseconds(10); // leave the pin on for 10 microseconds
- digitalWrite(right_trigger_pin, LOW);
-
- ping_microseconds = pulseIn(right_echo_pin, HIGH);
-
- // wait for the sonar sensor hardware to recover from pinging
- delayMicroseconds(50);
-
- // print out the pulse time
- Serial.print(ping_microseconds);
- Serial.print(" = ping time (microseconds), ");
- Serial.print(ping_microseconds * sound_cm_per_microsecond_at_sea_level / 2);
- Serial.print(" = distance (cm). #");
- Serial.println(count++);
-
- // wait so it's easier to read the serial monitor.
- // change delay to 0 for fullspeed.
- // default is 333, which is about 1/3 of a second
- delay(333);}
+++ /dev/null
-(how to use this
- (connect your robot to your computer via usb cable)
- (open arduino ide)
- (in arduino ide, open serial.ino)
- (hit the upload button)
- (hit control+shift+m,
- or go to the menu item tools, and down to serial monitor)
- (in the bottom right, select "9600 baud")
- (hit the reset button on the arduino)
- (you should see "hello computer!" each time you hit the reset button,
- (this means your arduino is talking to your computer when it starts))
- (yaaay!))
+++ /dev/null
-/*
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of
- the License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-void setup(){
- Serial.begin(9600);
- Serial.println("hello computer!");}
-
-void loop(){}
+++ /dev/null
-/*
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-// define which pins are connected to which components
-int right_motor_speed_pin = 3; // orange wire
-int right_motor_forward_pin = 4; // brown
-int right_motor_backward_pin = 5; // purple
-
-int right_echo_pin = 6; // white
-int right_trigger_pin = 7; // blue
-
-int left_motor_speed_pin = 8; // orange
-int left_motor_forward_pin = 9; // brown
-int left_motor_backward_pin = 10; // purple
-
-int left_echo_pin = 11; // white
-int left_trigger_pin = 12; // blue
-
-void on(int pin){
- digitalWrite(pin, HIGH);}
-
-void off(int pin){
- digitalWrite(pin, LOW);}
-
-void set_motor(int speed_pin,
- int forward_pin,
- int backward_pin,
- int speed){
- if(speed > 0){
- off(backward_pin);
- on(forward_pin);}
- else if(speed < 0){
- off(forward_pin);
- on(backward_pin);
- speed = -speed;}
- else{ // speed is 0
- off(forward_pin);
- off(backward_pin);}
- if(speed > 255){
- speed = 255;}
- analogWrite(speed_pin, speed);}
-
-void go(int left_motor_speed, int right_motor_speed){
- set_motor(left_motor_speed_pin,
- left_motor_forward_pin,
- left_motor_backward_pin,
- left_motor_speed);
- set_motor(right_motor_speed_pin,
- right_motor_forward_pin,
- right_motor_backward_pin,
- right_motor_speed);}
-
-int ping(int trigger, int echo){
- int ping_time = 0;
- // turn off trigger
- off(trigger);
- delayMicroseconds(2);
- // turn on the trigger and leave it on long enough for the
- // sonar sensor to notice
- on(trigger);
- delayMicroseconds(10);
- off(trigger);
- ping_time = pulseIn(echo, HIGH);
- if(ping_time <= 0){
- ping_time = 3000;}
- // sonar needs some time to recover before pinging again,
- // so make sure it gets enough sleep right here. 50 milliseconds
- delay(50);
- return ping_time;}
-
-void backup(int backup_time){
- go(-250, -250);
- delay(backup_time);}
-
-void turn_around(int turn_around_time){
- go(-250, 250);
- delay(turn_around_time);}
-
-void setup(){
- Serial.begin(9600);
-
- pinMode(right_motor_speed_pin, OUTPUT);
- pinMode(right_motor_forward_pin, OUTPUT);
- pinMode(right_motor_backward_pin, OUTPUT);
-
- pinMode(right_echo_pin, INPUT);
- pinMode(right_trigger_pin, OUTPUT);
-
- pinMode(left_motor_speed_pin, OUTPUT);
- pinMode(left_motor_forward_pin, OUTPUT);
- pinMode(left_motor_backward_pin, OUTPUT);
-
- pinMode(left_echo_pin, INPUT);
- pinMode(left_trigger_pin, OUTPUT);
-
- off(left_motor_speed_pin);
- off(left_motor_forward_pin);
- off(left_motor_backward_pin);
- off(left_trigger_pin);
-
- off(right_motor_speed_pin);
- off(right_motor_forward_pin);
- off(right_motor_backward_pin);
- off(right_trigger_pin);}
-
-void loop(){
- int left_speed;
- int right_speed;
-
- int forward_speed = 250;
- int stop_speed = 0;
-
- // adjust this number as necessary for your robot.
- // it represents how far the table is from your sonar sensor.
- // larger values mean larger distance. default is 400
- int right_max_ping_time_over_table = 400;
- int left_max_ping_time_over_table = 400;
- int backup_time = 2000;
- // the exact amount of time for turning around might need
- // twerking for your robot. the default value is 3200
- int turn_around_time = 3200;
-
- int actual_left_ping_time = ping(left_trigger_pin, left_echo_pin);
- int actual_right_ping_time = ping(right_trigger_pin, right_echo_pin);
-
- Serial.print("left ping = ");
- Serial.print(actual_left_ping_time);
- Serial.print("\tright_ping = ");
- Serial.println(actual_right_ping_time);
-
- // if the left sonar senses a table, keep driving left side forward,
- // otherwise, stop left side
- if(actual_left_ping_time < left_max_ping_time_over_table){
- left_speed = forward_speed;}
- else{
- left_speed = stop_speed;}
- // if the right sonar senses a table, keep driving right side forward,
- // otherwise, stop right side
- if(actual_right_ping_time < right_max_ping_time_over_table){
- right_speed = forward_speed;}
- else{
- right_speed = stop_speed;}
-
- // if both sonars detect being off the table, backup and turn around
- // otherwise, go the correct speed for each wheel
- if(actual_left_ping_time >= left_max_ping_time_over_table
- && actual_right_ping_time >= right_max_ping_time_over_table){
- Serial.println("backing up");
- backup(backup_time);
- Serial.println("turning around");
- turn_around(turn_around_time);}
- else{
- Serial.println("going");
- go(left_speed, right_speed);}}
+++ /dev/null
-/*
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-// define which pins are connected to which components
-int right_motor_speed_pin = 3;
-int right_motor_forward_pin = 4;
-int right_motor_backward_pin = 5;
-
-int left_motor_speed_pin = 8;
-int left_motor_forward_pin = 9;
-int left_motor_backward_pin = 10;
-
-void on(int pin){
- digitalWrite(pin, HIGH);}
-
-void off(int pin){
- digitalWrite(pin, LOW);}
-
-void setup(){
- // the arduino will change the voltage on these pins
- // therefore, these pins are OUTPUT pins
- pinMode(right_motor_speed_pin, OUTPUT);
- pinMode(right_motor_forward_pin, OUTPUT);
- pinMode(right_motor_backward_pin, OUTPUT);
-
- pinMode(left_motor_speed_pin, OUTPUT);
- pinMode(left_motor_forward_pin, OUTPUT);
- pinMode(left_motor_backward_pin, OUTPUT);}
-
-void loop(){
- off(right_motor_backward_pin);
- on(right_motor_forward_pin);
- analogWrite(right_motor_speed_pin, 255);
-
- off(left_motor_backward_pin);
- on(left_motor_forward_pin);
- analogWrite(left_motor_speed_pin, 255);}
--- /dev/null
+(how to use this
+ (connect your robot to your computer via usb cable)
+ (open arduino ide)
+ (in arduino ide, open serial.ino)
+ (hit the upload button)
+ (hit control+shift+m,
+ or go to the menu item tools, and down to serial monitor)
+ (in the bottom right, select "9600 baud")
+ (hit the reset button on the arduino)
+ (you should see "hello computer!" each time you hit the reset button,
+ (this means your arduino is talking to your computer when it starts))
+ (yaaay!))
--- /dev/null
+/*
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as
+ published by the Free Software Foundation, either version 3 of
+ the License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+void setup(){
+ Serial.begin(9600);
+ Serial.println("hello computer!");}
+
+void loop(){}
--- /dev/null
+/*
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+int right_echo_pin = 6;
+int right_trigger_pin = 7;
+
+int ping_microseconds = 0;
+double sound_cm_per_microsecond_at_sea_level = 0.034029;
+int count = 0;
+
+void setup(){
+ Serial.begin(9600);
+ pinMode(right_echo_pin, INPUT);
+ pinMode(right_trigger_pin, OUTPUT);}
+
+void loop(){
+ // make sure trigger pin is off, a.k.a. LOW
+ digitalWrite(right_trigger_pin, LOW);
+ delayMicroseconds(2);
+
+ // send a 10 microsecond HIGH pulse to the trigger pin
+ digitalWrite(right_trigger_pin, HIGH);
+ delayMicroseconds(10); // leave the pin on for 10 microseconds
+ digitalWrite(right_trigger_pin, LOW);
+
+ ping_microseconds = pulseIn(right_echo_pin, HIGH);
+
+ // wait for the sonar sensor hardware to recover from pinging
+ delayMicroseconds(50);
+
+ // print out the pulse time
+ Serial.print(ping_microseconds);
+ Serial.print(" = ping time (microseconds), ");
+ Serial.print(ping_microseconds * sound_cm_per_microsecond_at_sea_level / 2);
+ Serial.print(" = distance (cm). #");
+ Serial.println(count++);
+
+ // wait so it's easier to read the serial monitor.
+ // change delay to 0 for fullspeed.
+ // default is 333, which is about 1/3 of a second
+ delay(333);}
--- /dev/null
+/*
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+int right_echo_pin = 6;
+int right_trigger_pin = 7;
+
+int left_echo_pin = 11;
+int left_trigger_pin = 12;
+
+int right_ping_microseconds = 0;
+int left_ping_microseconds = 0;
+double sound_cm_per_microsecond_at_sea_level = 0.034029;
+int count = 0;
+
+void on(int pin){
+ digitalWrite(pin, HIGH);}
+
+void off(int pin){
+ digitalWrite(pin, LOW);}
+
+int ping(int trigger, int echo){
+ int ping_time = 0;
+ // turn off trigger
+ off(trigger);
+ delayMicroseconds(2);
+ // turn on the trigger and leave it on long enough for the
+ // sonar sensor to notice
+ on(trigger);
+ delayMicroseconds(10);
+ off(trigger);
+ ping_time = pulseIn(echo, HIGH);
+ if(ping_time <= 0){
+ ping_time = 3000;}
+ // sonar needs some time to recover before pinging again,
+ // so make sure it gets enough sleep right here. 50 milliseconds
+ delay(50);
+ return ping_time;}
+
+void setup(){
+ Serial.begin(9600);
+ pinMode(right_echo_pin, INPUT);
+ pinMode(right_trigger_pin, OUTPUT);
+ pinMode(left_echo_pin, INPUT);
+ pinMode(left_trigger_pin, OUTPUT);}
+
+void loop(){
+ right_ping_microseconds = ping(right_trigger_pin, right_echo_pin);
+ left_ping_microseconds = ping(left_trigger_pin, left_echo_pin);
+ // print out the pulse time
+ Serial.print(right_ping_microseconds);
+ Serial.print(" = right ping microseconds, ");
+ Serial.print(left_ping_microseconds);
+ Serial.print(" = left ping microseconds. #");
+ Serial.println(count++);
+
+ // wait so it's easier to read the serial monitor.
+ // change delay to 0 for fullspeed.
+ // default is 333, which is about 1/3 of a second
+ delay(333);}
--- /dev/null
+/*
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// define which pins are connected to which components
+int right_motor_speed_pin = 3;
+int right_motor_forward_pin = 4;
+int right_motor_backward_pin = 5;
+
+void setup(){
+ // the arduino will change the voltage on these pins
+ // therefore, these pins are OUTPUT pins
+ pinMode(right_motor_speed_pin, OUTPUT);
+ pinMode(right_motor_forward_pin, OUTPUT);
+ pinMode(right_motor_backward_pin, OUTPUT);}
+
+void loop(){
+ digitalWrite(right_motor_backward_pin, LOW);
+ digitalWrite(right_motor_forward_pin, HIGH);
+ analogWrite(right_motor_speed_pin, 255);}
--- /dev/null
+/*
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// define which pins are connected to which components
+int right_motor_speed_pin = 3;
+int right_motor_forward_pin = 4;
+int right_motor_backward_pin = 5;
+
+int left_motor_speed_pin = 8;
+int left_motor_forward_pin = 9;
+int left_motor_backward_pin = 10;
+
+void on(int pin){
+ digitalWrite(pin, HIGH);}
+
+void off(int pin){
+ digitalWrite(pin, LOW);}
+
+void setup(){
+ // the arduino will change the voltage on these pins
+ // therefore, these pins are OUTPUT pins
+ pinMode(right_motor_speed_pin, OUTPUT);
+ pinMode(right_motor_forward_pin, OUTPUT);
+ pinMode(right_motor_backward_pin, OUTPUT);
+
+ pinMode(left_motor_speed_pin, OUTPUT);
+ pinMode(left_motor_forward_pin, OUTPUT);
+ pinMode(left_motor_backward_pin, OUTPUT);}
+
+void loop(){
+ off(right_motor_backward_pin);
+ on(right_motor_forward_pin);
+ analogWrite(right_motor_speed_pin, 255);
+
+ off(left_motor_backward_pin);
+ on(left_motor_forward_pin);
+ analogWrite(left_motor_speed_pin, 255);}
--- /dev/null
+/*
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// define which pins are connected to which components
+int right_motor_speed_pin = 3;
+int right_motor_forward_pin = 4;
+int right_motor_backward_pin = 5;
+
+int left_motor_speed_pin = 8;
+int left_motor_forward_pin = 9;
+int left_motor_backward_pin = 10;
+
+void on(int pin){
+ digitalWrite(pin, HIGH);}
+
+void off(int pin){
+ digitalWrite(pin, LOW);}
+
+void setup(){
+ // the arduino will change the voltage on these pins
+ // therefore, these pins are OUTPUT pins
+ pinMode(right_motor_speed_pin, OUTPUT);
+ pinMode(right_motor_forward_pin, OUTPUT);
+ pinMode(right_motor_backward_pin, OUTPUT);
+
+ pinMode(left_motor_speed_pin, OUTPUT);
+ pinMode(left_motor_forward_pin, OUTPUT);
+ pinMode(left_motor_backward_pin, OUTPUT);}
+
+void loop(){
+ off(right_motor_backward_pin);
+ on(right_motor_forward_pin);
+ analogWrite(right_motor_speed_pin, 255);
+
+ off(left_motor_backward_pin);
+ on(left_motor_forward_pin);
+ analogWrite(left_motor_speed_pin, 255);}
--- /dev/null
+/*
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// define which pins are connected to which components
+int right_motor_speed_pin = 3; // orange wire
+int right_motor_forward_pin = 4; // brown
+int right_motor_backward_pin = 5; // purple
+
+int right_echo_pin = 6; // white
+int right_trigger_pin = 7; // blue
+
+int left_motor_speed_pin = 8; // orange
+int left_motor_forward_pin = 9; // brown
+int left_motor_backward_pin = 10; // purple
+
+int left_echo_pin = 11; // white
+int left_trigger_pin = 12; // blue
+
+// you'll need to adjust these based on your sonar sensor's behavior
+int desired_right_ping_time = 800;
+int desired_left_ping_time = 800;
+
+void on(int pin){
+ digitalWrite(pin, HIGH);}
+
+void off(int pin){
+ digitalWrite(pin, LOW);}
+
+void set_motor(int speed_pin,
+ int forward_pin,
+ int backward_pin,
+ int speed){
+ if(speed > 0){
+ off(backward_pin);
+ on(forward_pin);}
+ else if(speed < 0){
+ off(forward_pin);
+ on(backward_pin);
+ speed = -speed;}
+ else{ // speed is 0
+ off(forward_pin);
+ off(backward_pin);}
+ // since speed has been set positive, no need to check if speed < -255.
+ if(speed > 255){
+ speed = 255;}
+ analogWrite(speed_pin, speed);}
+
+void go(int left_motor_speed, int right_motor_speed){
+ set_motor(left_motor_speed_pin,
+ left_motor_forward_pin,
+ left_motor_backward_pin,
+ left_motor_speed);
+ set_motor(right_motor_speed_pin,
+ right_motor_forward_pin,
+ right_motor_backward_pin,
+ right_motor_speed);}
+
+int ping(int trigger, int echo){
+ int ping_time = 0;
+ // turn off trigger
+ off(trigger);
+ delayMicroseconds(2);
+ // turn on the trigger and leave it on long enough for the
+ // sonar sensor to notice
+ on(trigger);
+ delayMicroseconds(10);
+ off(trigger);
+ ping_time = pulseIn(echo, HIGH);
+ if(ping_time <= 0){
+ ping_time = 3000;}
+ // sonar needs some time to recover before pinging again,
+ // so make sure it gets enough sleep right here. 50 milliseconds
+ delay(50);
+ return ping_time;}
+
+void backup(int backup_time){
+ go(-250, -250);
+ delay(backup_time);}
+
+void turn_around(int turn_around_time){
+ go(-250, 250);
+ delay(turn_around_time);}
+
+void setup(){
+ Serial.begin(9600);
+
+ pinMode(right_motor_speed_pin, OUTPUT);
+ pinMode(right_motor_forward_pin, OUTPUT);
+ pinMode(right_motor_backward_pin, OUTPUT);
+
+ pinMode(right_echo_pin, INPUT);
+ pinMode(right_trigger_pin, OUTPUT);
+
+ pinMode(left_motor_speed_pin, OUTPUT);
+ pinMode(left_motor_forward_pin, OUTPUT);
+ pinMode(left_motor_backward_pin, OUTPUT);
+
+ pinMode(left_echo_pin, INPUT);
+ pinMode(left_trigger_pin, OUTPUT);
+
+ off(left_motor_speed_pin);
+ off(left_motor_forward_pin);
+ off(left_motor_backward_pin);
+ off(left_trigger_pin);
+
+ off(right_motor_speed_pin);
+ off(right_motor_forward_pin);
+ off(right_motor_backward_pin);
+ off(right_trigger_pin);}
+
+void loop(){
+ int left_speed;
+ int right_speed;
+
+ int forward_speed = 250;
+ int stop_speed = 0;
+
+ unsigned int actual_left_ping_time = ping(left_trigger_pin, left_echo_pin);
+ unsigned int actual_right_ping_time = ping(right_trigger_pin, right_echo_pin);
+
+ left_speed = actual_left_ping_time - desired_left_ping_time;
+ right_speed = actual_right_ping_time - desired_right_ping_time;
+
+ Serial.print("left: ping = ");
+ Serial.print(actual_left_ping_time);
+ Serial.print(" speed = ");
+ Serial.print(left_speed);
+ Serial.print(" right: ping = ");
+ Serial.print(actual_right_ping_time);
+ Serial.print(" speed = ");
+ Serial.println(right_speed);
+
+ go(left_speed, right_speed);}
--- /dev/null
+/*
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as
+ published by the Free Software Foundation, either version 3 of the
+ License, or (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// define which pins are connected to which components
+int right_motor_speed_pin = 3; // orange wire
+int right_motor_forward_pin = 4; // brown
+int right_motor_backward_pin = 5; // purple
+
+int right_echo_pin = 6; // white
+int right_trigger_pin = 7; // blue
+
+int left_motor_speed_pin = 8; // orange
+int left_motor_forward_pin = 9; // brown
+int left_motor_backward_pin = 10; // purple
+
+int left_echo_pin = 11; // white
+int left_trigger_pin = 12; // blue
+
+void on(int pin){
+ digitalWrite(pin, HIGH);}
+
+void off(int pin){
+ digitalWrite(pin, LOW);}
+
+void set_motor(int speed_pin,
+ int forward_pin,
+ int backward_pin,
+ int speed){
+ if(speed > 0){
+ off(backward_pin);
+ on(forward_pin);}
+ else if(speed < 0){
+ off(forward_pin);
+ on(backward_pin);
+ speed = -speed;}
+ else{ // speed is 0
+ off(forward_pin);
+ off(backward_pin);}
+ if(speed > 255){
+ speed = 255;}
+ analogWrite(speed_pin, speed);}
+
+void go(int left_motor_speed, int right_motor_speed){
+ set_motor(left_motor_speed_pin,
+ left_motor_forward_pin,
+ left_motor_backward_pin,
+ left_motor_speed);
+ set_motor(right_motor_speed_pin,
+ right_motor_forward_pin,
+ right_motor_backward_pin,
+ right_motor_speed);}
+
+int ping(int trigger, int echo){
+ int ping_time = 0;
+ // turn off trigger
+ off(trigger);
+ delayMicroseconds(2);
+ // turn on the trigger and leave it on long enough for the
+ // sonar sensor to notice
+ on(trigger);
+ delayMicroseconds(10);
+ off(trigger);
+ ping_time = pulseIn(echo, HIGH);
+ if(ping_time <= 0){
+ ping_time = 3000;}
+ // sonar needs some time to recover before pinging again,
+ // so make sure it gets enough sleep right here. 50 milliseconds
+ delay(50);
+ return ping_time;}
+
+void backup(int backup_time){
+ go(-250, -250);
+ delay(backup_time);}
+
+void turn_around(int turn_around_time){
+ go(-250, 250);
+ delay(turn_around_time);}
+
+void setup(){
+ Serial.begin(9600);
+
+ pinMode(right_motor_speed_pin, OUTPUT);
+ pinMode(right_motor_forward_pin, OUTPUT);
+ pinMode(right_motor_backward_pin, OUTPUT);
+
+ pinMode(right_echo_pin, INPUT);
+ pinMode(right_trigger_pin, OUTPUT);
+
+ pinMode(left_motor_speed_pin, OUTPUT);
+ pinMode(left_motor_forward_pin, OUTPUT);
+ pinMode(left_motor_backward_pin, OUTPUT);
+
+ pinMode(left_echo_pin, INPUT);
+ pinMode(left_trigger_pin, OUTPUT);
+
+ off(left_motor_speed_pin);
+ off(left_motor_forward_pin);
+ off(left_motor_backward_pin);
+ off(left_trigger_pin);
+
+ off(right_motor_speed_pin);
+ off(right_motor_forward_pin);
+ off(right_motor_backward_pin);
+ off(right_trigger_pin);}
+
+void loop(){
+ int left_speed;
+ int right_speed;
+
+ int forward_speed = 250;
+ int stop_speed = 0;
+
+ // adjust this number as necessary for your robot.
+ // it represents how far the table is from your sonar sensor.
+ // larger values mean larger distance. default is 400
+ int right_max_ping_time_over_table = 400;
+ int left_max_ping_time_over_table = 400;
+ int backup_time = 2000;
+ // the exact amount of time for turning around might need
+ // twerking for your robot. the default value is 3200
+ int turn_around_time = 3200;
+
+ int actual_left_ping_time = ping(left_trigger_pin, left_echo_pin);
+ int actual_right_ping_time = ping(right_trigger_pin, right_echo_pin);
+
+ Serial.print("left ping = ");
+ Serial.print(actual_left_ping_time);
+ Serial.print("\tright_ping = ");
+ Serial.println(actual_right_ping_time);
+
+ // if the left sonar senses a table, keep driving left side forward,
+ // otherwise, stop left side
+ if(actual_left_ping_time < left_max_ping_time_over_table){
+ left_speed = forward_speed;}
+ else{
+ left_speed = stop_speed;}
+ // if the right sonar senses a table, keep driving right side forward,
+ // otherwise, stop right side
+ if(actual_right_ping_time < right_max_ping_time_over_table){
+ right_speed = forward_speed;}
+ else{
+ right_speed = stop_speed;}
+
+ // if both sonars detect being off the table, backup and turn around
+ // otherwise, go the correct speed for each wheel
+ if(actual_left_ping_time >= left_max_ping_time_over_table
+ && actual_right_ping_time >= right_max_ping_time_over_table){
+ Serial.println("backing up");
+ backup(backup_time);
+ Serial.println("turning around");
+ turn_around(turn_around_time);}
+ else{
+ Serial.println("going");
+ go(left_speed, right_speed);}}