Go Down

Topic: arduino rescue bot (Read 697 times) previous topic - next topic

hello i am trying to make a robot that recocnises objects and walls by  combining two ultrasonic sensors but i cant get it to work plis help me
#include <Servo.h>                                  //includes the servo library
#include <NewPing.h>
int motor_pin1 = 4;
int motor_pin2 = 5;
int motor_pin3 = 6;
int motor_pin4 = 7;
int servopin = 8;
int sensorpin = 0;
int leftdist = 0;
int rightdist = 0;
int object = 5;             //distance at which the robot should look for another route                          
NewPing sonar1(11, 12, 200); // Sensor 1: trigger pin, echo pin, maximum distance in cm
NewPing sonar2(9, 10, 200);
Servo myservo;
#define pingSpeed 1000 // Ping frequency (in milliseconds), fastest we should ping is about 35ms per sensor
unsigned long pingTimer1, pingTimer2;
int in1, in2;

void setup ()
{
 pingTimer1 = millis() + pingSpeed; // Sensor 1 fires after 1 second (pingSpeed)
 pingTimer2 = pingTimer1 + 35; // Sensor 2 fires 35ms later
 pinMode(motor_pin1,OUTPUT);
 pinMode(motor_pin2,OUTPUT);
 pinMode(motor_pin3,OUTPUT);
 pinMode(motor_pin4,OUTPUT);
 myservo.attach(servopin);
 myservo.write(90);
 delay(700);
}
void loop(){
{if (millis() >= pingTimer1) {
   pingTimer1 += pingSpeed; // Make sensor 1 fire again 1 second later (pingSpeed)
   in1 = sonar1.ping_in();
 }
 if (millis() >= pingTimer2) {
   pingTimer2 = pingTimer1 + 35; // Make sensor 2 fire again 35ms after sensor 1 fires
   in2 = sonar2.ping_in();}}

if (in2  <  object) && (in1 < object)
{ forward();
}
if (in2  > object) && (in1 > object){
findroute();
}
 //if (in2  < object) && (in1 > object)
//grap():
}

void forward() {                            // use combination which works for you
  digitalWrite(motor_pin1,HIGH);
  digitalWrite(motor_pin2,LOW);
  digitalWrite(motor_pin3,HIGH);
  digitalWrite(motor_pin4,LOW);
  return;
}

void findroute() {
 halt();                                             // stop
 backward();                                       //go backwards
 lookleft();                                      //go to subroutine lookleft
 lookright();                                   //go to subroutine lookright
                                     
 if ( leftdist < rightdist )
 {
   turnleft();
 }
else
{
  turnright ();
}
}

void backward() {
 digitalWrite(motor_pin1,LOW);
 digitalWrite(motor_pin2,HIGH);
 digitalWrite(motor_pin3,LOW);
 digitalWrite(motor_pin4,HIGH);
 delay(500);
 halt();
 return;
}

void halt () {
 digitalWrite(motor_pin1,LOW);
 digitalWrite(motor_pin2,LOW);
 digitalWrite(motor_pin3,LOW);
 digitalWrite(motor_pin4,LOW);
 delay(500);                          //wait after stopping
 return;
}

void lookleft() {
 myservo.write(150);
 delay(700);                                //wait for the servo to get there
 leftdist= in2();
 myservo.write(90);
 delay(700);                                 //wait for the servo to get there
 return;
}

void lookright () {
 myservo.write(30);
 delay(700);                           //wait for the servo to get there
 rightdist = in2();
 myservo.write(90);                                  
 delay(700);                        //wait for the servo to get there
 return;
}

void turnleft () {
 digitalWrite(motor_pin1,HIGH);       //use the combination which works for you
 digitalWrite(motor_pin2,LOW);      //right motor rotates forward and left motor backward
 digitalWrite(motor_pin3,LOW);
 digitalWrite(motor_pin4,HIGH);
 delay(1000);                     // wait for the robot to make the turn
 halt();
 return;
}

void turnright () {
 digitalWrite(motor_pin1,LOW);       //use the combination which works for you
 digitalWrite(motor_pin2,HIGH);    //left motor rotates forward and right motor backward
 digitalWrite(motor_pin3,HIGH);
 digitalWrite(motor_pin4,LOW);
 delay(1000);                              // wait for the robot to make the turn
 halt();
 return;
}
void

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy