making my code better

hi

my code involves cars entering a beam and then variable b3 count up only once the car exits the beam.
currently i am using two sub routines that have to happen in order to get b3 to count up(in_the_beam and out_of_the_beam).

i was wondering if there is a simpler way of getting this to happen:

1) the beam is broken
2) the beam is unbroken

if both of these things happen get b3 to count up

here is my code, thanks for an help available:cool:


Code:
init:     
    b3=4
    pause 500 ; wait for display to initialise
    serout B.1,N2400,(254,1)
    pause 30

main: 
   serout B.1,N2400,(254,128)   ; move to start of first line
   serout B.1,N2400,("places availible")  ; output text
   irout b.2, 1, 16   ; Send the command 
   'pause 45      ; Pause after each command sent 

   readadc b.3,b1
   debug
   if b1 <50 then goto in_the_beam  'the car needs to be in the beam, but the exit the beam for b2 to count down, 
   'otherwise a car could sit in the beam for too long and cause multiple spaces to be displayed as taken.
   'if b1 >50 then 
   
   serout b.1, n2400, ( 254, 192 ) 'second line
   'pause 500
   serout b.1, N2400, (#b3)
   
   
   
   
   
   goto main
 
in_the_beam:
   readadc b.3,b1
   pause 500
   if b1 >50 then goto out_of_the_beam
   goto in_the_beam
 

out_of_the_beam:
   
   let b3 = b3 -1 ;the car is now out of the beam so display one less space as available (count up from 4)
   if b1 < 50 then goto in_the_beam
   if b3=0 then goto full  'stop b1 from counting up (maximum spaces taken up)

goto main


full:
  let b3=4 'stop at 0
  serout B.1,N2400,(254,1)
  serout b.1, n2400, ( 254, 128 ) 'second line
  serout b.1, n2400, ( "park is full " )

[code]
 
Last edited:

techElder

Well-known member
titaniumnitratecoatedcow, you should really start studying how to use the compiler SYMBOL command in your code. It really makes it easier to decipher what your variables are for.

On your other thread of posts, I suggested a better way of counting large objects. If you just use one IR pair, your count will always be susceptible to erroneous counts.

If this project is only for educational purposes, then I suppose you can get away with what you are doing.
 

westaust55

Moderator
As yet no significant improvements but beside the use of symbols as mentioend by Tex
some tidy up to comments ("count up" versus "count down", and "stop b1" versus "Stop b3" from further counting.
Some indenting of the code so that the program flow and sub sections are clearly visible will help with reading.
also what do you want to happen at the end when the park is full? without some program commands the PICAXE will jump tot he start again at the Init: label.
Do you intend to allow spaces to become available as cars leave ?
A slightly "tweaked" version of your program here:
Code:
init: 
   b3=4
   pause 500   ; wait for display to initialise
   serout B.1,N2400,(254,1)
   pause 30

main: 
  serout B.1,N2400,(254,128)   ; move to start of first line
  serout B.1,N2400,("places availible")   ; output text
  irout b.2, 1, 16   ; Send the command 
  'pause 45   ; Pause after each command sent 

   readadc b.3,b1
  debug
   if b1 <50 then goto in_the_beam   'the car needs to be in the beam, but the exit the beam for b2 to count down, 
   'otherwise a car could sit in the beam for too long and cause multiple spaces to be displayed as taken.
   'if b1 >50 then 

   serout b.1, n2400, ( 254, 192 ) 'second line
   'pause 500
   serout b.1, N2400, (#b3)
   goto main

in_the_beam:
   readadc b.3,b1
   pause 500
   if b1 <50 then in_the_beam  ; loop while the beam is still broken

out_of_the_beam:
   let b3 = b3 -1 ;the car is now out of the beam so display one less space as available (count [b]down[/b] from 4)
  ;  if b1 < 50 then goto in_the_beam  [b]; - not really necessary another car is not likely to bream beam in less than 0.25 msec[/b]
   if b3=0 then goto full   'stop [b]b3[/b] from further counting down (maximum spaces taken up)
   goto main

full:
   let b3=4   'stop at 0
   serout B.1,N2400,(254,1)
   serout b.1, n2400, ( 254, 128 ) 'second line
   serout b.1, n2400, ( "park is full " )
; where should program go next ????  with no proper program ending/loop, it will return to the start at the label [b]init:[/b]
 
if this addresses your concerns

i have now put symbols in my code , symbol IR_receiver= b1, etc.

looking back on my sub system diagram i am actually using 3 IR beams and an infra red remote.

At the entrance of the car park, a remote is pressed and the entrance barrier arm opens.
The car passes under the barrier arm
The car then passes through an infra red beam on the other side of the barrier arm which counts down the spaces availible and tells the barrier arm to Close.

Once the car wants to exit, it drives up to another infra red beam in front of the exit barrier arm.
Once the car is in the beam, the servo opens, and one more space is made available on the LSD screen.
Once the car has cleared the exit barrier arm, it drives through another IR beam, which can only CLOSE the barrier arm.

The car the drives then drives away

my subsystem diagram is a google slides, I'm not sure how i can send you a link without allowing everyone who views this post to edit it :confused:
 
As yet no significant improvements but beside the use of symbols as mentioend by Tex
some tidy up to comments ("count up" versus "count down", and "stop b1" versus "Stop b3" from further counting.
Some indenting of the code so that the program flow and sub sections are clearly visible will help with reading.
also what do you want to happen at the end when the park is full? without some program commands the PICAXE will jump tot he start again at the Init: label.
Do you intend to allow spaces to become available as cars leave ?
A slightly "tweaked" version of your program here:
Code:
init: 
   b3=4
   pause 500   ; wait for display to initialise
   serout B.1,N2400,(254,1)
   pause 30

main: 
  serout B.1,N2400,(254,128)   ; move to start of first line
  serout B.1,N2400,("places availible")   ; output text
  irout b.2, 1, 16   ; Send the command 
  'pause 45   ; Pause after each command sent 

   readadc b.3,b1
  debug
   if b1 <50 then goto in_the_beam   'the car needs to be in the beam, but the exit the beam for b2 to count down, 
   'otherwise a car could sit in the beam for too long and cause multiple spaces to be displayed as taken.
   'if b1 >50 then 

   serout b.1, n2400, ( 254, 192 ) 'second line
   'pause 500
   serout b.1, N2400, (#b3)
   goto main

in_the_beam:
   readadc b.3,b1
   pause 500
   if b1 <50 then in_the_beam  ; loop while the beam is still broken

out_of_the_beam:
   let b3 = b3 -1 ;the car is now out of the beam so display one less space as available (count [b]down[/b] from 4)
  ;  if b1 < 50 then goto in_the_beam  [b]; - not really necessary another car is not likely to bream beam in less than 0.25 msec[/b]
   if b3=0 then goto full   'stop [b]b3[/b] from further counting down (maximum spaces taken up)
   goto main

full:
   let b3=4   'stop at 0
   serout B.1,N2400,(254,1)
   serout b.1, n2400, ( 254, 128 ) 'second line
   serout b.1, n2400, ( "park is full " )
; where should program go next ????  with no proper program ending/loop, it will return to the start at the label [b]init:[/b]
there will be another beam wich counts back up the places availible

b1 is the the variable for that particular ir beam, b3 is the counting variable inputted by both entrance and exit beams. (exit beam coming soon)
 
Last edited:

BESQUEUT

Senior Member
At the entrance of the car park, a remote is pressed and the entrance barrier arm opens.
The car passes under the barrier arm
The car then passes through an infra red beam on the other side of the barrier arm which counts down the spaces availible and tells the barrier arm to Close.

Once the car wants to exit, it drives up to another infra red beam in front of the exit barrier arm.
Once the car is in the beam, the servo opens, and one more space is made available on the LSD screen.
Once the car has cleared the exit barrier arm, it drives through another IR beam, which can only CLOSE the barrier arm.

The car the drives then drives away:
Is there only one car in your car park ?
If more than one, a car can be present at the exit beam while another is at the entrance...

So, you can't stuck code flow with a loop like "loop while the beam is still broken" ...
So you'll have to deal with multi tasking or collaborative multi-tasking...

How many beams and where are there ? Little schema please...

To my mind, a code is more readable if GOTO command is avoided...
 

rossko57

Senior Member
If more than one, a car can be present at the exit beam while another is at the entrance...
So, you can't stuck code flow with a loop like "loop while the beam is still broken" ...
This is a very good point. You might think about structuring the code to respond to beam EVENTS (changes) rather than steady-states. E.g. when this beam is broken, do that ; when this beam is cleared, do this. Which this or that to do will be dependent on what happened before, so you'll need to remeber at which point in the sequences you are. (Essentially you have two unrelated sequences, one at the entrance barrier and one at the exit)
This begins to feel like a programming model called "state machine".

There will be unexpected/illegal combinations that you'll need to decide what to do about - ignore, crash, flash a light - such as a beam after barrier getting broken without the barrier being opened.
When the basics are working, you could go on to add timing - if a beam is blocked for hours, something is wrong.
 

BESQUEUT

Senior Member
State machine for entrance only...
With only one beam :
Code:
	Beam 1   	Beam 1		
State	Last State	Current   	State         	What to do ?
1	unbroken	unbroken	no car         	nothing
2	unbroken	BROKEN  	Car at door         nothing
3	BROKEN  	unbroken	Car entering Park	+1
4	BROKEN  	BROKEN  	till here	    nothing
With 2 beams :
Code:
	Beam 1   	Beam 1   	Beam 2   	Beam 2	
State	Last State	Current  	Last State	Current   	What happen ?       What to do ?
1	unbroken	unbroken	unbroken	unbroken  	no car
2	unbroken	BROKEN  	unbroken	unbroken  	Car at door
3	BROKEN  	unbroken	unbroken	unbroken	car between 1 and 2
4	unbroken	unbroken	unbroken	BROKEN  	Car leaving
5	unbroken	unbroken	BROKEN  	unbroken	Car in park           +1
					
6	BROKEN  	BROKEN  	unbroken	unbroken	Car still entering
7	BROKEN  	unbroken	BROKEN  	unbroken	Very long car  last time ?
8	BROKEN  	unbroken	unbroken	BROKEN  	
9	unbroken	BROKEN  	BROKEN  	unbroken	
10	unbroken	BROKEN  	unbroken	BROKEN  	
11	unbroken	unbroken	BROKEN  	BROKEN  	
12	BROKEN  	BROKEN  	BROKEN  	unbroken	
13	BROKEN  	BROKEN  	unbroken	BROKEN  	
14	unbroken	BROKEN  	BROKEN  	BROKEN  	
15	BROKEN  	unbroken	BROKEN  	BROKEN  	
16	BROKEN  	BROKEN  	BROKEN  	BROKEN
State machine is supposed to react only if beam state change.
 
Last edited:

BESQUEUT

Senior Member
Test program for simulation. Change values from 58 to 48 for b.2 or b.4 to increment or decrement car count.
Code:
' Car Park

Symbol BROKEN=1
Symbol UnBroken=0

Symbol Beam1=10	'b.1
Symbol Beam2=8	'b.2
Symbol Beam3=9	'b.3
Symbol Beam4=11	'b.4


Symbol Cur1=bit0
Symbol Cur2=bit1
Symbol Cur3=bit2
Symbol Cur4=bit3
Symbol Last1=bit8
Symbol Last2=bit9
Symbol Last3=bit10
Symbol Last4=bit11

symbol CurrentState=b0
symbol LastState=b1
symbol PrevState=b2

Symbol Temporary=b3

Symbol CarCount=b4


do
	readadc Beam1,Temporary
   	if Temporary <50 then
		cur1=BROKEN
	else
		cur1=UnBroken
	endif

	readadc Beam2,Temporary
   	if Temporary <50 then
		Cur2=BROKEN
	else
		Cur2=UnBroken
	endif

	readadc Beam3,Temporary
   	if Temporary <50 then
		Cur3=BROKEN
	else
		Cur3=UnBroken
	endif

	readadc Beam4,Temporary
   	if Temporary <50 then
		Cur4=BROKEN
	else
		Cur4=UnBroken
	endif

	if CurrentState<>PrevState then
		lastState=PrevState
		if cur2=UnBroken and last2=BROKEN	then
			inc CarCount
		endif
			
		if cur4=UnBroken and last4=BROKEN	then
			dec CarCount
		endif
		PrevState=CurrentState
	endif

loop
I do not know the rules to deal with 2 beams for entrance...
so I implemented only one rule :
- conter is incremented/decremented only if the second beam state change from BROKEN to UnBroken.

Note that with correct usage of symbols, the need for comments is reducted...
Also note that you always can write code without GOTO command.
 
Last edited:
Top