Author Topic: line follower programming problem  (Read 2234 times)

0 Members and 1 Guest are viewing this topic.

Offline r0b0t1cTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
line follower programming problem
« on: February 13, 2013, 03:11:19 AM »
Hi. I'm doing a line following robot.
my problem is, in the simulation (pic as below), the code seems like does not work at all.

Quote
#include <htc.h>

#if defined(WDTE_OFF)
__CONFIG(WDTE_OFF & LVP_OFF);
#elif defined (WDTDIS)
__CONFIG(WDTDIS & LVPDIS);
#endif


int main(void) {
TRISB = 0X00; //PortB as output
TRISD = 0XFF; //PortD as input

//0
if (PORTD == 0X00)
{ PORTB = 0X00;
}

//1 Slow Turn Right
if (PORTD == 0X01)
{ PORTB = 0X01; //right motor reverse
PORTB = 0X04; //left motor foward
}

//2 Slow Turn Right
if (PORTD == 0X02)
{ PORTB = 0X01; //right motor reverse
PORTB = 0X04; //left motor foward
}

//3 Slow Turn Right
if (PORTD == 0X03)
{ PORTB = 0X01; //right motor reverse
PORTB = 0X04; //left motor foward
}
//4 Slow Turn Left
if (PORTD == 0X04)
{ PORTB = 0X02; //right motor foward
PORTB = 0X03; //left motor reverse
}

//5 Fast Turn Right
if (PORTD == 0X05)
{ PORTB = 0X01; //right motor reverse
PORTB = 0X04; //left motor foward
}

//6 Straight
if (PORTD == 0X06)
{ PORTB = 0X01; //right motor foward
PORTB = 0X03; //left motor foward
}
return 0;
}

based on my code, when port RD0 or RDO+RD1 or RD0+RD1+RD2, the motor in out1, will run backward and the motor in out3 will run foward, thus the body will rotate to the left.

but in my case, it does not work. can anyone help me to solve this problem?
thx

Offline newInRobotics

  • Supreme Robot
  • *****
  • Posts: 1,015
  • Helpful? 48
  • N.I.R.
Re: line follower programming problem
« Reply #1 on: February 13, 2013, 03:20:47 AM »
You forgot Your while(1) loop. Now Your program runs only once and then returns :)
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,345
  • Helpful? 82
Re: line follower programming problem
« Reply #2 on: February 13, 2013, 11:50:56 AM »
The other problem is that you're overwriting your commands.

Code: [Select]
PORTB = 0x1;
PORTB = 0x4;

This will end up with PORTB only containing the value 0x4, and thus the 0x1 direction won't be moving. You probably want something like:

Code: [Select]
PORTB = 0x1 | 0x4; // fuse the bits together

Offline r0b0t1cTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Re: line follower programming problem
« Reply #3 on: February 13, 2013, 05:47:57 PM »
 :o thanks guys..
its working now
by added the while(1) and PORTB = 0x1 | 0x4;  :)