Hello. I am doing a robotics project in which I build a robot that uses a sensor to detect an object (in this case, trash). The robot will first move a certain distance using DC motors. As it drives toward the object, the sensor will detect the object, and the buzzer will buzz. After that, the two servo motors at the front that have two sticks that will move via the servo motors. I have the DC motor codes, the ultrasonic and buzzer codes, and the two servo codes that I'm using. I'll also be testing a PIR sensor later on once I find a code for it. My main question is, how will I combine the first three code mentioned so the robot can work altogether? I'm not really sure where to start.
the DC motors code:
/**
* Bruno Santos, 2013
* feiticeir0@whatgeek.com.pt
* Small code to test DC motors - 2x with a L298 Dual H-Bridge Motor Driver
* Free to share
**/
//Testing the DC Motors with
// L293D
//Define Pins
//Motor A
int enableA = 5;
int MotorA1 = 6;
int MotorA2 = 7;
//Motor B
int enableB = 8;
int MotorB1 = 9;
int MotorB2 = 10;
void setup() {
Serial.begin (9600);
//configure pin modes
pinMode (enableA, OUTPUT);
pinMode (MotorA1, OUTPUT);
pinMode (MotorA2, OUTPUT);
pinMode (enableB, OUTPUT);
pinMode (MotorB1, OUTPUT);
pinMode (MotorB2, OUTPUT);
}
void loop() {
//enabling motor A
Serial.println ("Enabling Motors");
digitalWrite (enableA, HIGH);
digitalWrite (enableB, HIGH);
delay (1000);
//do something
Serial.println ("Motion Forward");
digitalWrite (MotorA1, LOW);
digitalWrite (MotorA2, HIGH);
digitalWrite (MotorB1, LOW);
digitalWrite (MotorB2, HIGH);
//9s forward
delay (9000);
Serial.println ("Motion Backwards");
//reverse
digitalWrite (MotorA1,HIGH);
digitalWrite (MotorA2,LOW);
digitalWrite (MotorB1,HIGH);
digitalWrite (MotorB2,LOW);
Serial.println ("Stoping motors");
//stop
digitalWrite (enableA, LOW);
digitalWrite (enableB, LOW);
delay(9000);
//9s backwards
delay(9000);
}
The ultrasonic sensor and buzzer code:
/*
This code should work to get warning cross the buzzer when something be closer than 0.5 meter
Circuit is ultrasonic sensor and buzzer +5v and Arduino uno is used
a_atef45@yahoo.com
www.zerosnones.net
+201153300223
*/
// Define pins for ultrasonic and buzzer
int const trigPin = 12;
int const echoPin = 11;
int const buzzPin = 2;
void setup()
{
pinMode(trigPin, OUTPUT); // trig pin will have pulses output
pinMode(echoPin, INPUT); // echo pin should be input to get pulse width
pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
}
void loop()
{
// Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters
int duration, distance;
// Output pulse with 1ms width on trigPin
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
// Measure the pulse input in echo pin
duration = pulseIn(echoPin, HIGH);
// Distance is half the duration devided by 29.1 (from datasheet)
distance = (duration/2) / 29.1;
// if distance less than 0.5 meter and more than 0 (0 or less means over range)
if (distance <= 50 && distance >= 0) {
// Buzz
digitalWrite(buzzPin, HIGH);
} else {
// Don't buzz
digitalWrite(buzzPin, LOW);
}
// Waiting 60 ms won't hurt any one
delay(60);
}
and the two servos code:
#include <Servo.h>
Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo
void setup() {
servoLeft.attach(10); // Set left servo to digital pin 10
servoRight.attach(9); // Set right servo to digital pin 9
}
void loop() { // Loop through motion tests
forward(); // Example: move forward
delay(2000); // Wait 2000 milliseconds (2 seconds)
reverse();
delay(2000);
stopRobot();
delay(2000);
}
// Motion routines for forward, reverse, turns, and stop
void forward() {
servoLeft.write(0);
servoRight.write(180);
}
void reverse() {
servoLeft.write(180);
servoRight.write(0);
}
void stopRobot() {
servoLeft.write(90);
servoRight.write(90);
}
I know this is a lot of code to go through, but any help on this would be greatly appreciated. Thank you.