I've written this simple program to contol a motor and an air valve. On my breadboard, I'm using LEDs to simulate the output. All seems to work fine most of the time.
Here is my code:
'================================================================
'===== CHIP SETUP
'================================================================
#chip 16F876A, 6
#config HS_OSC, WDT_OFF, LVP_OFF
#define StartUpDelay 10 ms
'================================================================
'===== DEFINE THE PORTS
'================================================================
#define Pin2 PORTA.0
#define Pin21 PORTB.0
#define Pin22 PORTB.1
#define StopDelay 100 ms
#define FlashDelay 1 s
#define Sensor Pin2
#define Gate Pin21 'LED 1
#define Motor Pin22 'LED 2
'================================================================
'===== SET DIRECTION OF THE PORTS
'================================================================
'Pin1 ===> MCLR
Dir Sensor IN
Dir Motor OUT
Dir Gate OUT
Startup:
Wait StartUpDelay
Set Motor OFF 'LED 1 ON
Set Gate ON
gatedown=0 'LED 2 OFF
'================================================================
'===== MAIN PROGRAM LOOP
'================================================================
MAIN:
if Sensor on then 'Sensor UnCovered
set Motor off 'MOTOR ON (LED 1 ON)
set Gate on 'Gate UP (LED 2 OFF)
Gatedown=0
end if
if Sensor off then 'Sensor covered
set Motor on 'Motor off (LED 1 OFF)
if Gatedown=0 then
LowerGate
end if
end if
Goto Main
Sub LowerGate
wait FlashDelay
set Gate off 'Lower Gate (LED 2 ON)
wait StopDelay
set Gate on 'Raise Gate (LED 2 OFF)
Gatedown=1
end sub
When the sensor is covered, Led 1 should be off and Led 2 should be on - that's exactly what happens.
However, when the sensor is uncovered, at random times Led 2 goes off as expected but flashes on and off again.
In my real world application this would be very undesireable.
Does anyone know why this is happening and how to stop it from happening.
Thanks,
terrynbama
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The light sensor isignal s probably a slow moving one, like an ldr or a pv cell. A bit of hysteresis would solve the problem in a hardware situation, like in using one of the PIC comparators.
Software with a debounce switch routine could also work. The routine waits a number of iterations before declaring the condition true. Here is how I debounce a switch with GCBasic. Lengthen, or add more wait cycles, as you see fit..
If button = 0 Then
ButtonCount = 0
Do While Button = 0
wait 1 10ms
ButtonCount += 1
Loop
end if
If ButtonCount > 3 then
Call ChangeColor
ButtonCount = 0
end if
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I've written this simple program to contol a motor and an air valve. On my breadboard, I'm using LEDs to simulate the output. All seems to work fine most of the time.
Here is my code:
'================================================================
'===== CHIP SETUP
'================================================================
#chip 16F876A, 6
#config HS_OSC, WDT_OFF, LVP_OFF
#define StartUpDelay 10 ms
'================================================================
'===== DEFINE THE PORTS
'================================================================
#define Pin2 PORTA.0
#define Pin21 PORTB.0
#define Pin22 PORTB.1
#define StopDelay 100 ms
#define FlashDelay 1 s
#define Sensor Pin2
#define Gate Pin21 'LED 1
#define Motor Pin22 'LED 2
'================================================================
'===== SET DIRECTION OF THE PORTS
'================================================================
'Pin1 ===> MCLR
Dir Sensor IN
Dir Motor OUT
Dir Gate OUT
Startup:
Wait StartUpDelay
Set Motor OFF 'LED 1 ON
Set Gate ON
gatedown=0 'LED 2 OFF
'================================================================
'===== MAIN PROGRAM LOOP
'================================================================
MAIN:
if Sensor on then 'Sensor UnCovered
set Motor off 'MOTOR ON (LED 1 ON)
set Gate on 'Gate UP (LED 2 OFF)
Gatedown=0
end if
if Sensor off then 'Sensor covered
set Motor on 'Motor off (LED 1 OFF)
if Gatedown=0 then
LowerGate
end if
end if
Goto Main
Sub LowerGate
wait FlashDelay
set Gate off 'Lower Gate (LED 2 ON)
wait StopDelay
set Gate on 'Raise Gate (LED 2 OFF)
Gatedown=1
end sub
When the sensor is covered, Led 1 should be off and Led 2 should be on - that's exactly what happens.
However, when the sensor is uncovered, at random times Led 2 goes off as expected but flashes on and off again.
In my real world application this would be very undesireable.
Does anyone know why this is happening and how to stop it from happening.
Thanks,
terrynbama
The light sensor isignal s probably a slow moving one, like an ldr or a pv cell. A bit of hysteresis would solve the problem in a hardware situation, like in using one of the PIC comparators.
Software with a debounce switch routine could also work. The routine waits a number of iterations before declaring the condition true. Here is how I debounce a switch with GCBasic. Lengthen, or add more wait cycles, as you see fit..
I'm out of town at the moment, but I will try the debounce method upon my return. Thanks for the quick response.
I added a bounce routine as you suggested. It works great. Now I can move on to the larger picture, making a working project.
Thanks for the help!!!