Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

line follower robot programminh

Status
Not open for further replies.

Parth86

Member
HI,
I have just started to write c code. please suggest me i am going to wrong way
algorithm
If sensor is High -both motor move forward
If sensor is Low- stop both motor

PORT A A0 A1 A2 A3 A4 A5 A6 A7 output of sensor is connected to A4 pin
0 0 0 0 1 0 0 0

PORT B B0 B1 B2 B3 B4 B5 B6 B7 motor1 and motor2 connected pin 2345
0 0 1 1 1 1 0 0

[#include<AVR/IO.h>
main()
while(1)
{
DDRA=OXOO; //port A as input
DDRB=OXFF; //port B as output
PORTA=0b00001000; // set as high pin
PORTA=0b00000000; //set as low pin
{
If (port A=00001000); //If sensor is high
{
(port B=0b00111100); // both motor move forward
}
If (port A=00000000); //If sensor is Low
{
(port B=00000000); //stop both motor
}
}
}
]
 
Last edited by a moderator:
If the first "If then" is true that's all it going to do, never see the second one.

I'm not a avr user but I'm sure this is more in line with what your trying to do

Code:
#include<AVR/IO.h>
void main() {

DDRA=OXOO; //PORTA as input
DDRB=OXFF; //PORTB as output
PORTB=0b00000000; //let's start with it off

  If (PORTA=0b00001000){ //If sensor is high

      (PORTB=0b00111100); // both motor move forward
  }
    ELSE If (PORTA=0b00000000){ //If sensor is Low

     (PORTB=0b00000000); //stop both motor
  } while(1);
}
 
Last edited:
Sure you could or a arduino which uses a AVR but it's easier coding lots of examples. But if you used Mikro C pro AVR to write your code it wouldn't be to hard to write code with it. Great help files with it.

If you used a pic I would go with a 18f14k22 there cheap and when you get done with this you can do some really neat stuff with that little chip.
MikroC pro for the pic is easy to get started with.

I'll post you the same code for the pic.
 
Last edited:
This is a little better code maybe give you a idea
Code:
#define sensor1 PORTA.F0
#define sensor2 PORTA.F1
#define sensor3 PORTA.F2
#define motor_R PORTC.F0
#define motor_L PORTC.F1
#define motor_forward PORTC
#define motor_stop PORTC
void main() {


motor_stop =0b00000000;
     do {
           if (sensor2 == 1){           //starts you rolling forward
              motor_forward = 0x3;
              }
           if (sensor1 == 1){             //stears to right
               do {
                    motor_L =~ motor_L;
                       Delay_us  (100);
               } while (sensor2 == 0);
              motor_forward = 0x3;        // if the center found go forward
              }
             if (sensor3 == 1){          // stears to left
               do {
                    motor_R =~ motor_R;
                       Delay_us  (100);
               } while (sensor2 == 0);
              motor_forward = 0x3;       // if the center found go forward
              }
     }while (1);    // keep doing it

}
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top