Momentary switch / latching -Beginner help

Jarubell

Senior Member
I have a simple routine that checks a temp then turns on/off a relay but I'm trying to have a momentary switch that latches a different pin high or low. I was thinking that an interrupt would work but it seems to not work on the PICAXE. I'm struggling with that interrupt routine, any suggestions?


symbol R1=B.7
symbol R2=B.6
b1=0


setint %00000100,%00000100


Main:
if b1=0 then
low R2
endif

if b1=1 then
high R2
endif

readtemp C.1,b0

if b0>25 then
high R1
endif

if b0<=25 then
low R1
endif

goto main


Interrupt:
b1=b1+1
if b1=2 then
b1=0
endif

Pause 2000
setint %00000100,%00000100
return
 

Goeytex

Senior Member
Do you have a 10K pulldown reisitor on Pin C.2?

When using a relay to control something based upon temperature it is a good idea to add some hysteresis. This is especially important where the temperature changes fast and the relay is switching high currents . The hysteresis prevents the relay from switching on/off rapidly when the temp is at the set point.

Below is an example of one way it can be done ( including the latching output)

Code:
[COLOR="#008000"]'***********************************[/COLOR]
[COLOR="#008000"]'Simple Temperature Control Program[/COLOR]
[COLOR="#008000"]'***********************************[/COLOR]

#Picaxe 20M2 
#no_data

symbol temperature = b0
symbol IO_State = b1

symbol R1 = B.7
symbol R2 = B.6  
symbol sw_1 = PinC.2  [COLOR="#008000"] 'Momentary Switch [/COLOR]
;------------------------------------------------------------

INIT:
  
     IO_State = 0        [COLOR="#008000"]'Set initial state [/COLOR]
     low R2             
     setint %00000100,%00000100   [COLOR="#008000"]'Interrupt on Pin C.2 High [/COLOR]
                                 [COLOR="#008000"] '10K pulldown Resistor on C.2   [/COLOR]
;-------------------------------------------------------------  

MAIN:

      readtemp C.1,temperature
   
      select case temperature   [COLOR="#008000"]'Hysteresis added to prevent relay chatter[/COLOR]
                               [COLOR="#008000"] 'Extends relay life  May need more hyeteresis[/COLOR]
         case >= 26            [COLOR="#008000"] 'depending upon temp Delta[/COLOR]
             high R1            [COLOR="#008000"]'If relay is clicking on/off more than once every[/COLOR]
                                [COLOR="#008000"]'10 seconds  or so then suggest more hysteresis or[/COLOR]
         case <= 24            [COLOR="#008000"] 'solid state switch       [/COLOR]
             low  R1
        
      end select

goto main
;-----------------------------------------------------------------


Interrupt:
      pause 30                  [COLOR="#008000"]'Debounce switch[/COLOR]
      do while sw_1 = 1         [COLOR="#008000"]'Wait till switch is released[/COLOR]
      loop

      if IO_State <> 1 then
           high  R2  
           IO_State = 1
      else low R2  
           IO_State = 0
      endif 

pause 2000
setint %00000100,%00000100
return
 

oracacle

Senior Member
i circuit diagram would be helpful and a slightly more descriptive descrition of what you are trying to do will help too
 

techElder

Well-known member
Jarubell, the SYMBOL feature of the programming editor (PE) is there to make your programs more readable. In your program, writing "R1" instead of "B.7" doesn't exactly improve the readability. (Well, it probably does to you now, since you created the association! <GRIN>)

The point is to create your program with verbose and descriptive SYMBOL declarations for variables (and REMARK major lines of code.) You won't regret it when you have to visit this program again in 3 years.

Goeytex's suggested program is a good example: "symbol temperature = b0" and "symbol IO_State = b1" and " ' Interrupt on Pin C.2 High" and " ' 10K pulldown Resistor on C.2".
 

inglewoodpete

Senior Member
Jarubell, In addition to the above points, Goeytex's piece of code overcomes one of the problems that your code has with its interrupt routine. While Goeytex's code overcomes the problem in your code, he doesn't document the problem or the solution.

With your code, regardless of how briefly you press the button, the interrupt will execute multiple times. Your code toggles b1 every time the interrupt is triggered. If the routine is triggered an even number of times, the output (b1) will not appear to have changed: not what you want. Goeytex's code prevents the execution from progressing through the interrupt routine until the momentary switch is released.
 

rossko57

Senior Member
The point is to create your program with verbose and descriptive SYMBOL declarations for variables (and REMARK major lines of code.) You won't regret it when you have to visit this program again in 3 years.
Seconded. Perhaps not obvious is that using long symbol names uses no more precious memory.
 

jims

Senior Member
Goeytex, your example code does something that I have been wondering about for sometime. The "main" loop is quite tight and I expect that it would get back and turn ON
R1 again before the situation ( >26) has changed. If R1 is On and it's told to turn On again; does it have any deleterious effect on the Picaxe chip? I've had this situation on several projects and not seen a problem. Could there be long term damage??
Thank you Jims
 

lbenson

Senior Member
There is no problem with turning a pin on which is already on, or off if it is already off. If you wanted to avoid that, you could add a flag (bit variable) and only perform the action (or even the "if" statement) only if a change in the pin status could result.
 

Jarubell

Senior Member
Thank you all for your help and Goeytex for the sample code. I am using a 18M2 and yes I do have the pull down resistor on the switch. Goeytex, when you mentioned hysteresis and I read you code, that makes perfect sense. The one part of the code I need to go read up is where "Select Case" is used, I have not used it or seen it before, however it does make sense.

Thanks again
 
Top