View previous topic :: View next topic |
Author |
Message |
ferrousduke
Joined: 31 Mar 2008 Posts: 29 Location: Mumbai, India
|
Liquid dispenser |
Posted: Sun Aug 23, 2009 7:29 am |
|
|
Hello,
I am working on a liquid dispenser that would dispense some amount of liquid (alcohol based) once it detects an object (like hand) placed underneath it. Logic of operation is quite simple. I am including a flow chart with it. Please help me fill gaps in my code. Problem comes with #int_ext. I am making an infinite loop with the code below.
Code: | #if defined(__PCH__)
#include <16F84A.h>
#include <input.c>
#include <math.h>
#use delay(clock = 4000000)
#endif
#define IR_detect PIN_B0;
#int_ext
{
//{
//here I want to set the flag, PIC wakes up and does an interrupt routine.
//}
check_again(); // it will check whether even after 500 mS the object is present or not
}
void actuate_solenoid (void);
void check_again(void);
void check_again()
{
int i;
for(i = 0; i < 100; i++)
{
delay_ms(5);
}
//{
//here I want to clear the flag so to further wait for the interrupt.
//}
enable_interrupts(INT_EXT); // Is this the way to again enable the same interrupt?
enable_interrupts(GLOBAL); // According to me, it will be in a loop till the hand is removed
ext_int_edge(L_TO_H); //
if(flag)
{
actuate_solenoid();
}
else
{
break;
}
}
void actuate_solenoid()
{
for(int i, i < time; i++)
{
OUTPUT_HIGH(PIN_B1); //solenoid activates
delay_ms(1);
}
OUTPUT_LOW(PIN_B1); //solenoid deactivates blocking liquid supply.
//here I should let PIC in sleep mode. and wait for the next interrupt to happen. Return to main.
}
void main()
{
//setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
enable_interrupts(INT_EXT); // IF hand is placed, it will be notified by L_TO_H
enable_interrupts(GLOBAL);
ext_int_edge(L_TO_H);
// PIC sleep mode activated
}
|
|
|
|
bela
Joined: 31 Aug 2008 Posts: 27 Location: Bedford, BEDS UK
|
|
Posted: Sun Aug 23, 2009 8:54 am |
|
|
You should not have the ISR taking so long. Also, there's no need to actually 'do anything' for a wake up interrupt.
Code: |
#INT_EXT
void ext_isr()
{
return;
}
boolean check_again()
{
delay_ms(500);
if(!input(IR_detect))return(true);
else return(false);
}
main()
{
ext_int_edge(L_TO_H);
enable_interrupts(GLOBAL);
while(TRUE) {
enable_interrupts(INT_EXT);
sleep();
disable_interrupts(INT_EXT);
/* now we have been woken by the interrupt */
if(!check_again())continue;
actuate_solenoid();
while(!input(IR_detect)); //wait for the hand to be removed
}
}
|
|
|
|
ferrousduke
Joined: 31 Mar 2008 Posts: 29 Location: Mumbai, India
|
|
|
ferrousduke
Joined: 31 Mar 2008 Posts: 29 Location: Mumbai, India
|
|
Posted: Mon Aug 24, 2009 1:26 am |
|
|
Can anyone please help with the code? |
|
|
bela
Joined: 31 Aug 2008 Posts: 27 Location: Bedford, BEDS UK
|
|
Posted: Mon Aug 24, 2009 2:25 am |
|
|
Hi,
First thing: int8 i cannot count up to 1000; 255 max. Normally, replace int8 with long but this time, I'd be tempted to just do this:
Code: |
void activate_solenoid()
{
output_high(PIN_B2);
delay_ms(1000);
output_low(PIN_B2);
}
|
also, your i does not have an initializer; you can't guarantee that it will be zero so; for(i=0;i<1000;i++)
Let me know what happens.
Darren. |
|
|
ferrousduke
Joined: 31 Mar 2008 Posts: 29 Location: Mumbai, India
|
|
Posted: Mon Aug 24, 2009 5:47 am |
|
|
Hey,
THanks for your reply. I fixed the code but ext_int_edge(L_TO_H) does not work.. I had to change it to H_TO_L. Quite strange. |
|
|
ferrousduke
Joined: 31 Mar 2008 Posts: 29 Location: Mumbai, India
|
what am I doing wrong? |
Posted: Tue Aug 25, 2009 5:38 am |
|
|
operation faulty_indication() should ring only when power is not connected to relay. for me it shows even if the power is ok.. it blinks twice if the power is not available to relay and blinks once after the operation is finished even when everything is alright.
Below is the code:
Code: | #INT_EXT
void ext_isr()
{
return;
}
boolean verify()
{
delay_ms(time);
if(!input(IR_detect) && input(PIN_B1)) //Making sure Hand is still present
{ //and Power is connected to relay.
return(true);
}
else
{
fault_indication();
return(false);
}
}
void fault_indication()
{
int8 i;
for(i = 0; i < 5; i++)
{
output_high(PIN_B4); //Glows LED showing malfunction in relay.
delay_ms(200);
output_low(PIN_B4);
delay_ms(200);
}
}
void activate_solenoid()
{
output_high(PIN_B2); //solenoid activates
delay_ms(activated_time);
output_low(PIN_B2); //solenoid deactivates blocking liquid supply.
}
void main()
{
ext_int_edge(H_TO_L);
enable_interrupts(GLOBAL);
while(TRUE)
{
enable_interrupts(INT_EXT);
sleep();
disable_interrupts(INT_EXT);
if(!verify())continue;
activate_solenoid(); //Solenoid is activated and deactivated.
while(!input(IR_detect)); //waiting for the hand to be removed.
}
} |
|
|
|
|