Go Down

Topic: Robot Help (Read 380 times) previous topic - next topic

RoboDev

I am having a problem with my robot code and delay():
I tried using millis() but it's not allowing me to read my sensor any quicker. Any help is appreciated as I am dumbfounded at the moment. Thank you in advance.
Also; I accidentally posted this on a delay() help thread... sorry about that.
Code:
Quote

#include <Servo.h>
Servo Direction;
Servo DriveR;
Servo DriveL;
const int forwardThreshold = 30;// 12  inches
int forwardDist;
int RightDist;
int LeftDist;
unsigned long sensorWait = 150, servoWait = 500;
void setup()
{
  Serial.begin(9600); //for debugging
  DriveR.attach(9);
  DriveL.attach(11);
  Direction.attach(10);
}

void loop()
//needs improvement.
{  
  int sensor = AquireDist();
  DriveR.write(360);
  DriveL.write(-360);
  if(sensor < forwardThreshold)
  {
    DriveR.detach();
    DriveL.detach();
    Direction.write(90);
    
    forwardDist = AquireDist();
    Serial.println(forwardDist); // for debugging
    if(millis()>= sensorWait);
    sensorWait = sensorWait + millis();
    Direction.write(0);
    if(millis()>= servoWait);
    servoWait = servoWait + millis();
    LeftDist = AquireDist();
    Serial.println(LeftDist);// for debugging
     if(millis()>= sensorWait);
     sensorWait = sensorWait + millis()+150;
    Direction.write(200);
    delay(300);
    RightDist = AquireDist();
    Serial.println(RightDist);// for debugging
     if(millis()>= sensorWait);
     sensorWait = sensorWait + millis()+300;
    Direction.write(90);
    if(millis()>= servoWait);
    servoWait = servoWait + millis()+ 150;
    DriveR.attach(9);
    DriveL.attach(11);
  }
  if(RightDist> LeftDist && RightDist > forwardThreshold)
  {
    DriveL.write(360);
    delay(1500);
  }

  else
  {
    DriveR.write(360);
    delay(1500);
  }

   if(sensor > forwardDist && sensor > forwardThreshold)
  {}// do nothing, go back to  beginning... goto is messy when used too often

}

int AquireDist() // this can be used with any digital in/out sonar type distance sensor (PWM).
{
  const  int sensor = 7;
  int duration;
  pinMode(sensor, INPUT);
  //ping…(wait for the pong)
  pinMode(sensor, OUTPUT);
  digitalWrite(sensor, LOW);
  delayMicroseconds(2);
  digitalWrite(sensor, HIGH);
  delayMicroseconds(5);
  digitalWrite(sensor, LOW);
  //listening for pong now
  pinMode(sensor, INPUT);
  duration = pulseIn(sensor, HIGH);//PpPpPOoOoOnnnnnnng(hopefully)
  Serial.println(duration / 29 / 2 ); // for debugging
  return duration / 29 / 2 ;//convert to a human number with a unit...
}











RoboDev @ InfiniteSystems

growler

What sensor are you using? A link to a datasheet would be best. Before going through your code, describe what you want to have happen and describe what you observe when it goes wrong, it is hard to help if people have to guess.

RoboDev

Thank you for responding so quickly
I am using a PING from Parallax.
http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf

       This is a navigation program, all it does is drive forward continuously until it detects an obstacle less than 12 inches away, at which point it takes three readings, a left, right, and forward, reading an compares them. The last statement is people detection.  int AquireDist() reads the sonar sensor.
The Ping has an activity indicator LED, and in my first revision of the code, that did not have the turning sensor; the light was on almost continuously, and now it blinks only every 1.5 seconds. I saw that the original delay() stopped the sensor... but when I used millis(); the problem was not resolved.
Again, thank you.
RoboDev @ InfiniteSystems

growler

Ok, so I looked at your code, then I agreed with your comment at the top of your loop function.

Several very significant problems, making this actually do nothing like what you think it should.

1. You have a bunch of if statements that do nothing,
Code: [Select]
if (test == condition);
You should put the code you want to execute as a result of the test in braces like you have elsewhere. You are also testing things that will always be false for the most part but baby steps I guess.

2. I thought you said you removed delay? I can see two delay(1500) calls, one of which is going to execute every loop which would correspond with your .66HZ sensor activations. The way you are trying to wait for servos to finish their movements you might as well be using delay anyway. The point of avoiding delay is you can do something else instead of waiting, if the something else you are doing is only checking if you should still be waiting, you might as well use delay.

3. I'm really basically unsure what you think is going on here based on the last if statement and associated comment in your loop. setup executes once on starting up, then loop runs forever. You don't need to do anything at all to get to the beginning, other than getting to the end of it. That happens here every 1800 ms it looks like, because you are using delay.

Go Up