Pages: [1]   Go Down
Author Topic: Need help with a wall avoiding code.  (Read 471 times)
0 Members and 1 Guest are viewing this topic.
Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello.
I have build a simple robot with two gearmotors and a ping sensor on top of a servo, but I am not sure how I should make the code if I want a servo to do a sweep when it detects an object infront of it. And perhaps store a value for when it is turned right and left, so it can decide weather it should go right/left or backwards.

I appreciate all answers very much smiley
Thank you.

Here is the code i made for it to simply turn left if it detects an object.
Code:
const int pingPin = 3;
int ledPin = 5;
int motor1A = 10;
int motor1B = 11;
int enable1 = 7;
int motor2A = 8;
int motor2B = 9;
int enable2 = 6;
float dist = 0;
float Val;



void straight()
{
 digitalWrite(enable1, HIGH);
 digitalWrite(motor1A, HIGH);
 digitalWrite(motor1B, LOW);
 digitalWrite(enable2, HIGH);
 digitalWrite(motor2A, HIGH);
 digitalWrite(motor2B, LOW);
}
 
 
void turnLeft()
{
  digitalWrite(enable1, HIGH);
  digitalWrite(motor1A, LOW);
  digitalWrite(motor1B, HIGH);
  digitalWrite(enable2, HIGH);
  digitalWrite(motor2A, HIGH);
  digitalWrite(motor2B, LOW);

 
 
float distCalc()
{

  long duration, cm;
 
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
 
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
 
  cm = microsecondsToCentimeters(duration);

   long microsecondsToCentimeters(long microseconds)
    {
      dist = (microseconds / 29 / 2);
      return dist;
    }
 
 
void wall()
{
  if(dist < 35)
  {
    turnLeft();
    delay(100);
  } 
 
  else
  {
    straight();
  } 
}   
 
 
void led()
{
  if(dist > 35)
  {
    digitalWrite(ledPin, LOW);
    delay(100);
  }

  else
  {
    digitalWrite(ledPin, HIGH);
    delay(100);
  }
}

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(motor1A, OUTPUT);
  pinMode(motor1B, OUTPUT);
  pinMode(motor2A, OUTPUT);
  pinMode(motor2B, OUTPUT);
 
 
  straight();
 
 
 
}

void loop()
{
  digitalWrite(pingPin, HIGH);
  distCalc();
  wall();
  led();
  delay(20);
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49034
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First, distCalc() is defined with a return type of float, which means that it should return a value. There is no return statement in the function, so it does not actually return a value.

Therefore, when loop() calls distCalc(), the reading that is determined in distCalc() is lost when distCalc() ends.

That reading would be based on which way the sensor was facing when the function was called. You need to mount the sensor on a servo, and position the servo prior to taking a reading. The distCalc() function should take an argument - the position to send the servo to, prior to taking a reading.

The distCalc() function should then send the servo to that position, pause while it travels there (as short a pause as possible), then take a reading, and return a value.

Then, in loop(), call distCalc() in a loop, starting at some small value, incrementing in large steps, ending at some large value, like:
Code:
for(int pos = minAngle; pos<=maxAngle; pos+=(maxAngle-minAngle)/2)
{
}
where minAngle and maxAngle represent the physical limits of the servo. If minAngle is 0 and maxAngle is 180, this loop would position the servo at 0, 90, and 180 to take readings.

Store the readings in an array, and then loop through the array to find the largest distance detected, and make the robot go in that corresponding direction.
Logged

Pages: [1]   Go Up
Jump to: