first program assistance,, please?

Randy5140

Member
I am trying to write my first program for a picaxe 40x2

I have 1 input button switch (momentary) connected to Pin C.1

I have 10 LED's connected to Pins #1 = D.6, #2 = D.7, #3 = B.0, #4 = B.1 , #5 = B.2, # 6 = B.3 , #7 = B.4 , #8 = B.5 , # 9 = B.6 and # 10 or (0) = B.7

I need the program to count how many times the button is pushed within a certain length of time (I have been using 5 seconds) and then turn on the appropriate
led ( the one that numerically corrresponds to the count) .

Like, if the button it pushed 3 times within the time interval, then led #3 turns on (pinB.0 goes high)

not haveing too much luck so far,,, :)

Below is the code I have written using the "Count" example in the manual as a base to work from.

I someone could point me in the right direction I would really appreciate it!



Thanks!
Randy

Code:
#picaxe 40x2



main:

count C.1, 5000, w1 ; count pulses in 5secs

debug ; display value

if w1 = 1 then
high D.6
 
debug

elseif w1 = 2 then
high D.7

debug

elseif w1 = 3 then
high B.0

debug

elseif w1 = 4 then
high B.1

debug

elseif w1 = 5 then
high B.2

debug

elseif w1 = 6 then
high B.3

debug

elseif w1 = 7 then 
high B.4

debug

elseif w1 = 8 then
high B.5


elseif w1 = 9 then

high B.6

debug


elseif w1 = 10 then

high B.7

debug

end if


wait 2

'turns off the leds so button can be used again
low D.6
low D.7
low B.0 
low B.1
low B.2
low B.3
low B.4
low B.5
low B.6
low B.7

goto main
 
Last edited by a moderator:

premelec

Senior Member
Hi - is your pushbutton going from low to high when pushed? - are there pull down or up resistors involved? - are you getting anything on the input variable?
 

Randy5140

Member
Hi - is your pushbutton going from low to high when pushed? - are there pull down or up resistors involved? - are you getting anything on the input variable?
yes, the button is going high,, on the simulation the blue square next to pin C1. turns red each time the button is pushed. the switch is set up with resistors like in the manual

Randy
 

Buzby

Senior Member
From his previous posts I think the OP is using VSM.

I've never used VSM. Does it let you connect real hardware, like 'Simulate->Connect' in PE should ?
 

Puuhaaja

Senior Member
Try to put only one debug to command to whole code and check what happens.

and:

Code:
main:
let w1 = 0

count C.1, 5000, w1 ; count pulses in 5secs

debug ; display value
let w1 = 0 for the reason that I'm not sure that does count command automatically set w1 to zero when it starts to count.
 
Last edited:

SAborn

Senior Member
I would susspect using count might not be the best option as in this case it might be detecting switch bounce, where the switch contacts can chatter a few times on closing and opening and record several switch pushes for a single push.

You might be better to use a timer for the period and increase a counter for each switch press, this way a small pause (20ms) can be added after each switch press to allow for switch bounce.
 

Randy5140

Member
From his previous posts I think the OP is using VSM.

I've never used VSM. Does it let you connect real hardware, like 'Simulate->Connect' in PE should ?


Actually I just got VSM this week


I have the circuilt built, but I also have and use VSM when I am unable or away and have a few minutes to work on stuff.

Is there a problem using VSM first to work out bugs before spending time and money building a circuit?
 

Randy5140

Member
Try to put only one debug to command to whole code and check what happens.

and:

Code:
main:
let w1 = 0

count C.1, 5000, w1 ; count pulses in 5secs

debug ; display value
let w1 = 0 for the reason that I'm not sure that does count command automatically set w1 to zero when it starts to count.
Puuhaaja,

I commented out all the "debug" (s) except one at the bottom of the code.

that made it work to an extent,

I thought I might need some way to zero out the w1 but wasnt sure how to do it untill your suggestion.

I added the "let w1 = 0 just below "main" and it works fine now.

Thanks so much for the suggestions,,, and fixes!

Thanks to all for everything!
 

Puuhaaja

Senior Member
Nice to know that. Sometimes I have checked debug screen and notified that only one button pressing can cause even 40 registered button pressing in microcontroller. That's because of debounce effect and it can be eliminated by using pause 100 etc..command as mentioned earlier. You can check registered button presses(w1) if your program show you the debug screen. Remember that debug command use much processor power so it slows down your program.
 

Randy5140

Member
Puuhaaja,

Yes,, I really notice that the program ran slow,, although being new to this I wasnt sure if the speed I was seeing wasnt just normal.

when I removed the extra debug (s) it did run thru faster.

now that I have this first program working,

I am going to try something different using the same board I built for this one.

going to try and have the LED's increment with each button push. (one push = Led #1,,,, next push = Led #2 etc etc )

I am guessing that a " FOR NEXT loop" is what I want for that,,

Correct?

Thanks again!
 

Puuhaaja

Senior Member
I think that for next loop isn't that. if you press button 5 times and after that want to led flash 5 times -> use for next loop.

Now you can use variables.

I would start testing code this way:

let b0 = 0

main:
if c.0 = 1 then inc b0
pause 100 ;because of debounce effectincrement

if b0 = 1 then high b.1 ; better way is to use let dirsB = %00000010
low b.2 etc etc.

if b0 = 2 then let dirsB = %00000100

goto main
Hope you get the point!
 

Randy5140

Member
QUOTE=Puuhaaja;230925]I think that for next loop isn't that. if you press button 5 times and after that want to led flash 5 times -> use for next loop.

Now you can use variables.

I would start testing code this way:



Hope you get the point![/QUOTE]



Puuhaaja,

think I do,,

but what I am trying to do is simply advance to the next led with each button push,, no flashing.

like: push button once---- Call variable---- (fist led comes on). -------(D.6 high)

push button again Call next variable ---- (first Led goes off, second Led comes on) -----(D.6 low, D.7 high)

etc

etc

continues to advance across Leds with each button push until it reaches Led 10 then starts over with Led 1

I am thinking,, ( well,, going back to years ago when I did some VB programing) that I would need to declare outputs d.6, d.7, b.0, b.1, b.2, b.3, b.4, b.5, b.6, b.7 as a variable, (not really sure how to do that)

then call the "next" output variable "high" and the previous "low" with each button push.

never was to good at thinking the abstract of this.. thats why I normally stuck to " If,,,, Then" in my code,, :)


Question: Trying to stop the debounce on the switch, would it be better to isolate the switch with an Optoisolator (between the switch and the picaxe) than to simply use the resistors and a "wait" or "Pause" time out? Also think I read somewhere about putting a Cap on the pin to stop the debounce

The "wait" or "Pause" seems to interupt the button input count. If the button is pushed during the "wait" is doesnt seem to register in the count.


Please note that I am not trying to get you or someone here to write code for me,, just trying to learn how this all works so I can go ahead with the project that got this all started for me to begin with! :)


any thoughts?



Thanks
 

Puuhaaja

Senior Member
@ Randy 5140

I understood what you are trying to do. I tried to explain why for next loop can't be used but that's only my opinion so don't believe it as a truth. There are many ways to do code where result is same. If you use pause 10 command it means that it's only 1/100 of second so there no person who could press button 100 times in second.
 

SAborn

Senior Member
On average a 20ms pause is enough to remove switch bounce, the picaxe detects the first switch closure than pauses for 20ms, this allows all the contact chatter time to dissapear, you dont need caps and extra resistors etc, like we did in the old days of fixed hardware, because we can simply add a delay to our program.

I think Puuhaaja, gave about the simplest method in his example of code above, and for some strange reason you want to over complicate it with declaring outputs as variables etc.

Have a read in the manual2 about "Let Pins =" command it should work very simple for your needs.
 

westaust55

Moderator
With respect to the post 1 requirment try this code and see how it works.
Code:
#picaxe 40x2

; define the Outputs corresponding to LEDs
SYMBOL LED1 = D.6
SYMBOL LED2 = D.7
SYMBOL LED3 = B.0
SYMBOL LED4 = B.1
SYMBOL LED5 = B.2
SYMBOL LED6 = B.3
SYMBOL LED7 = B.4
SYMBOL LED8 = B.5
SYMBOL LED9 = B.6
SYMBOL LED10= B.7

SYMBOL Presses = b5
SYMBOL NewLED  = b6

Init:
	DirsB = %11111111 ; set portB pins to outputs ; so the pinsB command will work first time in the ClearAll routine
	GOSUB ClearAll



Main:

	COUNT C.1, 5000, Presses; count pulses in 5 secs

	LOOKUP Presses, (0, LED1, LED2, LED3, LED4, LED5, LED6, LEd7, LED8, LED9, LED10), newLED

	GOSUB ClearAll
	
	IF Presses > 0 THEN
		HIGH newLED
	ENDIF
	
	WAIT 2
	
	GOSUB ClearAll
	
	GOTO Main



ClearAll:
	LOW D.6
	LOW D.7
	pinsB = 0
RETURN
then it should not be hard to modify for the later requirement in post 11
 
Top