Simulating Buttons

LED Maestro

Senior Member
Hello forum,
I am having problems with the picaxe simulator. Namely trying to simulate a momentary push button. As I am new to picaxe, I haven't got to grips with everything the sim' has to offer. I was wondering if it was possible to, for example, press C.1 in the sim' for it to go high and activate B1 and then C.1 to go low without me having to press it but keep B1 on. It's all part of a project I am working on but can't seem to write the code very well. Any input would be greatly appreciated.
Thanks,
 

eclectic

Moderator
Simulation yes

Code:
#picaxe 18M2
main:

if pinC.1 = 1 then

high B.1 
low C.1
endif

goto main
In real life, you'll need to switch off

e
 

westaust55

Moderator
You may need to be careful when implementing that in the real world.
Let's say you have a switch from +5 Volts to the PICAXE input C.1 and a pull-down resistor from 0 Volts to the same input.
Then you operate the switch to make the input high (=1) and then the PICAXE executes the command LOW C.1
The result is the pin C.1 becomes an output driven low but connected to +5 Volts
Giving a short through the PICAXE pin C.1 = smoke released and PICAXE faulty.

It may be a good idea to post your proposed circuit and a clear step by step description of what you are trying to achieve (and why. Trying to force the input low) so folks can better understand and help.
 

LED Maestro

Senior Member
Ah...yes that is a good point. Thanks. I am trying to sequence a set of 16 led's. I press one of two push buttons and the first led comes on, press again and the next comes on whilst the 1st is still light and so on. Then the second button would be used to do the same operation in reverse, so cycle down through the leds. there is a vid on you tube of what I am trying to achieve.
http://www.youtube.com/watch?v=BLYiVdC7Iv4
I have tried to write code to do this but it just doesn't work quite right.
Thanks again for the warning,
 

eclectic

Moderator
@LED M.

Just post the code and say what exactly is happening.

Also say whether it's in simulation or for real.

Just one important question first:
Is it for coursework or assessment?

e
 

LED Maestro

Senior Member
Hi e,
Here is the code I have come up with. It was the only way I could find to use the same input to select the outputs. My only concern is that I have to press C.0 at the same time it is highlighted in the sim for each output. I don't have a chip as yet. I only ordered it a coulpe of days ago and with it being Christmas, it won't get here so I am confined to testing in the sim. As to what it is for, well it's personal curiosity really. I study Engineering at college and do electronics 2 days a week. I asked my tutor about my project and he suggested a picaxe and now here I am, bothering you with my problems which I am sure are a simple task.
Thanks againg for all you help thus far,

Code:
main:	pause 2000
pause 500 if pinC.0 = 1 then LED1on
pause 500 if pinc.0 = 1 then LED2on
pause 500 if pinc.0 = 1 then LED3on
pause 500 if pinC.0 = 1 then LED4on
pause 500 if pinC.0 = 1 then LED5on
pause 500 if pinc.0 = 1 then LED6on
pause 500 if pinC.1 = 1 then LED6off
pause 500 if pinC.1 = 1 then LED5off
pause 500 if pinC.1 = 1 then LED4off
pause 500 if pinC.1 = 1 then LED3off
pause 500 if pinC.1 = 1 then LED2off
pause 500 if pinC.1 = 1 then LED1off
	goto main

LED1on:	pause 100 
	high B.1
	goto main
	
LED2on: pause 100
	high B.2
	goto main
	
LED3on: pause 100
	high B.3
	goto main 
	
LED4on: pause 100
	high B.4
	goto main 
	
LED5on: pause 100
	high B.5
	goto main 
	
LED6on: pause 100
	high B.6
	goto main 
	
LED6off: pause 100
	low B.6
	goto main 
	
LED5off: pause 100
	low B.5
	goto main 
	
LED4off: pause 100
	low B.4
	goto main 
	
LED3off: pause 100
	low B.3
	goto main 
	
LED2off: pause 100
	low B.2
	goto main 
	
LED1off: pause 100
	low B.1
	goto main
 

MPep

Senior Member
Your code is written in such a way that whenever C.0 is pressed, the program goes to LED1on, then goes to Main and the cycle repeats.
I suggest that instead of GOTO command, you use GOSUB, together with RETURN.
This way if C.0 is till pressed, then after the sub-routine, your code will RETURN to where it jumped from, and then check for LED2on etc. If C.0 is still pressed, carry on with the next sub-routine.

Hope this makes sense to you.
 
Last edited:

Goeytex

Senior Member
Your code can be simplified to something like this.


Code:
Symbol LED = B2

main:    pause 2000

do
        for LED = 1 to 6
             pause 500
             if pinC.0 = 1 then high LED
            else if pinc.1 = 1 then low LED
            endif
       next
loop
 
Last edited:

LED Maestro

Senior Member
Thank you both, MPep and Goeytex. Both your suggestions and much needed and appreciated. Thanks for the code Goey'. It works wonders. Can't wait to put it into real life practice. Thank you MPep, also, for the GOSUB and RETURN advice. Works much better than GOTO and MAIN.
Thanks again,
 

MPep

Senior Member
That's my XMas present to you. :)
Try rewriting your code with GOSUBs etc. Just as an exercise and see how you original code would have worked, and how the GOSUB code would work.
Can you see what I mean about a constant loop being created in your code in post #7?

Goeytex's code is a GREAT example of an elegant program that fits the bill just right. Thanks mate.
 

LED Maestro

Senior Member
Ha ha! Thanks ;)
I will have a go at rewriting and see what I come up with. Yes I can see what you mean in Post #7. Great advice for a beginner like myself.
Merry Christmas and a happy new year
 

LED Maestro

Senior Member
Ah that's fantastic. You are right. It is basically a 16 LED bar graph using 2 push buttons to cycle up and down, at will, through the LEDs. Thanks for the link. I will study it carefully and have another go.
Thanks,
 
Top