0

I am using Atmega16A and L293D motor driver in my Line Follower. I am using code for following white lines only but now I want it to follow black line.

I first thought they are linked by color I tried changing the color codes, but no result achieved in changing the line color to follow. Here is the code:

#include <avr/io.h>
#include "util/delay.h"


#define s1 ((PINA)&(0x01))
#define s2 ((PINA)&(0x02))
#define ms ((PINA)&(0b00000100))
void forward()
{
    PORTD=0b00001001; 
}

void left()
{
    PORTD=0b00000001;
}

void right()
{
    PORTD=0b00001000;
}
void follow()
{
    if((ms!=0)&&(s1==0)&&(s2==0))
    {
        forward();
    } else if (s1!=0)
    {
        left();
    }
    else if (s2!=0)
    {
        right();
    }

//  
//  else if((ms==0)&&(s1==0)&&(s2==0))
//  {
//      right();
// //       _delay_ms(150);
//      
//  }
    else if((ms!=0)&&(s1!=0)&&(s2==0))
    {
        forward();
    }
    else if((ms!=0)&&(s1==0)&&(s2!=0))
    {
        forward();
    }
    else if((ms!=0)&&(s1!=0)&&(s2!=0))
    {
        forward();
    }
    else if (ms==0)
    {
        if (s1!=0)
        {
            while(ms==0)
            {
                left();
            }
        }
        else if (s2!=0)
        {
            while(ms==0)
            {
                right();
            }           
        }
    }

    else 
    {
        forward();
    }
}

int main(void)
{
    DDRA = 0b00000000;
    PORTA=0xFF;
    DDRD = 0b11111111;
    while(1)
    {
        follow();
    }

 }
inMILD
  • 168
  • 2
  • 12
  • What means `if((ms!=0)&&(s1==0)&&(s2==0))` ? The sensor reading? – jabujavi Jan 18 '17 at 07:50
  • Just a small hint, but you would already know it if this code was yours... you, probably, have three light sensors (ms, the central one, s1, which is the left one, and s2, which is the right one) which output 1 if there is white under them and 0 if there is black. Now it's following the black line (i.e. try to keep it in the middle). How can you reverse its behavior? One more thing: there are a lot of "if" cases which are not needed. – frarugi87 Jan 18 '17 at 08:14

1 Answers1

0

First of all, change the macro definitions like this :-

#define s1 ((PINA)&(0x01));
#define s2 (((PINA)>>1)&(0x01));
#define ms (((PINA)>>2)&(0x01));

Then use these macros in linefollow() comparisons. Now if s1,s2 or ms expands & outputs 0/1 then black/white surface under that sensor respectively. (That too depending upon the type of line sensor used.)

As you have said that your current code is for white line following on black surface then just change 0s to 1s and vice-versa for black line following on white surface.

for e.g.

if((ms!=1)&&(s1==1)&&(s2==1))
{
    forward();
} else if (s1!=1)
{
    left();
}
else if (s2!=1)
{
    right();
}

BTW, I would also like to suggest you to get rid of redundant if-else cases as your code seems to contain some. Happy Bot Making. :)

Banana
  • 2,435
  • 7
  • 34
  • 60