Projects › Projects › driving stepper motor with l293d › we used l298 instead of the
August 17, 2013 at 5:42 pm
#10331
Participant
we used l298 instead of the l293d and so the over heating problem is now solved and the motor is running fine but it often gives problem still. its not running as according to our project specifications. we are trying to make a solar tracker, and we are using three LDRs as teh sensors. as soon as any one of the sensor gets light the solar panel should be directed to that direction. here’s my code! it would be very helpful if my mistakes are pointed out! thank you#include<reg51.h>
#include<stdio.h> //port2 for load(stepper motor)::port1 for ldrs(sensors)el
#define high 1;
#define low 0;
sbit ldr1=P1^0; //initializing the bit of port 3
sbit ldr2=P1^1;
sbit ldr3=P1^2;
int position; //global variable to detect the position of the stepper motor
void T0delay();//time delay while tracking
void T0delay1(void);//time delay while moving fast to the required position
void clockd();//for tracking the position of the light
void clock(); //for clockwise from position1 to 2 or 2 to 3only while detecting the position
void clock1(); //for clockwise from position 1to 3
void anticlock1();//from position 2 to 1
void anticlock3();//for position 3 to 2
int check(); //to check the position of the stepper to return the position of stepper
void main(void)
{
unsigned int stepper_motor;
P3=0xFF; //initialize as input
position=0x00; //position=0 in first
P2=0x00;
while(1)
{
if(ldr1==1 & ldr2==0 & ldr3==0) //when ldr1 gets light
{
stepper_motor=check(); //to check the position of stepper motor in which step it is in and the value is returned from the function
if(stepper_motor==1) ///if position 1 then move complete rotation
{
T0delay();//just time delay
}
else if(stepper_motor==2)
{
anticlock1(); //move to the position 1
T0delay1();
}
else
{
anticlock1(); //if position 3 then moce anticlock and move complete rotation
T0delay1();
}
while(ldr1==1)
{//while ldr1 gets light only whole rotation should continue
clockd(); //start tracking one step function called and this is continued until ldr1 gets light
position++;//each movement position is increases
if(position==24) //if the complete rotation complete den only move anticlockwise rotation
{
anticlock1();//return back to original position
}
}
}
else if(ldr1==0&ldr2==1&ldr3==0)
{
stepper_motor=check();
if(stepper_motor==1)
{
clock(); //move fast to the position 2 then start tracking
T0delay1();
}
else if(stepper_motor==2)
{
T0delay1();
}
else
{
anticlock3(); //to go back to the 2nd position
T0delay1();
}
while(ldr2==1)
{//continue to rotate when ldr2=1
clockd();
position++;
if(position==24)
{
anticlock1();
}
}
}
else if(ldr1==0&ldr2==0&ldr3==1) //when light falls on 3rd ldr
{
stepper_motor=check();
if(stepper_motor==1)
{
clock1(); //move fast to the position 3 then start tracking
T0delay1();
}
else if(stepper_motor==2)
{
clock(); //move fast to the pisition 3 then start tracking
T0delay1();
}
else
{
T0delay1();
}
while(ldr3==1)
{
clockd();
position++;
if(position==24)
{
anticlock1();
}
}
}
else if(ldr1==1&ldr2==1&ldr3==0)//both ldr receives
{
stepper_motor=check();
if((stepper_motor==1)&&(stepper_motor==2)) //while steppermotor is in position 1 ana 2
{
T0delay1();
}
else
{
anticlock1();//go back to position 1
T0delay1();
}
while(ldr1==1&ldr2==1)
{ //tracking continues until light is on the ldr 1 and 2
clockd();
position++;
if(position==24)
{
anticlock1();
}
}
}
else if(ldr1==1&ldr2==0&ldr3==1)
{
stepper_motor=check();
if((stepper_motor==1)&&(stepper_motor=
))

{
T0delay1();
}
if(stepper_motor==2)
{
anticlock1();
T0delay1();
}
while(ldr1==1&ldr3==1)
{
clockd();
position++;
if(position==24)
{
anticlock1();
}
} }
else if(ldr1==0&ldr2==1&ldr3==1)
{
stepper_motor=check();
if((stepper_motor==2)&&(stepper_motor=
))

{
T0delay1();
}
else if(stepper_motor==1)
{
clock();
T0delay1();
}
while(ldr2==1&ldr3==1)
{
clockd();
position++;
if(position==24)
{
anticlock1();
}
}
}
else if(ldr1==1&ldr2==1&ldr3==1)
{
clockd();
position++; //whenever it is start rotating
if(position==24)
{
anticlock1();
}
}
//for no light search for the light by rotating left and right
if(ldr1==0&ldr2==0&ldr3==0)
{
;//stationar position
}
}//while
}//main
int check()
{
int stepper_motor1;
if(0<=position&&position<=8 )//to determine the position of motor.from step 1 to 8 it is in 1 position and from step 9 to 16 it is in 2 position and fron step 17 to 24 it is in position3
{
stepper_motor1=1;
}
if(9<=position&&position<=16)
{
stepper_motor1=2;
}
if(17<=position&&position<=24)
{
stepper_motor1=3;
}
return stepper_motor1;
}
void clockd()//function for clockwise rotation from position
{
P2=0x01;
T0delay();
P2=0x04;
T0delay();
P2=0x02;
T0delay();
P2=0x08;
T0delay();
}
void anticlock1() //function for anticlockwise from 2 to 1
{
unsigned int count;
for( count=position;count>=0;count–)
{
position=count;
P2=0x08;
T0delay1();
P2=0x02;
T0delay1();
P2=0x04;
T0delay1();
P2=0x01;
T0delay1();
}
}
void anticlock3() //anticlock from 3 to 2
{
unsigned int count;
for( count=position;count>=8;count–)
{
position=count;
P2=0x08;
T0delay1();
P2=0x02;
T0delay1();
P2=0x04;
T0delay1();
P2=0x01;
T0delay1();
}
}
void clock() //function for half clockwise from position 1 to 2
{
unsigned int count;
for( count=position;count<=8;count++)
{
position=count;
P2=0x01;
T0delay1();
P2=0x04;
T0delay1();
P2=0x02;
T0delay1();
P2=0x08;
T0delay1();
}
}
void clock1() //function for little clockwise from position 1 to 3
{
unsigned int count;
for( count=position;count<=16;count++)
{
position=count;
P2=0x01;
T0delay1();
P2=0x04;
T0delay1();
P2=0x02;
T0delay1();
P2=0x08;
T0delay1();
}
}
void T0delay1(void) //delay by using timer
{
TMOD=0x01; // timer 0, mode 1
TL0=0xFD; // load TL0
TH0=0x4B; // load TH0 for 0.050 sec
TR0=1; // turn on Timer0
while (TF0==0); // wait for TF0 to roll over
TR0=0; // turn off timer
TF0=0; // clear TF0
}
void T0delay(void) //delay by using timer
{
int j;
for(j=0;j<=10;j++)
{
TMOD=0x01; // timer 0, mode 1
TL0=0xFD; // load TL0
TH0=0x4B; // load TH0 for 0.050 sec now for 2 sec delay
TR0=1; // turn on Timer0
while (TF0==0); // wait for TF0 to roll over
TR0=0; // turn off timer
TF0=0; // clear TF0
}
}