1 Attachment(s)
Advices required for Soap dispenser / sanitizer code
Hi ! Both of my dispensers stopped working. So I thought of making a module with PIC. However, I encounter problems when implementing the function @Sleep in the program. I need your advice .... Thansk !
Code:
@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
DEFINE OSCCAL_1K 1
DEFINE OSC 4
CMCON = 7
TRISIO = %00000001
INTCON = 0
IOC = 0
GPIO = 0
ANSEL = %00110001
GIE var intcon.7 ' global interrupt enable 1=on ; 0=off
GPIE var intcon.3 ' port change interrupt enable 1=0n ; 0=off
GPIF var intcon.0 ' port change interrupt flag bit
DataW var word
last_b_level var byte
b_level var byte
b_cnt var byte
b_act var byte
GIE=0
GPIE=1
IOC.0=1 ' int-on-change for GPIO.0 enabled
Clear
GPIO.2 = 0
ADCON0 = %10000001
Pauseus 100 ' Wait for channel to setup
;============================================================================
Main:
ADCON0.1 = 1 ' Start conversion
@ SLEEP
While ADCON0.1=1:Wend ' Wait for conversion
DataW.HighByte=ADRESH ' Read variable from ADC and save
DataW.LowByte=ADRESL
if DataW < 600 then
b_level=1
if b_level=last_b_level then
b_cnt=b_cnt+1
pauseus 50
if b_cnt > 10 then b_act = 1 'TO AVOID FALSE READING
endif
last_b_level=b_level
endif
if b_act = 1 then gosub comanda
goto main
;============================================================================
comanda:
GPIO.2 = 1
PAUSE 4000
GPIO.2 = 0
b_act=0
last_b_level=0
b_cnt=0
GPIF=0 'Clear or change interrupt flag
Return
END
It's the first test ... simulating IR detector by resistor and reading the ADC.
Attachment 8939
Re: Advices required for Soap dispenser / sanitizer code
I think you have it a bit mixed up!
First, a voltage divider like that will not trigger IOC reliable or at all.
Second, in case of an interrupt what should the program do? You do not have an ISR to jump to.
Third, read the manual regarding the On Interrupt command to make a ISR subroutine.
The main idea is to make a digital input with preferably internal pull-up enabled. Then, an external trigger on that input will start PIC and jump to ISR.
In the ISR, you can recheck the input to make sure the trigger was real and then set the output for 4 sec. Then clear Interrupt flag, reset the output and go to sleep again.
Ioannis
Re: Advices required for Soap dispenser / sanitizer code
i assume you intend the button to cause an ioc interrupt to occur when pressed.
it wont because any i/p pin set as analog will always read as low so a pin change event
will never occur
why would use adc to react to a button anyway ?
Re: Advices required for Soap dispenser / sanitizer code
I can only guess that it is not a clear 0-5 volt state.
And that is why he simulates the trigger pulse with the two resistors. All the way wrong approach...
Ioannis
Re: Advices required for Soap dispenser / sanitizer code
Thank you for the answers. The 2 resistors and the button simulate a photodiode, whose resistance increases when it is no longer illuminated by the IR emitting diode (communication is interrupted by the hand interposed on the route). There is NO button !
Re: Advices required for Soap dispenser / sanitizer code
At work, we make a product that detects hand motion to light up a string of LED's. You can use a FET to drive the soap pump motor. We use the ADC in a 12F675 to detect fluctuations from a 5528 LDR. ADC has 47K pull down resistor, other side of LDR to B+.
1 Attachment(s)
Re: Advices required for Soap dispenser / sanitizer code
IF you use the comparator its as simple as this.it won't re-arm itself until hand is removed
Attachment 8941
Code:
#config
__config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
#endconfig
DEFINE OSCCAL_1K 1
DEFINE OSC 4
CMCON = %00001110
TRISIO = %111011
vrcon=$88 ;about vcc on 2
ANSEL=1
tmp var byte
Main:
intcon=$40
pir1=0
PIE1=8
asm
SLEEP
nop
endasm
if CMCON.6 then
gpio.2=1
pause 1000
gpio.2=0
endif
goto main
END
Re: Advices required for Soap dispenser / sanitizer code
Thank you very much ! I think this is what I need ! I will tested and post the results. Regards !