Help with code please: Flash circuit

kardzzz

New Member
Hi All,
my tac switch is on pin 4
LED is on pin 0

I want the LED to blink the number of times i've pressed the switch looping 0-3 cycles.
i.e. first press = 1 blink
2nd press = 2 blinks
3rd press = 3 blinks
4th press = 0 blinks
5th press = 1 blink
looping

my blink counter counts in 2 cycles as i am using a for-next loop. how do i correct my code?
thanks


symbol counter = b1
symbol blink = b2
symbol LED = 0



b1 = 0 'initialise b1 to 0



main:
if pin4 =1 then gosub flashcount
goto main




flashcount:
b1 = b1+1 'increase counter
for blink = 0 to b1 'set number of blinks for mode selected
high LED
pause 500
low LED
next blink


if b1 = 3 then gosub b1reset
goto main

b1reset: 'reset counter to 0 from 3
b1 = -1
return
 

donrecardo

Senior Member
Hi
I too am new to picaxe but will have a go at answering your question , but I dont promise it will be correct

In your code you say for
for blink = 0 to b1

now lets say its the first time you pressed the switch so b1 = 1
your loop will flash the led for 0 to 1 so it flashes twice not once

make the loop from 1 to b1

because of this here you wrote

if b1 = 3 then gosub b1reset

you need to make that if b1 = 4

The biggest problem I found was in Main: you gosub flashcount but flashcount doesnt have a return so will overflow and crash
change it from gosub to goto flashcount

lastly in

b1reset: 'reset counter to 0 from 3
b1 = -1
return

change b1 = -1 to b1 = 0

If you then try it in simulator it works ok

Don
 

donrecardo

Senior Member
Hi Kardzzz

Carrying on from my last post I found another snag

If you dont release the switch quickly enough the
code will cycle through endlessly so holding the switch down it counts 1,2,3,4,1,2,3 etc untill you release the key

so after you check to see if b1 = 4 ( max required count)
you could add a line like this

if b1 = 4 then gosub b1reset ' your original code
check: if pin4 = 1 then check ' new line
goto main ' your original code

what this does is halts the execution of the rest of the code untill the switch is released

Hope that helped

Don
 

donrecardo

Senior Member
Hi Kardzzz

My mistake for reading too quickly
I hadnt noticed the no blink at count 4

By checking for b1 = 4 before flashing the leds lets you jump out and miss the flash , like this
Code:
symbol counter = b1
symbol blink = b2
symbol LED = 0



b1 = 0 'initialise b1 to 0



main:
if pin4 =1 then goto flashcount
goto main




flashcount:
b1 = b1+1 'increase counter
if b1 = 4 then check1        ' miss out the led flash
for blink = 1 to b1 'set number of blinks for mode selected
high LED
pause 500
low LED
next blink

check1:
if b1 = 4 then gosub b1reset    ' checks to see max count reached
check2: if pin4 = 1 then check2      ' checks switch has released
goto main

b1reset: 
b1 = 0

return
Hope thats right now

Regards
Don
 

de0508

New Member
Hello,

this will be my solution:

107 Byte PIXACE-18x

See program code and documentation, some word are in german, i hope you can read it.

regards, uwe


Code:
' ------------------------------------------
' ------------------------------------------
' (c) DE0508
' ------------------------------------------
' Version 1.0
'
' 2009-01-19 start code
' ------------------------------------------

'---------------------------------------
' system settings
'---------------------------------------
#picaxe 18x
#gosubs 256
'setfreq m8

'---------------------------------------
' Define
'---------------------------------------

'---------------------------------------
' Konstanten
'---------------------------------------
symbol TRUE=1
symbol FALSE=0

symbol InputPush=0				' Taster sind low aktiv
symbol InputNotPush=1			' Taster ist nicht gedrückt

'---------------------------------------
' Pinbelegung PICAXE-18X
'---------------------------------------
' Input 2/ADC2			: 
' Serial Out			: *RS-2332 In
' Serial In				: *RS-2332 Out
' Reset					: *Pull-Up +5V
' 0V					: *GND
' Output 0				: LED_0
' Output 1/i2c data		: 
' Output 2				: 
' Output 3/ pwm 3		: 
' Output 4/i2c scl		: 
' Output 5				: 
' Output 6				: 
' Output 7				: 
' Vdd					: *+5V
' Input 6/keyb clock	: 
' Input 7/keyb data		: 
' Input 0/ADC0/Infrain	: INPUT_0
' Input 1/ADC1			: 
'---------------------------------------

'---------------------------------------
' Ausgänge festlegen
'---------------------------------------
symbol LED_0 = 0


'---------------------------------------
' Eingänge festlegen
'---------------------------------------
symbol INPUT_0 = Pin0


'---------------------------------------
' Variable Belegung
'---------------------------------------
' w0 := b1:b0
symbol i 		= b0			' Arbeitsregister
symbol j 		= b1			' Arbeitsregister

symbol bButton	= b1

'---------------------------------------
' w1 := b3:b2
symbol iLED 	= b2
'---------------------------------------
' w2 := b5:b4
'---------------------------------------
' w3 := b7:b6
'---------------------------------------
' w4 := b9:b8
'---------------------------------------
' w5 := b11:b10
'---------------------------------------
' w6 := b13:b12
symbol iBlinkCnt 	= b12
symbol iMode_Select = b13
'---------------------------------------

'---------------------------------------
' Variables - Storage PICAXE-18X
'---------------------------------------
' 80 to 127 ($50 to $7F)
' 192 to 239 ($C0 to $EF)
'---------------------------------------

'---------------------------------------
' EEPROM PICAXE-18X
' 0 to 255
'---------------------------------------
symbol PROG_VERSION = 0
EEPROM PROG_VERSION, ("1.0",0)		' wichtig '0' am Ende !

'---------------------------------------
' Symbole
'---------------------------------------
symbol WAIT_PUSH_TIME  = 100
symbol WAIT_LOOSE_TIME = 50

'---------------------------------------
' Init
'---------------------------------------
init_var:
; clear vars
	iMode_Select = 0

init_output:
	outpins	= %00000000				' alle Ausgänge auf low

main:

	gosub check_button0
	if bButton = TRUE then

		' we count from 0, 1, 2, 3, 4=0, ...
		BRANCH iMode_Select, (prog_1, prog_2, prog_3, prog_4 )
		
main_0:
		inc iMode_Select
		if iMode_Select > 3 then
			iMode_Select = 0
		endif
	endif
	
	goto main
end



prog_1:
	iBlinkCnt = 1
	goto blink_loop:

prog_2:
	iBlinkCnt = 2
	goto blink_loop:

prog_3:
	iBlinkCnt = 3

blink_loop:
	iLED = LED_0
	for i = 0 to iBlinkCnt 
		gosub Blink_LED
	next i

goto main_0

prog_4:
	' NOP
goto main_0


'---------------------------------------
' Blink_LED:
'---------------------------------------
Blink_LED:
' get   : iBlinkCnt, iLED
' change: j
' return: -

	for j = 1 to iBlinkCnt
		HIGH iLED
		pause 500					' 500 ms
		LOW iLED
	next j
	
return

'---------------------------------------
' TASTER abfragen
'---------------------------------------
check_button0:
' change: bButton bool
' return: bButton bool

	bButton = FALSE

	check_b0:
	' Taster gedrückt?
	if INPUT_0 = InputPush then
		pause WAIT_PUSH_TIME
		if Pin0 = InputNotPush then
		' Taster nicht mehr gedrückt?
			goto check_b0
		endif
		
		do								
			' warte bis die Taste wieder losgelassen wird
			pause WAIT_LOOSE_TIME	
		loop until Pin1 = InputNotPush

		' Zustand wechseln
		bButton = TRUE
	endif
return
 
Top