Monday, December 9, 2013

November 27 - Third Attempt

Second code failed. This code did make the ping sensor to work (awesome!!) but the only thing that worked was the ping sensor. The RC motors didn't want to turn on (not good), we have a working sensor but not a working car. Again we change the language to make it our own but unfortunately the RC did not want to budge. All until we came across this code (they do say the third one is the charm):
// Use this code to test your Obstacle Avoiding vehicle with the Arduino board:
// this constant won't change. It's the pin number
// of the sensor's output:
const int pingPin = 7;
// Motors
int motor_left[] = {6,8};
int motor_right[] = {3,5};
int ledPin = 13; // LED connected to digital pin 13
// Setup
void setup() {
Serial.begin(9600);
// Setup motors
int i;
for(i = 0; i < 3; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
pinMode(ledPin, OUTPUT);
}
digitalWrite(motor_left[2], HIGH);
digitalWrite(motor_right[2], HIGH);
}
// Loop
void loop() {
long duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(duration); Serial.println();
Serial.print(inches); Serial.print("in, ");
Serial.print(cm); Serial.print("cm");
Serial.println();
if (cm>10)
{
Serial.println("Moving Forward");
drive_forward();
delay(1000);
}
else
{
if (cm<10)
{
Serial.println("Obstacle Very Close");
Serial.println("Evasive Manuever");
motor_stop();
drive_backward();
delay(200);
}
else
{
motor_stop();
Serial.println("Turning Left");
turn_left();
Serial.println("Reverse");
drive_backward();
delay(800);
Serial.println("Turning Right");
turn_right();
Serial.println("Moving Forward");
drive_forward();
delay(500);
drive_straight();
drive_forward();
delay(1000);
}
}
motor_active();
motor_inactive();
delay(1);
}
// Drive
void motor_active(){
digitalWrite(ledPin, HIGH); // set the LED on
delay(500); // wait for a second
}
void motor_inactive(){
digitalWrite(ledPin, LOW); // set the LED off
delay(500); // wait for a second
}
void motor_stop(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);
delay(25);
}
void drive_straight(){
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
}
void drive_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
}
void drive_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
}
void turn_left(){
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void turn_right(){
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
long microsecondsToInches(long microseconds)
{
// there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
return microseconds / 148;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 58;
}
view raw Final Attempt hosted with ❤ by GitHub
This code works!!!! Our ping sensor was picking up obstacles and also was making the RC move forward when no obstacles were present and going in reverse when there was an obstacle. This code also made the wheels turn. We changed a lot of line to make it our own.

No comments:

Post a Comment