Microcontroller › AVR › PROBLEM IN USING FLEX SENSOR › That delay i am introducing
October 6, 2014 at 4:53 pm
#12265
himanshu mittal
Participant
That delay i am introducing in rotating motors is causing problem for me.when my function is in delay loops obeviously it will not in read ADC value from all five sensors. I can’t understand how to program such that my program reads all ADC value continouly and my motors respond to quickly to ADC value without facing the problem of delay.
This is my program for two flex sensors ..
That delay is main rival..
#include <avr/io.h>
#include <avr/LCD2014.h>
delay(unsigned int d)
{
int i,j;
for (i=0;i<d;i++)
{
for (j=0;j<1000;j++)
{
}
}
}
int main(void)
{
int x=0,y=0; // x for green, y for red
lcd_init();
DDRB=0xff;
ADCSRA=0x87;
int a=0,b=0;
while (1)
{
ADMUX=0x20;
ADCSRA|=(1<<ADSC);
while((ADCSRA&(1<<ADIF))==0);
x=ADCH;
ADMUX=0x20;
ADCSRA|=(1<<ADSC);
while((ADCSRA&(1<<ADIF))==0);
y=ADCH;
if (x>=180 && x<=192 && a==0)
{
a=1;
CLEARBIT(PORTB,BIT0);
CLEARBIT(PORTB,BIT1);
}
if (x>=197 && x<=207 && a==1)
{
a=2;
SETBIT(PORTB,BIT0);
CLEARBIT(PORTB,BIT1);
delay(150);
CLEARBIT(PORTB,BIT0);
CLEARBIT(PORTB,BIT1);
}
if (x>=212 && a==2)
{
a=3;
SETBIT(PORTB,BIT0);
CLEARBIT(PORTB,BIT1);
delay(150);
CLEARBIT(PORTB,BIT0);
CLEARBIT(PORTB,BIT1);
}
if (x<=208 && x>=200 && a=)
{
a=4;
CLEARBIT(PORTB,BIT0);
SETBIT(PORTB,BIT1);
delay(150);
CLEARBIT(PORTB,BIT0);
CLEARBIT(PORTB,BIT1);
}
if (x<=196 && x>=188 && a==4)
{
a=0;
CLEARBIT(PORTB,BIT0);
SETBIT(PORTB,BIT1);
delay(150);
CLEARBIT(PORTB,BIT0);
CLEARBIT(PORTB,BIT1);
}
if (y>=190 && y<=202 && b==0)
{
b=1;
CLEARBIT(PORTB,BIT2);
CLEARBIT(PORTB,BIT3);
}
if (y>=207 && y<=217 && b==1)
{
b=2;
SETBIT(PORTB,BIT2);
CLEARBIT(PORTB,BIT3);
delay(1500);
CLEARBIT(PORTB,BIT2);
CLEARBIT(PORTB,BIT3);
}
if (y>=222 && b==2)
{
b=3;
SETBIT(PORTB,BIT2);
CLEARBIT(PORTB,BIT3);
delay(1500);
CLEARBIT(PORTB,BIT2);
CLEARBIT(PORTB,BIT3);
}
if (y<=218 && y>=210 && b=)
{
b=4;
CLEARBIT(PORTB,BIT2);
SETBIT(PORTB,BIT3);
delay(1500);
CLEARBIT(PORTB,BIT2);
CLEARBIT(PORTB,BIT3);
}
if (y<=206 && y>=198 && b==4)
{
b=0;
CLEARBIT(PORTB,BIT2);
SETBIT(PORTB,BIT3);
delay(1500);
CLEARBIT(PORTB,BIT2);
CLEARBIT(PORTB,BIT3);
}
lcd_value(x);
lcd_cmd(LINE2);
lcd_showvalue(y);
delay(10);
}
}