Code to control output On and Off pulse times with pots

Visser

Well-known member
Hi,

Firstly, you say the load is "a.c." and you are using a current transformer (but the same would probably apply to a Hall-effect sensor) which will produce a 50Hz alternating voltage signal. How are you converting that to a "low" input voltage level on each of the PICaxe pins?

The basic sequencing is quite simple, but you might need to set some lower "timeout" limits, to ensure that each load is driven for a sufficiently long time.

However, your "timer" input may require some degree of "multi-tasking" capability, i.e. two or more pins must be monitored "at the same time" (depending whether the shut-down must occur "immediately" when c.5 from the timer goes low). There are at least three ways to do that : 1) You can ensure that each part of the program has a "polling loop" which continuously checks the levels of the required input pins rapidly in sequence. Or 2) an Interrupt could be used, or 3) , as you're using an M2 PICaxe, you could use its multi-tasking capability (which uses two or more "Start" labels). There should be an explanation in the User Manuals, but the Command Reference is probably a place to start.

Cheers, Alan.
Here is a diagram of the current transformer setup I plan to use. I haven't tested it yet
I also wonder if I cannot parallel 4 of this circuits and use only one sense input that will be high while any relay is on and goes low when its load switch off. Then the output must go to the next pin till all 4 is done and start to loop
Another way I can try is to use some logic chip that triggers on a falling edge and invert signal so I get a high to the sense input. Will have to look into it
 

Attachments

papaof2

Senior Member
If an ebay link has a "?" in it, just use the part to the left of the "?" - all else is options (type of sort, as in lowest price first, etc)
 

mushroom

New Member
Papaof2. I wish someone had told me about the "?" a long time ago. Thanks.
Visser. I use these modules for testing for the presents of 42Vac to 240Vac. The outputs can go straight to picaxe inputs. I'd struggle to buy the parts for the price and they're pre-assembled.
 
Last edited:

BillBWann

Member
Sequencer using a Picaxe 18M2
Hi
I need code for a sort of a sequencer using a Picaxe 18M2
A timer will switch on for 6 hours each day and during that time I need to switch AC with 4 relays to 4 different loads. Each load will switch off by itself after a while. I will sense the current to the load with a current transformer circuit that goes high when current flows from a relay to a load and low when it stops. I need to use that lows to trigger the next load to switch on till all 4 loads was powered
Then if the timer is still on the process must repeat till the timer times out. The next day it must start from the beginning again

5V high from timer will start process. Timer will be on for 6 hours each day
C5 high
B7 goes high which will make B0 high (from a current transformer circuit that goes high when current flows from a relay to a load. The load will switch off by itself after a while and B0 will go low)
When B0 goes low, B7 must go low and B6 high, which will make B1 high
After a while when
When B1 goes low, B6 must go low and B5 must go high, which will make B2 high
When B2 goes low, B5 must go low and B4 must go high, which will make B3 high
When B3 goes low, B4 must go low and the process must repeat and start again with B7 go high
This should go on till C5 from the timer goes low and all outputs must be low
The next day when the timer comes on it has to start from the beginning again

I don't see any other way to trigger the events. I will pull all the inputs low with a 10k resistor and the event will have to happen when the input goes from high to low. I don't know if that can be done
I believe that the following program achieves what you asked for in #115. When pin c.5 goes high, it switches on pin b.7 for 2 seconds. If pin b.1 has gone high by the end of that period, it leaves pin b.7 high until pin b.1 goes low. It then sequences onto pin b.6 and again waits for 2 seconds for pin b.2 to go high. If an input pins remain low, the output cycles onto the next output pin after the 2 second stabilisation on period. This continues indefinitely until pin c.5 goes low. The cycle always starts at pin b.7 when c.5 goes high.
Of course, if 2 seconds isn’t the right delay while waiting for the current transformers to stabilize, simply change the pause statement. If you decide to use only a single current transformer in the common power supply main and you are otherwise happy with this program, I could very simply modify it to allow for that.
I won’t attempt to explain my code and would suggest that you copy and paste it into the simulator where hopefully it will become obvious how it works.

Code:
#picaxe 18m2
#no_data


let dirsB = %11110000    'pins b4-7 as outputs

pullup %00001111

do
    strt:
    b0=1
    b1=128
    if pinc.5=1 then
        for b2=0 to 3
            b3=b2
            b1=128
            do while b3<>0
                b3=b3-1
                b1=b1/2
            loop
            pinsB = b1
            pause 2000    'wait for CT to switch on
            do
                b0=pinsb and %00001111
                b3=b2
                do while b3<>0
                    b3=b3-1
                    b0=b0/2
                loop
            loop while bit0>0 and pinc.5=1
            pinsB=0    'switch off all relays (3 should already be off)
            if pinc.5=0 then strt    
        next b2
        goto strt:
    endif
loop
 

BillBWann

Member
Oops, I forgot to remove the pullup command in the code I just posted. It just happened to be a convenient way for me to test the program on a real 18m2. It probably wouldn't do any harm if it remained in but most likely isn't needed by your hardware.
 

Visser

Well-known member
Oops, I forgot to remove the pullup command in the code I just posted. It just happened to be a convenient way for me to test the program on a real 18m2. It probably wouldn't do any harm if it remained in but most likely isn't needed by your hardware.
Oh my soul! This simulates exactly like I need it. You have such a brilliant mind. I have no idea how you did that or how it works but it seems to do the job
Thank you so much BillBWan
I will go ahead and build the circuit when I got all the spares and report back with photos and maybe a video
 

Visser

Well-known member
Hi Mushroom
The problem is that there will always be AC present while any relay is on. The load that is far away will switch off by itself after a while but the ac will still be present till the relay switch off. That is why I have to sense the current to determined when the load switch off to switch the relay off and the next one on. The only way I can think of to do that is by using the current transformer
 

Visser

Well-known member
If you decide to use only a single current transformer in the common power supply main and you are otherwise happy with this program, I could very simply modify it to allow for that.

Hi
I want it as simple as possible. I want to try above as you described. Will it be possible to do the same program with only one sense input working the same . Pulled low by a resistor it will go high when a relay switch on and low when the load switch of and current stops to flow which then must switch the next relay?
I noticed that on On M2 parts the ‘serial in’ pin may be used as a general purpose input and also Output pin 0 (leg 7) is used during the program download, but can also be used as a general purpose output once the download is complete'
If that is so I can use a 8M2 which will give me 4 outputs and 2 inputs. Than it has to be programed on a program board
This is what I found in the manual:

Using the Serial In pin as a general input pin
On M2 and X2 parts the ‘serial in’ pin may be used as a general purpose input,
connected to a switch as shown.
However there are certain special conditions to this use:
1) The program must contain a ‘disconnect’ command. This command prevents
the PICAXE chip from scanning the serial in pin for new program downloads.
If you do not add this command the PICAXE will reset when the switch is
pressed.
2) After a ‘disconnect’ command is used it will be necessary to perform a power on
‘hard-reset’ to download a new program
3) The switch must be open during a new program download.

Output pin 0 (leg 7) is used during the program download, but can also be used as a general purpose output once the download is complete"
 

BillBWann

Member
The following code should provide you with what you asked for. Pin C.5 is the sequence enable pin while C.3 is the current sense input. As I’m sure you’re aware, the enable pin (C.5) needs to be low when the 08M2 is first powered up. Once the 08M2 has been programmed, all subsequent reprogramming will require a hard reset.

Code:
#picaxe 08m2
#no_data

disconnect
let dirsC = %00110111    'pins c.0,1,2,4 as outputs

do
    strt:
    b0=1
    b1=0
    if pinc.5=1 then
        for b2=0 to 3
            b3=b2
            b1=1
            if b2=3 then
                b1=16    'select pin c.4
            else    '
                do while b3<>0
                    b3=b3-1
                    b1=b1*2    'select progressively pins c.0 to c.2
                loop
            endif
            pinsC = b1
            pause 2000    'wait for CT to switch on
            do
                b0=pinsC and %00001000
            loop while bit3>0 and pinc.5=1
            pinsC=0    'switch off all relays (3 should already be off)
            if pinC.5=0 then strt    
        next b2
        goto strt:
    endif
loop
 

Visser

Well-known member
The following code should provide you with what you asked for. Pin C.5 is the sequence enable pin while C.3 is the current sense input. As I’m sure you’re aware, the enable pin (C.5) needs to be low when the 08M2 is first powered up. Once the 08M2 has been programmed, all subsequent reprogramming will require a hard reset.

Code:
#picaxe 08m2
#no_data

disconnect
let dirsC = %00110111    'pins c.0,1,2,4 as outputs

do
    strt:
    b0=1
    b1=0
    if pinc.5=1 then
        for b2=0 to 3
            b3=b2
            b1=1
            if b2=3 then
                b1=16    'select pin c.4
            else    '
                do while b3<>0
                    b3=b3-1
                    b1=b1*2    'select progressively pins c.0 to c.2
                loop
            endif
            pinsC = b1
            pause 2000    'wait for CT to switch on
            do
                b0=pinsC and %00001000
            loop while bit3>0 and pinc.5=1
            pinsC=0    'switch off all relays (3 should already be off)
            if pinC.5=0 then strt   
        next b2
        goto strt:
    endif
loop
Thank you
I get a syntax error at line 12 and cant fix it
Vissie
 

BillBWann

Member
I get a syntax error at line 12 and cant fix it
I get line 12 to be "for b2=0 to 3". Is that the line you are having problems with? Do you have a corresponding "next b2" on line 30? Maybe, you haven't copied the whole program.

If that isn't it, can I suggest you copy & paste the offending line and also quote the syntax error.

Bill
 

Visser

Well-known member
Ok. It works perfectly now
For some reason when I copied and pasted line 30 went missing
Thank you so much
Vissie
 
Top