Need assistance in PIC using C programming!!!

Hello guys, i am newbie here, was doing a project regarding infrared sensor autogate...

what i wish to ask is how to write the command for the situation stated below?

When a IR receiver received a signal from IR transmitter for more than 3 sec, only the DC motor will move CW or CCW? else, the motor will be remained stationary, even there is something pass by between.... the concept is some sort like a level sensor, to sense the container volume, which the IR receiver was triggered when the container full....

I have try the delay command be4, but it does not work.... it just delay the time of the motor to move, even the IR receiver just get the signal.... so what i wish is to make sure that when the object is stick stationary for more than 3 sec only the motor will be triggered...

thanks very much! hope some1 to help out......URGENT PROJECT!!!


to let all more understand about the code, i hereby posted up the program to emphasize the prob i encounter now...



#define IN1 PIN_B0
#define IN2 PIN_B1
#define IN3 PIN_B2
#define IN4 PIN_B3

#define MOTOR_1_CW 1
#define MOTOR_1_CCW 2
#define MOTOR_1_STOP 3
#define MOTOR_2_CW 4
#define MOTOR_2_CCW 5
#define MOTOR_2_STOP 6

#define OFF 1 //IR receiver open circuited
#define ON 0 //IR receiver connected to ground

void motor_motion(unsigned int motion);

void main()
{

// setup_adc_ports(ALL_ANALOG);
// setup_adc(ADC_CLOCK_INTERNAL);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);

SET_TRIS_B(0x00);
SET_TRIS_A(0xFF);

motor_motion(MOTOR_1_STOP);
motor_motion(MOTOR_2_STOP);

while(1){
if(input_state(PIN_A0) == ON
|| input_state(PIN_A1) == ON
|| input_state(PIN_A2) == ON
|| input_state(PIN_A3) == ON
|| input_state(PIN_A5) == ON){

int i;
i = 1;
if(i < delay_ms(1000)){
motor_motion(MOTOR_1_STOP);
}
motor_motion(MOTOR_1_CCW);
}
else motor_motion(MOTOR_1_STOP);


>>> Here is the condition to make my situation proved, i nid some1 help here, my program ady written as, but it only works as stated above<<<
}

}

void motor_motion(unsigned int command){
if(command == MOTOR_1_CW){
output_high(IN1);
output_low(IN2);
}
else if(command == MOTOR_1_CCW){
output_high(IN2);
output_low(IN1);
}
else if(command == MOTOR_1_STOP){
output_low(IN1);
output_low(IN2);
}
}

Replies

  • bayazidahmed
    bayazidahmed
    I couldnt understand when do you want to turn it CW and when CCW?

    As far as my understanding goes your while part can be modified to give a simple solution as follows

    while(1)
    {
    if(input_state(PIN_A0) == ON
    || input_state(PIN_A1) == ON
    || input_state(PIN_A2) == ON
    || input_state(PIN_A3) == ON
    || input_state(PIN_A5) == ON)

    {
    delay_ms(3000);


    if(input_state(PIN_A0) == ON
    || input_state(PIN_A1) == ON
    || input_state(PIN_A2) == ON
    || input_state(PIN_A3) == ON
    || input_state(PIN_A5) == ON)

    {
    motor_motion(MOTOR_1_CW); //or CCW if you want
    }

    }
    }
  • Ashraf HZ
    Ashraf HZ
    Ahmed, doesnt your solution mean it'll check twice: the second time AFTER a delay of 3 seconds? The condition stated in the problem is that the IR is received for a continuous 3 seconds before the motor will move. If you just delay it, you wouldn't know whether there was an interruption during the delay.

    I think better we try adding a timer counter within a while loop, so when the timer reaches 3 seconds, we switch on the motor. Maybe we can take advantage of the TMR function of the PIC? When the conditions are met, run the timer, and check timer again continously. If there is an elapsed time of at least 3 seconds, then run the motor.

    Of course, timing is based on the clock frequency you are using 😛 Keigo, can you tell us more about the PIC you are using, and at which clock speed?
  • keigo911
    keigo911
    ya ash, it is actually exactly what u r saying.... this is the prob i encountered and the code is just able to delay, but not continue....

    btw i am using PIC16F877A with the DC clock speed of 20Mhz as usual...

    if u have the idea on how to do it, would u mind to edit the code given to me so i can test it on my Proteus ISIS 7 Pro simulator..... I am using CCS C Compiler for the code compilation

    thank you!
    hope to get your reply soon as it is an urgent project..... 😀

You are reading an archived discussion.

Related Posts

hello to everyone especially the mods and creators of this very interesting site. My name is Basti and i'm a telecom network planner... i'm very much honored to be a...
Hello everyone,below is the link for one of the best site for n/w profeesionals... You can also use the online Cisco Lab Firewall.cx - The World's First Free Cisco Lab!...
As reported by TechCrunch:- Shelfari, a social network for bibliophiles, has been acquired by Amazon for an undisclosed amount. Amazon has been a longtime supporter of the Seattle based startup,...
I'm addicted to Google Reader -> https://reader.google.com What about you?
I came across this interesting article by Guy Kawasaki ( whom we interviewed on CrazyEngineers couple of months back: Small Talk with Guy Kawasaki - How to change the world)...