The MAGIC SWITCHBOARD or SWITCHBOX

bagpipeman1

New Member
Hi All!!!!


Im new to this fourm so please be patient!

Anyhow , i am an electrician from NZ. I have recently came accoss this link on youtube , regarding the Magic Switchboard , where the magician apears to swap lamps , and lamp covers and magicly the lamps apear to follow the switch. This has me well baffled , and im trying to find out how it all works , and bulid one of my own ( via PLC ), However not knowing exactly how it all works has me buggered.

I am guessing that these devices use a microcontroller , sadly something electricians as a rule dont get to play with. I have downloaded the "basics" about picaxe , but it kinda has gone wayyyyy over my head ,not doing much with electronics.

These devices , if purchaed from "wellington industries" will set me back about 600 NZ dollars!!! , a lot to pay to impress the mates!

Anyhow , is there anyone out there that could help me? Or poss program one for me , i can do the hardware , ie make the box , and put it all together with switchs lamps etc....

Any help appreciated!

Regards

Craig.
 

John O

Senior Member
If you search this forum for 'magic switchbox' it'll come up with a couple of longish threads.

John.
 

Andrew Cowan

Senior Member
Including code written by Technical (and more code written/modified by others) on how to make your own using a PICAXE.

I made mine using an 18X for about £5.
 

bagpipeman1

New Member
Including code written by Technical (and more code written/modified by others) on how to make your own using a PICAXE.

I made mine using an 18X for about £5.
Hi there

Thanks for the reply , i do not have any knowledge on PICAXE really at all......yes dumb sparky here:) im picking its a microconroller or something?

Im not up with all the play on what i need etc etc , is there any chance i could get someone that HAS the knowelge to build one for me?? Im willing to pay all expences of course! I just havnt got any gear or the knowlege to build this device myself. I play with Phases Neutral and Earth , and the odd PLC program , but nothing that involves Micro-Chips!!!

Thanks again ,

Craig.
 

eclectic

Moderator
Craig.
Where are you in NZ?

There are a fair number of Kiwis
on the Forum who might
be able to advise you further.

e
 

cachomachine

Senior Member
I Craig

You can buy a Magic Switchbox for US$119.99
Their newest version let you swap bulbs and switchcaps even if the switches are turned ON.
They also have incorporate the multiple bulb lighting from a single switch.
Their youtube demo is http://www.youtube.com/watch?v=ZOodYc1Aio0.
They sell on Ebay, just search for Magic Switchbox

Bob
 

bagpipeman1

New Member
I am from Timaru , in the south island.

I realise that i can purchase the device from Ebay , at 200US dollars , but thats still $400.OO NZ dollars , i was hoping that i could get this thing built for less than that!.....if someone already has the code , then im guessing that its a matter of firing it on the Microchip , solder to a board , and attach the external components......anyone willing to build the PCB for me , and i pay for that bit? can make lamps myself as long as i know the voltage to get

Craig.
 

techElder

Well-known member
OH, I see ... you wanted someone to do something for you for free! Why didn't you just say so in the first place? Well, of course you'll pay for the parts because you're so magnanimous!

Get real ...
 

Buzby

Senior Member
Hi Bagpipeman,

I can see just what you want, and that you are willing to pay for it, not get it for free.

You want a Magic Switchbox, but cheaper than buying one.

You are willing to pay for the PCB and coding, and will do the mechanical construction yourself.

I think this is your perfect opportunity to learn PICAXE !

You already know about electrics, the things about voltages and currents are second nature to an industrial sparkie.

You already know about one form of programming, i.e. PLC. All programming is basically the same, just written differently. In your PLC you use contacts and coils to 'draw' the circuit, and say thing like 'If contact_1 is closed then turn on coil_6.

In PICAXE it's very similar, but we don't 'draw' the circuit, we go straight to the words, 'If input.1 is On Then Turnon output.6'

A pre-built off the shelf PCB from the PICAXE Online store ( at the top of this page ) will give you four outputs to light the lamps, and inputs for the switches. ( I can't remember the actual part number, but someone will. )

Your 'fear' of microcontrollers is understood, they can be very difficult to work with.

PICAXE is very different. It is a microcontroller which is pre-programmed with an easy to use language. If you've programmed anything before, even PLCs like me, then learning PICAXE will take no time at all.

Why not download the Programming Editor and try it for yourself ?. You don't need a PICAXE for this, there is a very good simulator built into the editor, with loads of examples, and it's all free !.

You will get a lot of help from this forum if you are seen to be trying to 'roll your own', but not quite so much if you just want to pay someone to do it for you.

Cheers,

Buzby
 
Last edited:

cdngunner

Senior Member
[edit: Just noticed Buzby pretty much wrote the same thing.......]

As one dumb sparky to another..........

I have recently joined here too not knowing about pics and very little about electronics. Between the brain trust here, the search function here, and ofcourse ,my favourite, google, there isn't much you shouldn't be able to figure out yourself.

To be kinda blunt, one sparky to another, if yuo cant figure this stuff out with your background, I feel sorry for you and wonder what kind of electrician you are.

On the other hand if you want to expand your knowledge, this forum and the project your thinking of will do just that.

Search the forum and make a start by buying some of the demo kits available from picaxe. Although you may suck at this now, within a couple of weeks with the help of this forum you should be well on your way.

This could be a great learning experience and you should grab the bull by the horn.

Have I mentioned that there are tonnes of people here who love nothing better then helping those who wish to help themselves.

Anyway, good luck. I think your selling yourself short by getting someone else to do it for you.
 
Last edited:

bagpipeman1

New Member
Hi there Buzby ,

Thanks for the advice.....and NO texas man i am NOT after someone to do it for FREE. I just do not have the knowledge to build this device myself , as i am an ELECTRICIAN NOT a Electronics expert!

Hey buzby ill have a go with picaxe , where do i get the program to get started , might as well have a crack at it!!!

Sorry to all that i offended , after a "freebee" :-(

Craig.
 

lbenson

Senior Member
>where do i get the program to get started

As suggested, search the forum and read the previous threads.

You can start programming without any equipment at all, other than your PC. Download the Programming Editor (click on "Software & Drivers" on the forum header and follow the links). When the program is installed, you can type your code and press "Simulate" to see how your code runs. Here's a very simple sample program to turn one LED on and off.

Code:
main:
  high 0
  pause 1000
  low 0
  pause 1000
  goto main
Download and have a read of the manuals--1,2,&3. Get those by clicking "PICAXE Manual" on the header line.
 

bagpipeman1

New Member
' Start of program
do_reset:
' ' reset position counter
if starting_lite = 1 then ' if starting with bulb 1, position reset to 15.
position = 15
end if

if starting_lite = 2 then ' if starting with bulb 2, position reset to 14.
position = 14
end if

if starting_lite = 3 then ' if starting with bulb 3, position reset to 13.
position = 13
end if

if starting_lite = 4 then ' if starting wtih bulb 4, position reset to 12.
position = 12
end if

flags = 0 ' reset flags
light0=0 ' reset all lights to off.
light1=0
light2=0
light3=0

' Learning loop
waiting_to_learn_loop:
' if not learnt switch learn it
if pin1 = 1 and flag0 = 0 then learn0
if pin0 = 1 and flag1 = 0 then learn1
if pin7 = 1 and flag2 = 0 then learn2
if pin6 = 1 and flag3 = 0 then learn3

' we have learnt that switch so light output accordingly
if flag0 = 1 then
if pin1 = 1 then
high light0
else
low light0
end if
end if
if flag1 = 1 then
if pin0 = 1 then
high light1
else
low light1
end if
end if

if flag2 = 1 then
if pin7 = 1 then
high light2
else
low light2
end if
end if

if flag3 = 1 then
if pin6 = 1 then
high light3
else
low light3
end if
end if

if pin0=0 and pin1=0 and pin7=0 and pin6=0 then ' count down to timeout even while in learning loop
pause 10 'wait 10ms
inc timeout_counter 'inc timeout
if timeout_counter > timeout then
timeout_counter=0
goto do_reset
end if
else
timeout_counter=0
end if

goto waiting_to_learn_loop

' Learn a light position and set flag so we know that switch is done
learn0:
' position gives you the output for this switch
flag0 = 1 ' set flag
light0 = position ' remember position for this switch
goto learn_end

learn1:
' position gives you the output for this switch
flag1 = 1 ' set flag
light1 = position ' remember position for this switch
goto learn_end

learn2:
' position gives you the output for this switch
flag2 = 1 ' set flag
light2 = position ' remember position for this switch
goto learn_end

learn3:
' position gives you the output for this switch
flag3 = 1 ' set flag
light3 = position ' remember position for this switch
'goto learn_end

learn_end:
if starting_lite = 1 then ' if starting with bulb 1
dec position ' subtract 1 to position counter
if position < 12 then have_learnt_all ' all done
end if

if starting_lite = 2 then ' if starting with bulb 2
inc position ' add 1 to position counter
if position > 15 then ' if position counter is greater than 15
position = 12 ' make it 12 instead (loop around to bulb 4)
else
if position = 14 then have_learnt_all ' else if position equals 14 then all bulbs learned
end if
end if

if starting_lite = 3 then ' if starting with bulb 3
dec position ' subtract 1 from position counter
if position < 12 then ' if position counter is less than 12
position = 15 ' make it 15 instead (loop around to bulb 1)
else
if position = 13 then have_learnt_all ' else if position equals 13 then all bulbs learned
end if
end if

if starting_lite = 4 then ' if starting with bulb 4
inc position ' add 1 to position counter
if position > 15 then have_learnt_all ' all done
end if


goto waiting_to_learn_loop ' not all done so back to loop

' now simply loop reacting to the switches
' timeout_counter value will increment every 10ms
' however if any light is on the timeout_counter is reset
' so this means the timeout will only
' occur after 10 secoonds of all switches off

have_learnt_all:
if pin1 = 1 then
high light0
timeout_counter = 0
else
low light0
end if

if pin0 = 1 then
high light1
timeout_counter = 0
else
low light1
end if

if pin7 = 1 then
high light2
timeout_counter = 0
else
low light2
end if

if pin6 = 1 then
high light3
timeout_counter = 0
else
low light3
end if


'Set which bulb will start next round. (only if switches all learned)
if b0 = 15 and pin1=1 and pin0=0 and pin7=0 and pin6=0 then ' if switch 1 on alone, set bulb 1 first
starting_lite=1
end if

if b0 = 15 and pin1=0 and pin0=1 and pin7=0 and pin6=0 then ' if switch 2 on alone, set bulb 2 first
starting_lite=2
end if

if b0 = 15 and pin1=0 and pin0=0 and pin7=1 and pin6=0 then ' if switch 3 on alone, set bulb 3 first
starting_lite=3
end if

if b0 = 15 and pin1=0 and pin0=0 and pin7=0 and pin6=1 then ' if switch 4 on alone, set bulb 4 first
starting_lite=4
end if

pause 10 ' wait 10ms
inc timeout_counter ' inc timeout

' if timed out then reset else loop
if timeout_counter > timeout then do_reset
goto have_learnt_all



Hi all this is what i copyed and pasted on the picaxe program page , however when i hit "simulate" it comes up with "syntax error" and stuff. I have tryed to delete some of what i have put in there but im unsure what code i am ment to be copying and pasting , purhaps i have copyed something in the program i shouldnt have??

Advice?

Cheers

Craig.
 

bagpipeman1

New Member
Sorry forgot to mention that even when i open a pre installed program , the samples to see whats going on , then when i hit simulate , then i get other syntax errors etc too Even without modifying the program

Craig
 

bagpipeman1

New Member
i have tryed severl different ones but its really above my head , all these symbols dont seam to make sence. Im used to ladder logic.

What settings should i have , i.e how do i make this puppy run? Also , what does the outputs do , obversly they are not clean contacts , but some kinda voltage output? This is frustrating me , and doesnt help that that texts man implied that i was "dumb" because i cant understand this foreign symbol language. 'bits and bytes ETC , are a computer programmers language , and im into PLC ladder logic..........

feeling pretty crap rite bout now!

Craig.
 

Svejk

Senior Member
Excuses excuses excuses. If you know Ladder Logic this should be a breeze.

Why don't you start with a 08M or 14M starter kit and build your knowledge from there? You'll be better off.

Svejk,
just another sparkie
 

bagpipeman1

New Member
Excuse me mate , but tell me how a buch of this relates to ladder logic in any way?

Thats a bit like saying if you can drive a car you can fly a plane.

I see the THEORY behind it kindof but its a bunch words! no diagrams or anything mate.flags = 0 ' reset flags
light0=0 ' reset all lights to off.
light1=0
light2=0
light3=0

and in ladder this means????

Im trying my best here , but i seam to be getting told this is "easy" well im sorry mate , but its not that simple for some people.....of corse everything is simple when you get to know it.....but ive neaver even heard of Picaxe until tonight......what is the difference in the chips? to me at the moment it seams just numbers.

Sorry to have a bitch but feeling pretty knocked back.

Craig.


' Start of program
do_reset:
' ' reset position counter
if starting_lite = 1 then ' if starting with bulb 1, position reset to 15.
position = 15
end if

if starting_lite = 2 then ' if starting with bulb 2, position reset to 14.
position = 14
end if

if starting_lite = 3 then ' if starting with bulb 3, position reset to 13.
position = 13
end if

if starting_lite = 4 then ' if starting wtih bulb 4, position reset to 12.
position = 12
end if

flags = 0 ' reset flags
light0=0 ' reset all lights to off.
light1=0
light2=0
light3=0

' Learning loop
waiting_to_learn_loop:
' if not learnt switch learn it
if pin1 = 1 and flag0 = 0 then learn0
if pin0 = 1 and flag1 = 0 then learn1
if pin7 = 1 and flag2 = 0 then learn2
if pin6 = 1 and flag3 = 0 then learn3

' we have learnt that switch so light output accordingly
if flag0 = 1 then
if pin1 = 1 then
high light0
else
low light0
end if
end if
if flag1 = 1 then
if pin0 = 1 then
high light1
else
low light1
end if
end if

if flag2 = 1 then
if pin7 = 1 then
high light2
else
low light2
end if
end if

if flag3 = 1 then
if pin6 = 1 then
high light3
else
low light3
end if
end if

if pin0=0 and pin1=0 and pin7=0 and pin6=0 then ' count down to timeout even while in learning loop
pause 10 'wait 10ms
inc timeout_counter 'inc timeout
if timeout_counter > timeout then
timeout_counter=0
goto do_reset
end if
else
timeout_counter=0
end if

goto waiting_to_learn_loop

' Learn a light position and set flag so we know that switch is done
learn0:
' position gives you the output for this switch
flag0 = 1 ' set flag
light0 = position ' remember position for this switch
goto learn_end

learn1:
' position gives you the output for this switch
flag1 = 1 ' set flag
light1 = position ' remember position for this switch
goto learn_end

learn2:
' position gives you the output for this switch
flag2 = 1 ' set flag
light2 = position ' remember position for this switch
goto learn_end

learn3:
' position gives you the output for this switch
flag3 = 1 ' set flag
light3 = position ' remember position for this switch
'goto learn_end

learn_end:
if starting_lite = 1 then ' if starting with bulb 1
dec position ' subtract 1 to position counter
if position < 12 then have_learnt_all ' all done
end if

if starting_lite = 2 then ' if starting with bulb 2
inc position ' add 1 to position counter
if position > 15 then ' if position counter is greater than 15
position = 12 ' make it 12 instead (loop around to bulb 4)
else
if position = 14 then have_learnt_all ' else if position equals 14 then all bulbs learned
end if
end if

if starting_lite = 3 then ' if starting with bulb 3
dec position ' subtract 1 from position counter
if position < 12 then ' if position counter is less than 12
position = 15 ' make it 15 instead (loop around to bulb 1)
else
if position = 13 then have_learnt_all ' else if position equals 13 then all bulbs learned
end if
end if

if starting_lite = 4 then ' if starting with bulb 4
inc position ' add 1 to position counter
if position > 15 then have_learnt_all ' all done
end if


goto waiting_to_learn_loop ' not all done so back to loop

' now simply loop reacting to the switches
' timeout_counter value will increment every 10ms
' however if any light is on the timeout_counter is reset
' so this means the timeout will only
' occur after 10 secoonds of all switches off

have_learnt_all:
if pin1 = 1 then
high light0
timeout_counter = 0
else
low light0
end if

if pin0 = 1 then
high light1
timeout_counter = 0
else
low light1
end if

if pin7 = 1 then
high light2
timeout_counter = 0
else
low light2
end if

if pin6 = 1 then
high light3
timeout_counter = 0
else
low light3
end if


'Set which bulb will start next round. (only if switches all learned)
if b0 = 15 and pin1=1 and pin0=0 and pin7=0 and pin6=0 then ' if switch 1 on alone, set bulb 1 first
starting_lite=1
end if

if b0 = 15 and pin1=0 and pin0=1 and pin7=0 and pin6=0 then ' if switch 2 on alone, set bulb 2 first
starting_lite=2
end if

if b0 = 15 and pin1=0 and pin0=0 and pin7=1 and pin6=0 then ' if switch 3 on alone, set bulb 3 first
starting_lite=3
end if

if b0 = 15 and pin1=0 and pin0=0 and pin7=0 and pin6=1 then ' if switch 4 on alone, set bulb 4 first
starting_lite=4
end if

pause 10 ' wait 10ms
inc timeout_counter ' inc timeout

' if timed out then reset else loop
if timeout_counter > timeout then do_reset
goto have_learnt_all
 

Svejk

Senior Member
if pin1 = 1 and flag0 = 0 then learn0
if pin0 = 1 and flag1 = 0 then learn1
if pin7 = 1 and flag2 = 0 then learn2
if pin6 = 1 and flag3 = 0 then learn3
Not that hard:

|--[xic IN.0]--[xio flag.1]--[jsr learn1]--|
|--[xic IN.1]--[xio flag.2]--[jsr learn2]--|
|--[xic IN.2]--[xio flag.3]--[jsr learn3]--|

But, as I said, start with something simpler and build you knowledge from there. My 2c worth.
 

hippy

Technical Support
Staff member
Sorry to have a bitch but feeling pretty knocked back.
That's understandable. As you say, it's easy when you're familiar with things and know how to, a completely different matter when you don't. I expect it's just like when you first encountered ladder logic and didn't understand what all that meant either.

Just like knowing French doesn't mean you know German and knowing German doesn't mean you can instantly program ladder logic, knowing ladder logic doesn't mean you can program PICAXE. It's a case of having to learn each.

There's really no way to learn anything without studying and understanding it, trying out small pieces until you are confident enough to tackle the more complicated. What you know will often help understand the new, but until the basics are grasped you're stuck at the early hurdles.

The best approach is to try smaller and simpler PICAXE programs, to get a feel for the language and how the Programming Editor works.
 

cdngunner

Senior Member
Good for your!

Knowing plc programming does not automatically qualify you as a picaxe expert, but the logical sequential thinking will take you a long way.

Don't get flustered. It took me about a month to do a little program. It was all the little things witch tripped me up. Like making sure you have the latest version of software for the chips your using.

Keep pluggin away, you will get there.
 

SAborn

Senior Member
You asked what chip to select in options to suit the program you are trying to simulate.

I understand it was written for the 18X picaxe chip.

If the program you posted earlier is all you have, then you are missing some of it from the start, that has all the symbols etc.
Without the full program you will get syntax errors.

Yes it is hard when you start out and try get an understanding to picaxe in one big step, but it is easy if taken in little steps, as once you understand a few of the basics the rest comes together rather fast then.

We all started out just like you, and i know it all looked like Chinglish to me at the beginning.

Hang in there and dont expect to master it in the first few attempts.
If need be some of us dont mind going 1 on 1 with you to help you learn.



Taken from post #32 in this thread, is what i understand to be the last program version presented by Wayne Thomason.
http://www.picaxeforum.co.uk/showthread.php?t=12873&highlight='magic+switchbox'&page=4

The attached file below is the full program and passes a syntax check..
 

Attachments

Last edited:

moxhamj

New Member
but ive neaver even heard of Picaxe until tonight
I've never heard of ladder logic till tonight, so I checked it out. It is mostly greek to me at the moment, but maybe we can muddle through and work some things out?

Regarding that code you posted, that looks unintelligable to me too, and I can program a picaxe. I suspect it would look a lot more logical in a picture language like ladder logic.

First thing that strikes me is that ladder logic works in parallel, and a picaxe works sequentially. But the picaxe can appear to work in parallel.

start:
ladder1
ladder2
ladder3
goto start

In general terms, that will cycle through about 100 to 1000 times a second, so it will appear like all the ladders are working in parallel.

Next bit is logic. Put two relays in series and both have to be on for the output to be on. In code, that is "and"

Put two relays in parallel, and either can be on for the output to be on. In code that is "or".

You can use numbers to represent a relay being on or off. eg zero is off, and 1 is on.

To keep things simple, there are 14 registers in a picaxe. You could call them relays if you like. Code might use b0 as a register, but you could call that "relay0" and there are ways to rename registers.

So -
relay0 = 0
relay1 = 1

Put them one after the other in ladder logic. The output is off.

In code, it is something like

if relay0 = 1 and relay1=1 then
output = 1
else
output = 0
endif

The code isn't exactly that, but that is the general idea.

If you like we can then move on to 'greater than' and timers and pulse generaters and latches. Maybe keep things simple first though. That code you posted is rather daunting. I'd start by trying to flash a led, and then read an input, and then read two inputs and turn the led on if both inputs are 'on'.
 
Last edited:

magicjohn

New Member

SAborn

Senior Member
VERY nice work, i very much like the style of switches you have there, it gives a real touch of class.
 

westaust55

Moderator
Yes an 18M2 can be used.

The original code at post 32 is for an earlier 18X
Just change the pin assignments at the SYMBOL statements
And add a DIRSB command at the start of the program to set the PortB pins as outputs.
 

Pererobert

New Member
Thanks for our reply. One question ; what happened with pin 4 ? It is a reset on Picaxe 18X and serial In on Picaxe 18M2. Not link up or link to + with a resistance of 4K7 like scheme ?
 

westaust55

Moderator
See PICAXE manual 1 pages 9 and 10.
http://www.picaxe.com/docs/picaxe_manual1.pdf

The older PICAXE 18X has an external rest function on physical pin/leg 4 which must be pulled high for normal operation.
The 18M2 in line with all of the M and M2 parts does not have an external RESET function and the pin is used as a normal input only pin.

The SerialIn is on physical pin/leg 3 for both the 18X and the 18M2 parts.
 

Buzby

Senior Member
Seeing as this thread wandered across Ladder Logic a few years ago, I thought I'd put this here.

Found last night while searching for PIC compilers, I found this : http://cq.cx/ladder-tutorial.pl

It's a simulator and compiler to run Ladder Logic on a PIC ( not a PICAXE ! )

I've not tried it, but it might be fun for someone to give it a go.

Cheers,

Buzby
 

marquelys

New Member
Hello I am in France and wish to build magic switchboard can you advise(recommend) to me?
There is a version with peak 16F88?
Or other version picaxe 18M2?
Thank you for advising(recommending) to me
 

Bill.b

Senior Member
This is the schematic I used in my magic switchboard, The secret of the operation without batteries is the addition of a super cap of 1F.

pxmagicsb.jpg


The program is for a 14m2 but can modified for the 18m2 by reassigning the IO.

Code:
#picaxe 14m2
 
' Magic Switchboard - original source by "Technical" from PicAxe Forum
'                     Modified for PicAxe 18x by Wayne Thomason of Addison, TX USA
'                     7/15/2009  
'			    Modified for Picaxe 14m2 By Bill Brown
'			    12/8/2012
'
'                      mods: 1. Now is easily configurable via switch? and bulb? variables
'                            2. "timeout" functions even without learning all 4 switches
'                            3. starting point and sequence direction dependent on last switch turned off
'                            4. Now has Audience_lockdown feature.  If power is turned on while switch-4 
'                               is set, each light will respond only to corresponding switch position 
'                               until circuit is reset.
'
' Assumptions
' 1. Times out after 10 seconds of all switches in the off position
'    regardless of whether all switches are learned yet
' 2. All switches must be off at start
'    (If switch 4 on when started, it starts up in audience-mode.)
' 3. All 4 switches must be switched on before that sequence is learned
' 4. Set bulb/LED outputs using bulb1, bulb2, bulb3 & bulb4
' 5. Set switch inputs using switch1, switch2, switch3 and switch4
' 6. first pattern is left to right, bulbs 1, 2, 3, 4
' 7. subsequent patterns are determined by last SWITCH turned off:
'       Switch 1 = 1234 order (bulb 1 first, then right in sequence)
'       Switch 2 = 2143 order (bulb 2 first, then left in sequence, wrapping after first)
'       Switch 3 = 3412 order (bulb 3 first, then right in sequence, wrapping after last)
'       Switch 4 = 4321 order (bulb 4 first, then left in sequence)
 
symbol switch1 = pinc.3  ' input pin for switch 1  (pinc.0, pinc.1, pinc.2 and pinc.3)
symbol switch2 = pinc.2  ' input pin for switch 2
symbol switch3 = pinc.1  ' input pin for switch 3
symbol switch4 = pinc.0  ' input pin for switch 4

symbol bulb1 = b.3       ' output for bulb 1  (b.0, b.1, b.2 and b.3)
symbol bulb2 = b.2       ' output for bulb 2
symbol bulb3 = b.1       ' output for bulb 3
symbol bulb4 = b.0       ' output for bulb 4

symbol timeout = 250  ' each loop takes a little more than twice as long as original code
                      ' by "Technical (about 2ms per loop).  450 x 2ms x 10ms = about 10
                      ' seconds. Your mileage may vary - tweak timeout if needed.
                      
'Change "timeout" to smaller number when simulating or you will get bored!


symbol flags = b0  ' flags to remember whether a switch has been learnt yet
symbol flag0 = bit0
symbol flag1 = bit1
symbol flag2 = bit2
symbol flag3 = bit3
 
symbol light0 = b1 ' variable to remember which switch is which light
symbol light1 = b2
symbol light2 = b3
symbol light3 = b4
 

symbol bulb = b7
symbol starting_lite = b6 ' variable to set starting light position (and direction)
symbol position = b5  ' position counter
symbol timeout_counter = w6 ' timeout counter

if switch4=1 then audience_lockdown



starting_lite = 1
 
' Start of program
do_reset:
'                ' reset position counter       
 if starting_lite = 1 then  ' if starting with bulb 1, position reset to 0.
    position = 0 
 end if
 
 if starting_lite = 2 then  ' if starting with bulb 2, position reset to 1.
    position = 1
 end if
 
 if starting_lite = 3 then  ' if starting with bulb 3, position reset to 2.
    position = 2
 end if
 
 if starting_lite = 4 then  ' if starting wtih bulb 4, position reset to 3.
    position = 3
 end if
 
 flags = 0  ' reset flags
 
' Learning loop
waiting_to_learn_loop:
' if not learnt switch learn it
 if switch1 = 1 and flag0 = 0 then learn0
 if switch2 = 1 and flag1 = 0 then learn1
 if switch3 = 1 and flag2 = 0 then learn2
 if switch4 = 1 and flag3 = 0 then learn3
 
 ' we have learnt that switch so light output accordingly
 if flag0 = 1 then
  if switch1 = 1 then
   high light0
  else
   low light0
  end if
 end if 
 if flag1 = 1 then
  if switch2 = 1 then
   high light1
  else
   low light1
  end if
 end if 
 
 if flag2 = 1 then
  if switch3 = 1 then
   high light2
  else
   low light2
  end if
 end if 
 
 if flag3 = 1 then
  if switch4 = 1 then
   high light3
  else
   low light3
  end if
 end if
 
  if switch2=0 and switch1=0 and switch3=0 and switch4=0 then    ' count down to timeout even while in learning loop
    pause 10   'wait 10ms
    inc timeout_counter  'inc timeout
    if timeout_counter > timeout then 
       timeout_counter=0
       goto do_reset
    end if
 else
    timeout_counter=0
 end if
 
 
 goto waiting_to_learn_loop
 
' Learn a light position and set flag so we know that switch is done
learn0:
gosub bulbset
 ' position gives you the output for this switch
 flag0 = 1    ' set flag
 light0 = bulb   ' remember position for this switch
 goto learn_end
 
learn1:
gosub bulbset
 ' position gives you the output for this switch
 flag1 = 1    ' set flag
 light1 = bulb   ' remember position for this switch
 goto learn_end
 
learn2:
gosub bulbset
 ' position gives you the output for this switch
 flag2 = 1    ' set flag
 light2 = bulb   ' remember position for this switch
 goto learn_end
 
learn3:
gosub bulbset
 ' position gives you the output for this switch
 flag3 = 1    ' set flag
 light3 = bulb   ' remember position for this switch
 'goto learn_end
 
learn_end:

if starting_lite = 1 then    'if starting with 1st lamp, sequence = 1-2-3-4
   inc position
   if position > 3 then have_learnt_all
   goto waiting_to_learn_loop
end if


if starting_lite = 2 then                'if starting with 2nd lamp, sequence = 2-1-4-3
   if position > 0 then                  'don't dec if position=0, will cause error
      dec position
   else
      position = 3                       'position was 0 before dec, set to "3"
   end if
   if position = 1 then have_learnt_all  'if position=1 then we have come full circle
   goto waiting_to_learn_loop            'not finished, loop and learn more switches
end if

if starting_lite = 3 then                'if starting with 3rd lamp, sequence = 3-4-1-2
   inc position
   if position > 3 then                  'if position greater greater than 4th lamp, reset to "0"
      position = 0
   end if
   if position = 2 then have_learnt_all  'if position=2 then we have come full circle
   goto waiting_to_learn_loop            'not finished, loop and learn more switches
end if

if starting_lite = 4 then                'if starting with 4th lamp, sequence = 4-3-2-1
   if position > 0 then                  'don't dec if position=0, will cause error
      dec position
   else
      goto have_learnt_all               'position is "0", we have learned last position.
   end if
   goto waiting_to_learn_loop
end if
  
 
' now simply loop reacting to the switches
' timeout_counter value will increment every 10ms
' however if any light is on the timeout_counter is reset
' so this means the timeout will only 
' occur after 10 secoonds of all switches off
 
have_learnt_all:

 if switch1 = 1 then
  high light0
  timeout_counter = 0
 else
  low light0
 end if
 
 if switch2 = 1 then
  high light1
  timeout_counter = 0
 else
  low light1
 end if
 
 if switch3 = 1 then
  high light2
  timeout_counter = 0
 else
  low light2
 end if
 
 if switch4 = 1 then
  high light3
  timeout_counter = 0
 else
  low light3
 end if
 
 'Set which bulb will start next round. (only if switches all learned)
 if b0 = 15 and switch1=1 and switch2=0 and switch3=0 and switch4=0 then  ' if switch 1 on alone, set lamp 1 first
   starting_lite=1
 end if
 
 if b0 = 15 and switch1=0 and switch2=1 and switch3=0 and switch4=0 then  ' if switch 2 on alone, set lamp 2 first
   starting_lite=2
 end if
 
 if b0 = 15 and switch1=0 and switch2=0 and switch3=1 and switch4=0 then  ' if switch 3 on alone, set lamp 3 first
   starting_lite=3
 end if
 
 if b0 = 15 and switch1=0 and switch2=0 and switch3=0 and switch4=1 then  ' if switch 4 on alone, set lamp 4 first
   starting_lite=4
 end if
 
 pause 10   ' wait 10ms
 inc timeout_counter ' inc timeout
 
 ' if timed out then reset else loop
 if timeout_counter > timeout then do_reset
 goto have_learnt_all

bulbset:
if position=0 then 
   bulb = bulb1
end if

if position=1 then
   bulb = bulb2
end if

if position=2 then
   bulb = bulb3
end if
   
if position=3 then
   bulb = bulb4
end if
return

audience_lockdown:
  if switch1 = 1 then
   high bulb1
  else
   low bulb1
  end if

  if switch2 = 1 then
   high bulb2
  else
   low bulb2
  end if

  if switch3 = 1 then
   high bulb3
  else
   low bulb3
  end if

  if switch4 = 1 then
   high bulb4
  else
   low bulb4
  end if
goto audience_lockdown
As shown below, the circuit board is hidden under a false bottom in the battery compartment.
Also shown is the 1 farad capacitor which will allow the circuit to operate for approximately 15 min after the batteries are removed.


magicbox3.jpg

Bill
 
Last edited:

marquelys

New Member
Thank you bill for his(her,its) precision

I inform you of the advance(overhang) of my manufacturing

thank you again for your availability
 

marquelys

New Member
DSCF3096-min.JPGDSCF3096-min.JPG
Manufacturing of cables for programming ok found
programme utilise bas805.exe
Test on circuit ok
I continue the manufacturing thank you very much
 
Top