Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

code help on solar tracking

Status
Not open for further replies.

deva

Junior Member level 1
Joined
Aug 20, 2010
Messages
18
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Location
kathmandu
Activity points
1,389
Please help in correcting code . its solar tracking on c . I have downloaded from net. i have corrected delay ....but did not work... Actually I am not good on C and did not make good algorithm....I have full hope on U on code correction and reply soon.... praveshkharel@yahoo.com


#include<at89x52.h>

void initialization();
void delay_ms(unsigned int);
unsigned int k;
void main()

//inputs
//P0_0=east sensor
//P0_1=west sensor
//P0_2=sunrise

//P1_0=cw
//P1_1=ccw

//P1_2=east led
//P1_3=west led
//P1_4=sunrise led

{
initialization();
delay_ms(400);

while(1)
{

if(P0_0==1&&P0_1==1&&P0_2==1)//east-west-sunrise(facing east)
{
P1_0=0;///cw
P1_1=0;//ccw

P1_2=1;//east led
P1_3=1;//west led
P1_4=1;//sunrise led
}

else if(P0_0==1&&P0_1==1&&P0_2==0)//east-west(position ok)
{
P1_0=0;///cw
P1_1=0;//ccw

P1_2=1;//east led
P1_3=1;//west led
P1_4=0;//sunrise led
}

else if(P0_0==0&&P0_1==0&&P0_2==0)//(no sun light)
{
P1_0=0;///cw
P1_1=0;//ccw

P1_2=0;//east led
P1_3=0;//west led
P1_4=0;//sunrise led
}

else if(P0_0==1&&P0_1==0&&P0_2==0)//east(move-east cw)
{
P1_0=1;///cw
P1_1=0;//ccw

P1_2=1;//east led
P1_3=0;//west led
P1_4=0;//sunrise led
delay_ms(20);
P1_0=0;///cw
P1_1=0;//ccw
delay_ms(20);
}

else if(P0_0==0&&P0_1==1&&P0_2==0)//west(move-west ccw)
{
P1_0=0;///cw
P1_1=1;//ccw

P1_2=0;//east led
P1_3=1;//west led
P1_4=0;//sunrise led
delay_ms(20);
P1_0=0;///cw
P1_1=0;//ccw
delay_ms(20);
}

else if(P0_0==0&&P0_1==0&&P0_2==1)//(sun rise)
{
P1_0=1;///cw
P1_1=0;//ccw

P1_2=0;//east led
P1_3=0;//west led
P1_4=1;//sunrise led
}

}
}

void initialization()
{
P0 = 0x00;
P1 = 0;
}


//generates delay in milli seconds
void delay_ms(unsigned int itime)
{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
}
 

Hello!

Why don't you try to debug yourself?
The problem with this kind of project is that we cannot check what is done.
I would advice to do as follows:

1. Set 2 push buttons to your processor, 1 for moving west, one for moving east.
2. Catch button interrupts, and act on motors. Verify that the system moves
in the right direction with each button.
3. At that point, the action is right. Verify the sensors. Put the system in a room,
use a lamp, act on the leds. Verify that the right led is lit with the right sensor.
4. Now the actuators (motors) and the sensors work. Try to move step by step
in your code, and see what happens.

Now some remarks about your code: it's very difficult to understand and it
looks very "heavy". There are many settings that you could do together
in a single line. You may understand it now, but if you try to open your
source files in 3 months from now, you will not understand anything.

You are checking the combinations of the states of P0[0~2].

Why not defining the events like this


Code C - [expand]
1
2
3
4
5
6
#define NO_SUNLIGHT     0x00 // 0, 0, 0
#define SUNRISE         0x01 // 0, 0, 1
#define SUN_WEST        0x02 // 0, 1, 0
#define SUN_EAST        0x04 // 1, 0, 0
#define EAST_WEST       0x06 // 1, 1, 0
#define E_W_SUNRISE     0x07 // 1, 1, 1



And the actions like this


Code C - [expand]
1
2
3
4
5
6
7
#define ACTION_NO_SUNLIGHT  0
#define ACTION_SUNRISE      0x11
#define ACTION_SUN_WEST     0x0A
#define ACTION_SUN_EAST     0x05
#define ACTION_EAST_WEST    0x0C
#define ACTION_EW_SUNRISE   0x1C
#define SHUTDOWN_MOTORS     0xFC



Then your code would become

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void main() {
    initialize();
    delay(400);
    [...]
    uint8 instate;
    while(1) {
        //  Keep the 3 last bits of port 0 to get status
        instate = P0 & 0x0F;
        switch(instate) {
        case NO_SUNLIGHT:
            P1 = ACTION_NO_SUNLIGHT;
            break;
        case SUNRISE
            P1 = ACTION_SUNRISE;
            break;
        case SUN_WEST
            P1 = ACTION_SUN_WEST;
            delay(20);
            P1 &= SHUTDOWN_MOTORS;
            delay(20);
            break;
        case SUN_EAST
            P1 = ACTION_SUN_EAST;
            delay(20);
            P1 &= SHUTDOWN_MOTORS;
            delay(20);
            break;
        case EAST_WEST
            P1 = ACTION_EAST_WEST;
            break;
        case E_W_SUNRISE
            P1 = ACTION_EW_SUNRISE;
            break;
        default:
            break;
        }
    }
}



NOTE: I'm not familiar with Atmel processors, so I am not sure about how
to set a port value, so I have just written P1 = xxx for setting P1 to the
xxx value. Please change accordingly.
As you can see, the core of your program fits within 1/3 or so of its
original size.
A few more hints:
- if you want to keep code simple, try to write it in a single page.
If a function takes more than one screen, then split it to subfunctions.
- Never leave a blank line in your code, it takes space for nothing
and your code may not fit in the screen. If you need to separate blocks
of code, then write a comment instead of a blank line. If you don't have
any comment to write, it means that you don't need to separate blocks.

Dora.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top