Simple code has me lost

regpye

New Member
What I am trying to do must be very simple, but it has me beat at present.
I have a small RF controller that simply puts out a signal and switches a relay on and off.
When the button is pressed on the transmitter the relay is on, but when I release the button the relay is off.
I want to have the relay stay on until I press again, so I thought I could use a picaxe to do that.
I have a small 08M2+ board already made that I could use that has a mosfet output on C.0 and an input on pinC.3
I envisaged using the toggle command to switch the mosfet on and off by means of the transmitter sending a signal to it's own relay which would act as a switch on pinC.3.

The code I started to prepare that has got me beat is shown below, I am at a loss to work it out.
The object is to switch on a mains powered light that has a very hard to reach switch, so I wanted to use a remote to be able to switch it on and off.

Code:
#Picaxe 08M2
#No_Data

;C.0 SSR 
;pinC.3 toggle pin
symbol SSR = C.0

main:
low C.0
If pinC.3 = 1 then change
goto main
change:
toggle SSR 

goto main
 

Aries

New Member
Are you sure you want to reset C.0 (=SSR) to LOW every time you go through the loop from main? It means you will be switching the relay every few microseconds.
 

hippy

Technical Support
Staff member
I would have gone for something like ...
Code:
Do
  ; Wait for high input
  Do : Loop Until pinC.3 = 1
  Toggle SSR
  Pause 10
  ; Wait for low input
  Do : Loop Until pinC.3 = 0
  Pause 10
Loop
 

regpye

New Member
Yes but nothing else can happen in the program...
I only need this one function for this project, I know it is overkill, but is my cheapest way out at present.
Actually if the code was put into a start0: and other codes could be put into other starts and all run at the same time I think.
I added a bit of extra code just for testing for two starts and it works OK so the do loop code doesn't lock other code out.
Code:
#Picaxe 08M2
#No_Data

;C.0 SSR 
;pinC.3 toggle pin
symbol SSR = C.0

start0:
do
If pinC.3 = 1 then
do loop until pinC.3 = 0
toggle SSR
endif
loop
goto start0

start1:
If pinC.4 = 1 then
high c.2
endif
if pinC.4 = 0 then
    low c.2
    endif
    goto start1
 
Last edited:

regpye

New Member
Are you sure you want to reset C.0 (=SSR) to LOW every time you go through the loop from main? It means you will be switching the relay every few microseconds.
I wasn't sure of anything at time of coding, it was just a start to try and work it all out. I knew that there would be a problem, but very fortunately there is help available for learners like me from people like you, so thanks for your input.:)
 

regpye

New Member
no need.
Program is waiting for C.3. That's why i only wrote "nothing else can happen in the program ". :)
I tried it with two starts and it works fine, and can have several starts if needed, so it is not locked into just that code at all.
See the simple example I previously gave
 

PieM

Senior Member
I know the use parallel task processing (only on M2) ... thanks.
This is an other program, not the easiest. But if you prefer.
I was talking about #5 program, not an other...
 

hippy

Technical Support
Staff member
I would have gone for something like ...
Yes but nothing else can happen in the program...
Do : Loop Until pinC.3 = 1 is a trap.
The code won't move on until the conditions are met but I wouldn't call it a trap as I didn't see any indication that there was any requirement to be doing anything else while waiting.

But it's easy enough to add things within the 'DO-LOOP' if one wants to do other things and it won't prevent other tasks running when multi-tasking as demonstrated.

If it were something which had to layered on top of existing code I would probably go for an interrupt, set that to interrupt on going high or low so interrupt only occurs when the signal changes, and only toggles the output when the signal has gone high. Pseudo code -
Code:
Interrupt:
  If interrupt was set for going high Then
    Toggle SSR
    set interrupt for going low
  Else
    set interrupt for going high
  End If
  Return
 
Top