Pages: [1]   Go Down
Author Topic: line follower with p.i.r sensor it beeps when some one tries to hold the robot  (Read 613 times)
0 Members and 1 Guest are viewing this topic.
isabela, philippines.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 62
if you dont go, you will never know.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


ive been configuring this for a month.
until now i am not satisfy with the result.
the analog pir in the line follower with buzzer.
it will follow the line and when a person got close to the robot in 1 second, it will stop and beeps continuously, though it will resume its movements when their is no person near it.

my problem is when it triggers the alarm  the robot will derailed on its track due to the "delay" used in code..

this is my code.

Code:

/*

I/O Usage

2- colision1 as input
3- colision2 as input
4- colision3 as input

-line sensor inputs
5- linesense1 as input low on black
6- linesense2 as input
7- linesense3 as input

-motor control output
8 - mot2 dir as output high=fwd
9 - mot2 run as output
11 - mot1 dir as output high= fwd
10 - mot1 run as output
 
 */


int  ls1 = 5;
int  ls2 = 6;
int  ls3 = 7;
int  m2dir = 8;
int m2run = 9;
int m1dir = 11;
int m1run = 10;
long int rtc;
int x = 0;
int val = 0;
int inputPin = A0;
int pinSpeaker = 4;   

// Initialization routine
void setup()   {
pinMode(pinSpeaker, OUTPUT);   
  pinMode(m2dir, OUTPUT);
  pinMode(m2run, OUTPUT);
  pinMode(m1dir, OUTPUT);
  pinMode(m1run, OUTPUT);
  pinMode(13, OUTPUT);
  rtc=millis()+10;
}


int  linesense=0;
int giveup=0;
int lastsense;
int runspeed=90;

//timers
byte  retry_dly=0;
byte  ledflsh=25;

// Main Line following program loop



void loop()                     
{
      //[b]  PROBLEM STARTS HERE.... [/b]
  val = analogRead(inputPin);   // val is where the output of analog pir when some one got near.
  if (val < 1) {                                 
  x++;
 delay(250);                          // if the sensor triggers above 1 seconds it will buzz. THIS IS MY MAIN PROBLEM DUE TO THE DELAY
                                               THE LINE FOLLOWER WILL DERAILED IN ITS PATH BECAUSE OTHER INSTRUCTIONS ARE WAITING IN
                                                1 SECONDS BEFORE OTHER INSTRUCTION ARE MADE.
if (x==3){

Stop();                         // the robot stops when the sensor is high under 1 second....
 
     for(int a = 2; a<15; a++){                       // beeps 15 times before continuing its path as long as the sensor reports no movement
  digitalWrite(pinSpeaker,HIGH);
    delay(200);
    digitalWrite(pinSpeaker,LOW);
    delay(50);}
     x=0;
}}
 
 
 
 
  // Hardware Timer service
  if(millis()>=rtc)
  {
      rtc=rtc+10;
      if(retry_dly>0) retry_dly--;
      if(ledflsh>0)
      {
        ledflsh--;
        if(ledflsh==0)
        {
          ledflsh=25;
          PORTB ^= 0x20;
        }
      }       
         
  }     
  linesense=0;

  if(digitalRead(ls1)==LOW) linesense=1;
  if(digitalRead(ls2)==LOW) linesense=linesense+2;
  if(digitalRead(ls3)==LOW) linesense=linesense+4;
 
 
  // if no line is detected (all high)
  if((linesense==0) & (retry_dly==0))
    {
     if(giveup<10)
     {
          //reverse for 20mS
        if(lastsense==1) runBot(runspeed*15/10,runspeed,LOW);
        if(lastsense==3) runBot(runspeed*12/10,runspeed,LOW);
        if(lastsense==4) runBot(runspeed,runspeed*15/10,LOW);
        if(lastsense==6) runBot(runspeed,runspeed*12/10,LOW);
       
        delay(40);
        giveup++;
      }
      else
      Stop();
      //delay(1000);
    }
 
 // if line is detected
  if(linesense!=0)
     {
        lastsense=linesense;
        giveup=0;
        retry_dly=50;  // retry delay=500ms
     }       
   
// extreme left

  if(linesense==1)
  {
        //Stop();
        dirfwd();
        analogWrite(m2run, runspeed);
        analogWrite(m1run, 0);
//        digitalWrite(m1dir,LOW);
        delay(50);
       
  }
 
 // centered
 
  if((linesense==2)|(linesense==7))
  {
      runBot(runspeed,runspeed,HIGH);
      delay(50);
  }     
 
 
 // skewed left
 
  if(linesense==3)
  {
        Stop();
        dirfwd();
        analogWrite(m2run, runspeed*12/10);
        delay(50);
       
  }
 
 // extreme right
  if(linesense==4)
  {
        //Stop();
        dirfwd();
        analogWrite(m1run, runspeed);
        analogWrite(m2run, 0);
//        digitalWrite(m2dir,LOW);
        delay(50);
       
  }

// skewed right
  if(linesense==6)
  {
        Stop();
        dirfwd();
        analogWrite(m1run, runspeed*12/10);
        delay(50);
       
  }
 
 

 
}


void runBot(int spd1,int spd2, boolean direction )
{
    digitalWrite(m2dir,direction);
    digitalWrite(m1dir,direction);
    analogWrite(m1run, spd1);
    analogWrite(m2run, spd2);
}

void dirfwd(void)
{
    digitalWrite(m2dir,HIGH);
    digitalWrite(m1dir,HIGH);
}   

void rotBot(int speed, boolean direction)
{
    digitalWrite(m2dir,direction);
    digitalWrite(m1dir,~direction);
    analogWrite(m2run, speed);
    analogWrite(m1run, speed);
}

void Stop(void)
{
    analogWrite(m2run, 0);
    analogWrite(m1run, 0);
}



THERE IS NO PROBLEM WITH THE LINE FOLLOWER CODES, ONLY IN THE DELAY LINE MAKES THE PROBLEM UNSOLVED.

I HOPE SOME ONE IN THIS WORLD WILL HELP ME SOLVE THIS PROBLEM. IVE BEEN CONFIGURING THIS FOR THE PAST MONTHS.
Logged

jaylisto

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 114
Posts: 4263
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Take a look at http://arduino.cc/en/Tutorial/BlinkWithoutDelay to see how to do 2 things at once
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26345
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If "delay" is causing you problems, get rid of the calls to "delay".
Have a look at the "blink without delay" example for some clues.

Please do not post in all capitals, it is the equivalent of SHOUTING, and considered rude.
« Last Edit: January 21, 2013, 05:19:14 am by AWOL » Logged

"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.

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Instead of this:
Code:
if(millis()>=rtc)
{
rtc=rtc+10;

it would be better to put this:

Code:
if(millis() - rtc >10)
{
rtc += 10;
since the second version copes with timer rollover correctly.

This is the piece of code which I think you're referring to as causing the problem:

Code:
val = analogRead(inputPin);   // val is where the output of analog pir when some one got near.
if (val < 1) {                                 
x++;
delay(250);                          // if the sensor triggers above 1 seconds it will buzz. THIS IS MY MAIN PROBLEM DUE TO THE DELAY
THE LINE FOLLOWER WILL DERAILED IN ITS PATH BECAUSE OTHER INSTRUCTIONS ARE WAITING IN
1 SECONDS BEFORE OTHER INSTRUCTION ARE MADE.
if (x==3){

Stop();                         // the robot stops when the sensor is high under 1 second....

for(int a = 2; a<15; a++){                       // beeps 15 times before continuing its path as long as the sensor reports no movement
digitalWrite(pinSpeaker,HIGH);
delay(200);
digitalWrite(pinSpeaker,LOW);
delay(50);}
x=0;
}}

I assume that the delay(250) is there so that you can use the sample count to know how long the obstruction has been there. A better way to do this without the delay would be to have a state variable that indicates whether there is currently an obstruction, and an unsigned long to record when the obstruction was first noticed. Each time through loop you test whether the obstruction is [ still ] there and if so how long it has been there for, and if that exceeds your interval (one second, or whatever) then carry out the Stop() and so on.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: