Input switch/count to ground

Axel87

Senior Member
Hey guys,

Working on a basic project with an 08m2.
I have worked with counting high pulses, but this is my first low and dont want to burn anything up.

My input(C.3) is a button that switches to ground momentarily.
I would like to count 3 pushes of that button to activate a simple program.

Do I make pin c.3 High? Do I need to add a resistor or anything to gnd?

I know I need a variable bit- b0 for a count
I added Variable bit b1 for program end? two presses of the button ends program.

Basic Schematic- View attachment 05 V Star Flasher Circuit.PDF

This is what I have thus far:

Thanks guys!

Code:
init:
	let b0 = 0 ;switch counter byte
	let b1 = 0 ;Flasher end
        
main:
	if b0 = 3 then flasher
	goto main
    
add:
	if c.3 = 0
	then b0 + 1
	

Flasher:

	High c.4	
	pause 1000
	low c.4
	pause 1000
	goto Flasher
 
Last edited:

ZOR

Senior Member
Have you tried running in the simulator? If you look, when the program reaches main, then if b0 does not equal 3 then it goes back to the label main.
Also, if it got to the add section, if c.3 was made 0 then it straight away goes to flasher.

I put this version in my simulator and it worked, however don't rely on it as untested, I just did it because the forum was quiet.

Code:
init:
	let b0 = 0 ;switch counter byte

        
main:
      if pinc.3 = 1 then goto add
      goto main
    
add:
	
	b0 = b0 + 1
	if b0=3 then goto Flasher
	pause 500
	goto main

Flasher:
	High c.4	
	pause 1000
	low c.4
	pause 1000
	goto Flasher
You might have to play with the Pause 500 value, it's to allow for the switch to go back to making C.3 = 0
 

AllyCat

Senior Member
Hi,

Do I make pin c.3 High? Do I need to add a resistor or anything to gnd?
NO, leave it as an input!

Basically, you need a Pull-Up resistor, typically about 10k from the switch to the positive supply (Vcc). Or the internal pullup resistor can be enabled using the PULLUP {8} command.

Yes, it would be wise to connect the switch to the pin via typically 1k, just in case you do drive the pin High and press the button!

However, your code cannot work because b0 is always zero and it will just cycle around main:. You need something like if pinc.3 = 0 then b0 = b0 + 1 : pause 100 : endif and ensure that all the individual parts of the program interact together (i.e. you have no instruction to exit the Flasher: or enter the Add: loops. Then you may need to consider aspects such as contact bounce and waiting for the button to be released, etc..

And for the diagram: R1 is far too high (< 100 ohms) and you must put a decoupling capacitor (say 10 uF) between Vdd and Vss pins. Also, it's well worth putting the full download circuit on all boards (for debugging, not just convenient programming).

Cheers, Alan.
 

Axel87

Senior Member
Hi,

NO, leave it as an input!

Basically, you need a Pull-Up resistor, typically about 10k from the switch to the positive supply (Vcc). Or the internal pullup resistor can be enabled using the PULLUP {8} command.

Yes, it would be wise to connect the switch to the pin via typically 1k, just in case you do drive the pin High and press the button!

However, your code cannot work because b0 is always zero and it will just cycle around main:. You need something like if pinc.3 = 0 then b0 = b0 + 1 : pause 100 : endif and ensure that all the individual parts of the program interact together (i.e. you have no instruction to exit the Flasher: or enter the Add: loops. Then you may need to consider aspects such as contact bounce and waiting for the button to be released, etc..

And for the diagram: R1 is far too high (< 100 ohms) and you must put a decoupling capacitor (say 10 uF) between Vdd and Vss pins. Also, it's well worth putting the full download circuit on all boards (for debugging, not just convenient programming).

Cheers, Alan.
Alan-

I already have the main circuit built. :/ But it does work like a charm

"need a Pull-Up resistor, typically about 10k from the switch to the positive supply (Vcc). Or the internal pullup resistor can be enabled using the PULLUP {8} command.

Yes, it would be wise to connect the switch to the pin via typically 1k"
Do I use a 10k or 1k then to the 5v supply? If I can,I would prefer to use the pullup command as the board and everything is built- this step was an after thought :/. If I do that, I can skip these correct?

"R1 is far too high (< 100 ohms) and you must put a decoupling capacitor"
I took a guess on R1, but ended up throwing it out on the actual assembly. I do not have a decoupling cap between pin 1 or 5 either. It main circuit works fine, is this necessary?

"full download circuit on all boards (for debugging, not just convenient programming)."
I skipped the d/l circuit because I wanted to save some space on the board.

I will take your advice on the coding, I will continue to play with it and get back to you with any questions!
Thanks!
 

AllyCat

Senior Member
Hi,

But it does work like a charm
It's uncountable the number of times that's been written in posts, then within a few days there's another post: "It's stopped working and I don't know why" !

Yes, the internal pullup resistor is a good "workaround", but you'll probably get lots of people looking at the circuit diagram and asking "where is the pullup?". A 1k resistor between the pin and switch is to limit the current if the pin is accidentally set high. And decoupling capacitors are important.

All my suggestions are strongly recommended because otherwise you very probably WILL be back here within a few days with problems such as "The code isn't working as expected/simulated", "Sometimes it works but then it stops", "I don't know how to know what the hardware/software is actually doing", "It's stopped working, smoke came out ...", etc,............

Cheers, Alan.
 

Axel87

Senior Member
Hi,

It's uncountable the number of times that's been written in posts, then within a few days there's another post: "It's stopped working and I don't know why" !

Yes, the internal pullup resistor is a good "workaround", but you'll probably get lots of people looking at the circuit diagram and asking "where is the pullup?". A 1k resistor between the pin and switch is to limit the current if the pin is accidentally set high. And decoupling capacitors are important.

All my suggestions are strongly recommended because otherwise you very probably WILL be back here within a few days with problems such as "The code isn't working as expected/simulated", "Sometimes it works but then it stops", "I don't know how to know what the hardware/software is actually doing", "It's stopped working, smoke came out ...", etc,............

Cheers, Alan.
LOL.
Alright Alan, I will take your advice then and add the cap.
Should I use a 1K as a pull up then or a 10k?

As far as the code goes-
I am looking into setint, as I presume I will need an interrupt to cancel the flash program.
Not experienced with this at all-
I presume the %0000ect is telling the picaxe a binary representation of a pin correct?
Basically, how do i translate the %000011 ect gibberish lol ;)

Am I heading in the right path on the setint?

As far as adding the variable-
I was planning on using c.3 because that is an input correct? But when I run this code I get an error "C.0 is an output only"

init:
let b0 = 0 ;switch counter byte
let b1 = 0 ;Flasher end
pullup pin3


main:
if b0 = 3 then flasher
goto main
if c.3 = 0 then goto add
add:
b0 + 1


Flasher:

High 4
pause 1000
low 4
pause 1000
goto Flasher

Can I not use c.3 and should be using a different pin?
 
Last edited:

AllyCat

Senior Member
Hi,

There's no need to "quote" the complete previous post (just use Reply) unless there are (many) intermediate posts.

10k is a typical pullup value (the internal is about 30k, but suitable for most applications). Actually, the 1k (protection) is not necessary ONLY for pin c.3 beacuse it's an "input only" pin (as is c.5). C.0 is indeed "Output only", but why is that an issue as you're using C.3? All other pins can be used as input or output, but start as inputs until programmed otherwise.

No, you do not need to use interrupts, and certainly not for the purpose you suggest.

What code are you now referring to (since there is no c.0 in any of the code above) and have you syntax checked and tried to simulate it?

Cheers, Alan.
 

westaust55

Moderator
As already stated, the normal arrangement is to use a 10 kOhm resistor for the pull-up.
A 10 kOhm resistor provides sufficient pull-up without excessive current when the switch is pressed.
You can also add a resistor (a value from say 330 Ohms to 1 kOhm is good) between the junction of the pull-up and switch to the PICAXE IO pin.
 
Last edited:

Axel87

Senior Member
I am a little lost here guys-

Code:
init:
	let b0 = 0 ;switch counter byte
	let b1 = 0 ;Flasher end
	pullup c.3 
        
       
main:
	if b0 < 3 then flasher
	if b1 > 3 then goto Cancel 
	if c.3 = 0 then goto add
add:
	if c.3 = 0 then b0,b1 + 1

Cancel

	if b1 = 2 then goto main
	

Flasher:

	High 4	
	pause 1000
	low 4
	pause 1000
	goto Flasher
 

Rick100

Senior Member
I am a little lost here guys-
Hello Axel87,

The reason for the error on the 'pullup c.3' is the compiler interprets the line as 'pullup %00011' which would activate pullups on c.0 and c.1 but c.0 can't be an input. The term c.3 is actually a constant in the compiler with a value of 3. The correct way to activate the pullup on c.3 is 'pullup %01000' . I changed your code enough to pass syntax but it still won't do what you want as it just jumps to the 'Flasher' routine which has no exit.



Code:
init:
	let b0 = 0 ;switch counter byte
	let b1 = 0 ;Flasher end
	'pullup c.3		would be the same as pullup %00011
	pullup %01000 
        
       
main:
	if b0 < 3 then flasher
	if b1 > 3 then goto Cancel
	'if c.3 = 0 then goto add
	if pinc.3 = 0 then goto add

add:
	'if c.3 = 0 then b0,b1 + 1
	if pinc.3 = 0 then
		b0 = b1 + 1
	endif
	'if c.3 = 0 then b0,b1 + 1

Cancel:

	if b1 = 2 then goto main
	

Flasher:

	High 4	
	pause 1000
	low 4
	pause 1000
        goto Flasher
Good luck,
Rick
 
Last edited:

Axel87

Senior Member
Thanks for the input Rick

If I may ask, my binary is a little rust but from what I remeber 0011 would be 3 correct?
I believe I am translating these binary words wrong. Anyone clear this up for me?

Here is my latest attempt with Rick's recommendation-

Code:
init:
	let b0 = 0 ;switch counter byte
	let b1 = 0 ;Flasher end
	pullup %01000  
        
       
main:
	if pinc.3 = 1 then b0 + 1 
	if b0 = 3 then flasher
	if b1 > 2 then goto main 
	
add:
	b0 + 1
	b1 + 1
	goto main
	

Flasher:

	High 4	
	pause 1000
	low 4
	pause 1000
	goto main
I am getting an error with the variables.
I believe I am overlooking something with the If...Then command.

Any recommendations or can anyone help clear these issues up for me?

Thanks guys!
 

Rick100

Senior Member
Thanks for the input Rick

If I may ask, my binary is a little rust but from what I remeber 0011 would be 3 correct?
I believe I am translating these binary words wrong. Anyone clear this up for me?
Binary 0011 does equal 3 in decimal. The windows calculator does binary to decimal conversions.

I am getting an error with the variables.
I believe I am overlooking something with the If...Then command.
The Picaxe If command takes several forms. The line "if pinc.3 = 1 then b0 + 1" will have to converted to:
Code:
	if pinc.3 = 1 then
	   b0 = b0 + 1
	endif
Look in manual 2 for the various forms of the if statement. I almost always have manual 2 open when I'm writing a program.

Also "b0 + 1" is not correct. It needs to be "b0 = b0 + 1" or "inc b0" .
Even after you correct these syntax problems, I don't think it will do what you want. Could you describe exactly what the program is supposed to do? Is it a learning exercise or will you be trying to control something?

Good luck,
Rick
 

tony_g

Senior Member
%00000000 would be used as a quick way to set a pin high or low or as input/output with the let dirsxx command,

%00000100 would indicate pin2, %00000111 would be pins 2,1,0 and so forth.
 

Axel87

Senior Member
%00000000 would be used as a quick way to set a pin high or low or as input/output with the let dirsxx command,

%00000100 would indicate pin2, %00000111 would be pins 2,1,0 and so forth.
Just checking my understanding here. %00000111 is not actually binary but more of a pin placement starting from the left-8 to 0 on the right?
Hypothetically if I wanted to turn pin 7 on it would be %01000000.
Is this correct?
 

Axel87

Senior Member
Binary 0011 does equal 3 in decimal. The windows calculator does binary to decimal conversions.



The Picaxe If command takes several forms. The line "if pinc.3 = 1 then b0 + 1" will have to converted to:
Code:
	if pinc.3 = 1 then
	   b0 = b0 + 1
	endif
Look in manual 2 for the various forms of the if statement. I almost always have manual 2 open when I'm writing a program.

Also "b0 + 1" is not correct. It needs to be "b0 = b0 + 1" or "inc b0" .
Even after you correct these syntax problems, I don't think it will do what you want. Could you describe exactly what the program is supposed to do? Is it a learning exercise or will you be trying to control something?

Good luck,
Rick
Thank you for the input Rick.

This is a control program, basically using a switch to activate a program in the picaxe that runs a relay.
I will attempt to break down my program-

Switch A- Switches to GROUND

If switch A is pressed 3 times
Activate flasher program

If switch A is then pressed 2 times
This will deactivate the flasher program

Please see my schematic that I provided on the original post. I havent updated it yet to include others recommendations but the general purpose is there.
Im not fishing for someone to just give me the code, but just give me some clues as to what I am missing.
I am still a very basic programmer, so I try to ask alot of questions along the way.
Thanks again!!
 

Axel87

Senior Member
Did you play with my code in message 2? Maybe not perfect, but did work
ZOR,

I did not attempt your code earlier as stated below I was trying to ask questions along the way to further my understanding. I apologize.
But, I did just plug it into the simulator. And it does do the first step I need.

I see that I need to add another byte variable for the cancellation command correct?
I also need a way to monitor the b1(exit flasher bit) once the flasher program has been started.
Would I use the Lookup command inside the flasher program to monitor b1?

OR am i making this to complex, and could still use b0 to deactivate the flasher program and put into standby?
 

Rick100

Senior Member
Hello Axel87,

Switch A- Switches to GROUND

If switch A is pressed 3 times
Activate flasher program

If switch A is then pressed 2 times
This will deactivate the flasher program
There are several ways to do this. The easiest way may be to use multitasking. One task will moniter the switch and count the presses, setting and clearing a "flash" flag variable. The other task will flash the led when the "flash" flag is set(= 1) and do nothing when the flag is clear(= 0). You could use the "suspend" and "resume" commands to turn off the flasher programs, instead of a flag variable. You also need to debounce the switch. If you don't debounce the switch you counter will increment as long as the switch is closed. One easy way to debounce is with the "button" command. The "button" command needs to be in a loop and will "goto" a label when certain conditions are met.
Here is code for the mutitasking approach:

Code:
#picaxe 08m2
#no_data

symbol btnWrk = b2	'used by button command
symbol flash = b3		'1 = flash on / 0 = flash off

'first task
start0:
	flash = 0	'flasher program off
	b0 = 0		'button pressed counter = 0

main:
	button c.3, 0, 255, 20, btnWrk, 0, btnPushed	'use this line if switch is tied high
	'button c.3, 1, 255, 20, btnWrk, 1, btnPushed		'use this line if switch is tied low
	pause 50	'debounce time
	sertxd(#b0,13,10)		'print out the value of the counter
	goto main
	
btnPushed:
	b0 = b0 + 1		'increment button pressed counter
	if flash = 1 and b0 = 2 then
		flash = 0	'turn off flash
		b0 = 0		'reset button pressed counter
	endif
	if b0 >= 3 then
		flash = 1	'turn on flash
		b0 = 0		'reset button pressed counter
	endif

	goto main
	
'second task	
start1:
Flasher:
	if flash = 1 then 
		High 4	
		pause 1000
		low 4
		pause 1000
	endif
	goto Flasher
	end
You could also use an "open loop" programming structure to write your program. It would use a main loop with counters and flag variables to moniter the switch and flash the led. The above program could probably be modified to use that method without multitasking.

Good luck,
Rick
 

AllyCat

Senior Member
Hi,

Or a "minimalist" approach without multi-tasking (I'll leave it to you to put in the comments! ) : ;)

Code:
#picaxe 08m2
#no_data
pullup 8
begin:
	b0 = 0
	low c.4
mainloop:
	if pinc.3 = bit0 then nobutton
		inc b0
		pause 20	
nobutton:
	if b0 < 6 or b1 = time then noled
		toggle c.4
		b1 = time
noled:
	if b0 > 9 then begin
	goto mainloop
It uses a few "tricks" but they're all in the manual.

Try it in the simulator and come back if you get really stuck.

Cheers, Alan.
 

Axel87

Senior Member
Thanks for the input guys!

Rick- I cant quite yet grasp what is going on in you code example.
I can get c.3 to flash after 3 presses of the button and can follow along the code path somewhat.

button c.3, 1, 255, 20, btnWrk, 1, btnPushed 'use this line if switch is tied low
I believe this is the correct one, as my switch is to ground.
Wouldn't I still need to add the "pullup" command at init??
If I am reading the manual correct -
If the input is active low, at 0V when the button is pressed (e.g. a
10k pull up resistor with switch wired to 0V) then enter 0.
I havent seen "button" which you use "btnWRK" is this the same??

The 255 disables the repeat, 20 states the time variable for on/off correct?
BUT the program never seems to actually go to FLASHER, which doesnt really matter because of button under main, correct?


AlleyCat- I found your code quite ingenious! You seem to count both the on and off of the switch.
I havent used toggle before, this was a good introduction to that.
if b0 < 6 or b1 = time then noled
Where does "Time" come from? I see that "Time" or b1 increases, starting at 15.
What does this tell me or why is this sentence needed?
Also, I need to ask about the pullup 8?
As my switch to ground is on c.3, wouldn't I need to have pullup 4 instead? And of course add the 10k pullup resister, as noted by others earlier in the forum.

I hope my analysis of these examples is at least close.
Like I mentioned before I am just starting on this and trying to understand the steps in the process. In no way am i critiquing your codes, merely trying to grasp the many different paths.

Please get back to me with any further information your willing to provide.
Thank you for your time.
 

Rick100

Senior Member
Hello axel87,
Sorry, I made a mistake in the button line. I tested the code on an axe091 board and the switches have pull down resistors. I modified the line and tested it with a switch with a pullup resistor. I also included the pullup command. Here is the code that should run with c.3 using the pullup command:
Code:
#picaxe 08m2
#no_data

symbol btnWrk = b2	'used by button command
symbol flash = b3 	'1 = flash on / 0 = flash off


'first task
start0:
	flash = 0	'flasher program off
	b0 = 0		'button pressed counter = 0
	pullup %01000  'activate pullup
main:
	button c.3, 0, 255, 20, btnWrk, 1, btnPushed 'use this line if switch is tied high(pullup resistor)
	'button c.3, 1, 255, 20, btnWrk, 1, btnPushed		'use this line if switch is tied low(pulldown resistor)
	pause 50 'debounce time
	sertxd(#b0,13,10) 	'print out the value of the counter
	goto main
	
btnPushed:
	b0 = b0 + 1 	'increment button pressed counter
	if flash = 1 and b0 = 2 then
		flash = 0	'turn off flash
		b0 = 0		'reset button pressed counter
	endif
	if b0 >= 3 then
		flash = 1	'turn on flash
		b0 = 0		'reset button pressed counter
	endif

	goto main
	
'second task	
start1:
Flasher:
	if flash = 1 then 
		High 4	
		pause 1000
		low 4
		pause 1000
	endif
	goto Flasher
	end
As my switch to ground is on c.3, wouldn't I need to have pullup 4 instead? And of course add the 10k pullup resister, as noted by others earlier in the forum.
The reason for "pullup %01000" which is the same as "pullup 8" is binary 01000 or decimal 8 has the 3rd bit set. The bits are numbered 7,6,5,4,3,2,1,0 . The bit values are 128,64,32,16,8,4,2,1 . So if the 3rd bit from the right is set(start counting at 0) then the value is 8.
If your going to add the 10k pullup resistor, you don't need the pullup command.

I havent seen "button" which you use "btnWRK" is this the same??
btnWrk is just a variable used by the button command. It's defined with the line "symbol btnWrk = b2" at the top of the program.

The 255 disables the repeat, 20 states the time variable for on/off correct
The 20 is the autorepeat rate. Since the 255 value in delay disables autorepeat the 20 doesn't do anything. The button command is pretty confusing but it's an easy way to debounce a switch.

BUT the program never seems to actually go to FLASHER, which doesnt really matter because of button under main, correct?
The reason the program never goes to Flasher, is the program is running the Flasher as a separate task. The first task is everything between "start0" and "start1" . That's the task that reads the switch and keeps track of whether or not the flasher should be flashing. The second task is everything between "start1" and "end" . The two programs appear to be running at the same time. The 1st task uses the variable flash to signal to the 2nd task whether to flash or not. The second task reads the flash variable with the line "if flash = 1 then" to run or skip the section of code that flashes the led. Using the labels "start0" and "start1" makes the program multitasking.

Good luck,
Rick
 
Last edited:

Axel87

Senior Member
Thank you for taking the time to help me understand and work through this Rick!
(Jealous of that Axe091 board!! :) )

I believe I am still not grasping the pullup command clearly.
The reason for "pullup %01000" which is the same as "pullup 8" is binary 01000 or decimal 8 has the 3rd bit set. The bits are numbered 7,6,5,4,3,2,1,0 . The bit values are 128,64,32,16,8,4,2,1 . So if the 3rd bit from the right is set(start counting at 0) then the value is 8.
Let say for example, I wanted to pullup pin c.1- would this then be pullup %00000010 or could be written as pullup 1??
I was able to disassemble the board from my enclosure (damn hot glue) and will be able to use the 10K resistor as a pulldown. I made that in italics to bring attention to this question. I need to utilize a pulldown as i am switching to ground vs a pullup if I was switching to v+, correct?

To address this multitasking, "start0" or "start1" are not actually commands correct? How does the picaxe know to run both at the same time, as far as I know everything runs linear; unless a command states otherwise such as "Goto" or "If..Then"

Back to the button command-
button c.3, 0, 255, 20, btnWrk, 1, btnPushed
At the end of this sentance is btnWrk,1,btnPushed. Can you explain this end part for me?
Does this 1 identifiy the Target State?( manual 2 pg 41)
The btnPushed is the address label? similar to a goto command?
I also notice in simulation, after this command finishes and then goes to btnPushed it adds a 1. But only after the first initialization.
This could be a concern, tips as to why this is?

If feel like it would be a good idea to incorperate a sort of time command here.
So that after 2 presses of the button and after so many seconds go by bo clears.
Would I be looking at the enable time command? Recommendations?

Cant emphasis how nice it is to have others offer a hand and help explain this, sometimes gibberish code. Definitely helps to make these projects alot more fun than frustrating!
Thanks again
 
Last edited:

Rick100

Senior Member
Hello axel87,

Let say for example, I wanted to pullup pin c.1- would this then be pullup %00000010 or could be written as pullup 1??
"pullup %00000010" is the correct way to enable the pullup on c.1 . "pullup 1" would equal "pullup %00000001" which would enable the pullup on c.0 since bit 0 is the rightmost bit. The easiest way for me, is to do it in binary and put a 1 in the place I want a pullup.

I need to utilize a pulldown as i am switching to ground vs a pullup if I was switching to v+, correct?
No, just the opposite. If your 10K resistor is connected to ground like the first example in manual 3 on page 26, the switch will go to +V. When the switch is not pressed the pin will have 0V. When the switch is pressed, the pin will have +V.

If your 10K resistor is connected to +V like the second example in manual 3 on page 26, the switch will go to ground. When the switch is not pressed the pin will have +V. When the switch is pressed, the pin will have 0V.

The term pullup means the resistor is pulling the input to the positive voltage. Pulldown means the resistor is pulling the input pin to the ground. The switch connects the pin to the opposite voltage.

To address this multitasking, "start0" or "start1" are not actually commands correct? How does the picaxe know to run both at the same time, as far as I know everything runs linear; unless a command states otherwise such as "Goto" or "If..Then"
When the Picaxe editor sees the labels "start0" and "start1" it makes the program multitasking.
At the end of this sentance is btnWrk,1,btnPushed. Can you explain this end part for me?
Does this 1 identifiy the Target State?( manual 2 pg 41)
The btnPushed is the address label? similar to a goto command?
Yes, on both questions.

Good luck,
Rick
 

AllyCat

Senior Member
Hi,

Time is a system variable in M2 PICaxes. It automatically increments by 1 (from zero) each second. It can avoid needing to "lock up" the flow of the code with a Pause, or having to write complex polling/delay program loops.

My code looks for changes in values, i.e. when time increases by 1 (the LED is toggled) or when the button is pressed or released (so two changes for each button-press). The reason for the latter is that avoids a "lockup" (or multiple counts) if the button is pressed continuously. I know you said the button is only pushed for a short time, but "good" programming tries to be "bomb proof". ;)


Just checking my understanding here. %00000111 is not actually binary but more of a pin placement starting from the left-8 to 0 on the right?
Hypothetically if I wanted to turn pin 7 on it would be %01000000.
Is this correct?
I didn't pick up on this before, but there are a number of "errors" (underlined).

%00000111 is binary (the % says so), it's just one way of presenting a "number", or the value of a byte. It can also be written as decimal 7 or Hex(-adecimal) $07 to do exactly the same thing.

Some of the PICaxe commands (and many other processors) use a "bit mask" to specify which bits (or pins) are to be controlled. One way to consider this is that the rightmost bit-position has a "weight" of 1, the next (to the left) 2, then 4, 8, 16 ... 128. You add the "weights" together for any bit-position which has a value of 1, so %00000111 = 4 + 2 + 1 = 7. The advantage of this is that you can specify any combination of bits/pins by a single value from 0 to 255 (= 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1).

The bits are normally numbered from 7 down to 0 (zero) so to turn on "pin 7" you would actually specify %10000000 (or 128 or $80).

Cheers, Alan.
 

Axel87

Senior Member
Thanks again for the help guys!

I believe I am following along alright now.
I deleted-
'button c.3, 1, 255, 20, btnWrk, 1, btnPushed 'use this line if switch is tied low(pulldown resistor)
pullup %01000 'activate pullup
As I am using a pullup circuit, similar to the second image on pg 26 manual 3.
And I dont need the command if I can add the resistors. Is all this correct?

I am still puzzled as to were the initial value to b0 comes from upon the beginning of the simulation, with no press of the switch?

If I wanted to add a time limit to activate the flash program ( 3 presses of switch)
Would I use COUNT?
One reason I ask, is that this is a mechanical switch. I understand that we used Button to overcome the bounce of the switch.
I cant simply use Count without using a pause or something correct?

Or is there a better way to do this? Or is this getting into the polling commands Alleycat mentioned?
 

Rick100

Senior Member
Hello axel87,

As I am using a pullup circuit, similar to the second image on pg 26 manual 3.
And I dont need the command if I can add the resistors. Is all this correct?
That is correct.

I am still puzzled as to were the initial value to b0 comes from upon the beginning of the simulation, with no press of the switch?
When you run the program in the simulator, use the step command to start the program running. As soon as the simulation starts, click on the c.3 pin to set it to high. This will be the normal state(no button pushed) of the pin that has a pullup resistor. If you leave the pin low the button command will jump to "btnPushed" the first time through. After you get c.3 set to high, you can single step or run the program. To simulate a button push, click on c.3 to make the pin low. If your single stepping , you will have to keep the pin low until the button command executes at least once. Click c.3 again to make it high(not pushed) . This will simulate one button press.

If I wanted to add a time limit to activate the flash program ( 3 presses of switch)
Would I use COUNT?
I don't think count would work very well. It counts the number of low to high transitions within a time 'period'. So if you wanted to count the number of button pushes in five seconds, you would have to wait 5 seconds to get the result. You might try to use the "time" variable like in Alleycats program. Is this time limit to turn off the flashing or turn it on?

Which version of the editor are you using?

Good luck,
Rick
 

Axel87

Senior Member
Thanks Rick,

I just started using PE6 a couple days ago, but I also have 5.5 on my laptop as well.
( I love that you can adjust the simulation speed while in simulation and not have to go through 4 windows)

As soon as the simulation starts, click on the c.3 pin to set it to high. This will be the normal state(no button pushed) of the pin that has a pullup resistor.
Yup, thank you! This was my problem. Lesson learned there lol

The time variable would be to turn on the flasher program.

I am off to work right now but can look more closely at this more when I get home.
Thanks again for the help.
 

Axel87

Senior Member
Back again guys,

Been thinking on this a couple of days and havent come up with much.

The connection I made to the horn switch has 12Vdc on it until the horn is pressed. This is the connection I was going to use for my input on c.3.

My question now, do I need to convert this 12Vdc to 5vdc, even though I am using a pullup circuit?
Do I need another regulator? Or can I use a resistor for a voltage drop?
Thought it best to ask before I connected everything and burn it up :/
 

inglewoodpete

Senior Member
You certainly do require protection for the PICAXE's input. Pin C.3 on a PICAXE 08M2 is the programming pin on the native PIC. As a result, it does not have any over-voltage protection built in. Abetter combination of resistors would be +12v to 10k to 6.8k to 0v. Also, put a 1N4148 or similar between the junction of the two resistors and the PICAXE's Vcc (+ve) supply, with the cathode (banded end) to +ve. A 1N4004 is a bit large for that job but one could be used.

A zener is not suitable. Their characteristics are not exactly the same as the theoretical right-angle response shown in some textbooks.
 

Axel87

Senior Member
Thanks for the input inglewood,

I wired up everything including a switch, but I cant get the program to activate c.4.
Feel like i missing some fundamental key of this pullup/pulldown circuit.

Is everyone clear on what I am aiming to do? switch is tied to gnd on the anode end of the diode and at the junction of the resistors
From what I can diagnose-
With the switch disengaged i read 4.8V at the Anode of the diode.
With the switch engaged, i read 0V.

At the cathode(banded end)
4.3V
3.2V- engaged
Respectively

I feel like I am missing something simple.
Any ideas guys?
 

hippy

Technical Support
Staff member
Is this correct?
Doesn't look like it. I would probably have gone for a circuit similar to below -

Code:
12V --.--              -.------.------ 5V
      |                 |      |
     |_|<| Horn    10K |_|  .--^--.
      |                 |   |     |
      }-----------|<|---^---|     |
      |                     |     |
      o |-.                 `--.--'
      o |-' Button             |
      |                        |
0V ---^------------------------^------ 0V
 
Top