Hi,
I am working on Line follower robot with two sensor connected with variable 20K making voltage divider. directly to ADC input ...it is working fine..
i want to add PID for gaining more speed and control...
so, this link tutorial for one sensors..
how to set offset and other const parameters?
In the above most link he is using one sensors....i.e. offset 45
but i am using 2 sensors with black line on white the voltage is 0..2 to 0.5 and for black it is more than 2.8V (ranges is 0 to 5V) so, how should i calculate offset or just reading on black line?
here is the code...
I am working on Line follower robot with two sensor connected with variable 20K making voltage divider. directly to ADC input ...it is working fine..
i want to add PID for gaining more speed and control...
so, this link tutorial for one sensors..
how to set offset and other const parameters?
In the above most link he is using one sensors....i.e. offset 45
but i am using 2 sensors with black line on white the voltage is 0..2 to 0.5 and for black it is more than 2.8V (ranges is 0 to 5V) so, how should i calculate offset or just reading on black line?
here is the code...
Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
int ReadADC(unsigned char ch);
void main(void)
{ ADCON0=0b01000001;//000 = channel 0, (RA0/AN0)
ADCON1=0b10000000;
TRISC=0X00;
unsigned int b;
int number;
int number1;
PR2 = 0b11111001 ;
T2CON = 0b00000100 ;
CCP1CON = 0b00111100;
CCP2CON = 0b00111100;
PORTC = 0 ;
CCPR1L = 145 ;//pwm1
CCPR2L = 145 ; //pwm 2
__delay_ms(100);
RC4=1;//forward a
RC3=0; RC0=0;//forward b
RC6=1;
__delay_ms(100);
RC4=0;
RC3=0;
RC0=0;
RC6=0;
while(1){
b = ReadADC(2);// compare value
number = ReadADC(0); //sensor 1
number1 = ReadADC(1);// sensor 2
if(number<=b&&number1<=b){
CCPR1L = 145 ;
CCPR2L = 145 ;
RC4=1;//forward a
RC3=0;
RC0=0;//forward b
RC6=1;
}
if(number>=b&&number1<=b){
CCPR1L = 155 ;
CCPR2L = 155 ;
RC4=0;//forward a
RC3=1;
RC0=0;//forward b
RC6=1;
}
if(number<=b&&number1>=b){
CCPR1L = 155 ;
CCPR2L = 155
RC4=1;//forward a
RC3=0;
RC0=1;//forward b
RC6=0;
}
if(number>b&&number1>b){
CCPR1L = 155 ;
CCPR2L = 155 ;
RC4=0;//forward a
RC3=0;
RC0=0;//forward b
RC6=0;
} } }
int ReadADC(unsigned char ch)
{ int ret = 0;
ADCON0 = 0b10000001 + (ch<<3); // set channel to read
__delay_ms(5);
GO_DONE = 1; // start conversion
while(GO_DONE); // wait for conversion
ret = (ADRESH & 0x3) << 8; // get
ret += ADRESL; // result
return ret;
}