Old dog, new tricks.

Ruzzz

Member
Hello all.

I'm returning to microelectronics from a long absence, being more involved in systems of electronics than the nuts and bolts, so time to knock the rust off. I have a few projects that will be using PICAXE devices, so more questions will be following. :D

I've written a program for a simple controler and it works, BUT the way I would have coded it is shown in the comments.
Is there any way that it could be simplified so that it is closer to the way I would have liked to have coded it ?

Code:
'terminator program
' input sensors		l_arm = input 0
'			r_arm = input 1
'
' Outputs 		l_arm_led 	= output 0
' 			r_arm_led 	= output 1
'			Buzzer		= output 7
'			l_arm_laser	= output 4
'			r_arm_laser 	= output 5			

Symbol l_arm_led 	= 0 
Symbol r_arm_led 	= 1 
Symbol l_arm_laser 	= 4
Symbol r_arm_laser 	= 5
Symbol buzzer 		= 7
Symbol l_arm = b0
Symbol r_arm = b1

main:

l_arm = pin0
r_arm = pin1

if r_arm = 1 then low r_arm_laser 		' }
else						' } r_arm_laser = NOT r_arm
high r_arm_laser				' }
endif						' }

if l_arm = 1 then low l_arm_laser 		' }
else						' } l_arm_laser = NOT r_arm
high l_arm_laser				' }
endif						' }

low l_arm_led
low r_arm_led
if r_arm = 0 then low buzzer 			' }
else						' } buzzer = NOT r_arm
high buzzer					' }
endif						' }

gosub delay

if r_arm = 1 then low r_arm_led 		' }
else						' } r_arm_led = NOT r_arm
high r_arm_led					' }
endif						' }

if l_arm = 1 then low l_arm_led 		' }
else						' } l_arm_led = NOT r_arm
high l_arm_led					' }
endif						' }

if l_arm = 0 then low buzzer 			' }
else						' } buzzer = NOT l_arm
high buzzer					' }
endif						' }

gosub delay
goto main

delay:

pause 350
Thanks in advance
 

hippy

Ex-Staff (retired)
By defining your outputs using "outpinX" you can use ...

Symbol r_arm_laser = outpin5

r_arm_laser = Not r_arm
 

Ruzzz

Member
its:
PICAXE programming editor version 5.2.2
Running on an Asus EEE 900 ( was linux now XP )
for a PICAXE 18x

Code:
'terminator program picaxe 18x
' input sensors 	l_arm = input 0
'			r_arm = input 1
'
' Outputs 		l_arm_led 	= output 0
' 			r_arm_led 	= output 1
'			Buzzer		= output 7
'			l_arm_laser	= output 4
'			r_arm_laser 	= output 5			

Symbol l_arm_led 		= outpin0 
Symbol r_arm_led 		= outpin1
Symbol l_arm_laser 		= outpin4
Symbol r_arm_laser 		= outpin5
Symbol buzzer 			= outpin7
Symbol l_arm = b0
Symbol r_arm = b1

main:

l_arm = pin0
r_arm = pin1

r_arm_laser = Not r_arm
'if r_arm = 1 then low r_arm_laser 	' }
'else					' } r_arm_laser = NOT r_arm
'high r_arm_laser			' }
'endif					' }

l_arm_laser = Not r_arm
'if l_arm = 1 then low l_arm_laser 	' }
'else					' } l_arm_laser = NOT r_arm
'high l_arm_laser			' }
'endif					' }

l_arm_led = 0
r_arm_led = 0
buzzer = Not r_arm

'if r_arm = 0 then low buzzer		' }
'else					' } buzzer = NOT r_arm
'high buzzer				' }
'endif					' }

gosub delay

r_arm_led = Not r_arm
'if r_arm = 1 then low r_arm_led 	' }
'else					' } r_arm_led = NOT r_arm
'high r_arm_led				' }
'endif					' }

l_arm_led = Not l_arm
'if l_arm = 1 then low l_arm_led 	' }
'else					' } l_arm_led = NOT r_arm
'high l_arm_led				' }
'endif					' }

buzzer = Not l_arm
'if l_arm = 0 then low buzzer 	        ' }
'else					' } buzzer = NOT l_arm
'high buzzer				' }
'endif					' }

gosub delay
goto main

delay:

pause 350

return
the working sloppy code is commented out.

There is no error message, it just doesnt flash the leds, light the lasers or buzz the buzzer :D
 

westaust55

Moderator
ANDNOT and ORNOT are supported keywords
but NOT does not appear :confused: to be included in PICAXE BASIC although the P.E. V5.2.2 shows it in blue colour as a key word.


EDIT:

just did some tests in the PE and NOT does work as a keyword but it is then as expected, a bitwise function

so

if pin1 = On, high (=1)
then b0 = NOT pin1 gives b0 = 254

if pin1 = Off, low (=0)
then b0 = NOT pin1 gives b0 = 255

so what does OUTPIN4 = 254 give ?

Seems OUTPINx only accepts 0 or 1 as values. Thought it might take the LSB but ????
 
Last edited:

Dippy

Moderator
I don't know if relevant but if you NOT a byte then it won't just invert the value of 1 to a 0.

NOT 0 = 255 i.e. invert 00000000
NOT 1 = 254 i.e. invert 00000001
 

Ruzzz

Member
I've come to the conclusion its bytewise.
Is there anyway of forcing it to be bitwise ?

e.g bitwise NOT 1 = 0
 

Dippy

Moderator
Well, I don't think you can shift bits on an 18X.
I only had a quick fiddle but couldn't make it do a bitwise NOT (on the simulator).
Maybe do a NOT followed by a /254?
..or even 128 to simulate a 7 bit shift rather than my poor brain doing arithmetic ;)
 
Last edited:

Jeremy Leach

Senior Member
1 XOR 1 = 0
0 XOR 1 = 1

So could use XOR operator instead of NOT to do bitwise inversion (ie Pin1 XOR 1). XOR still operates bytewise but gives the result you're looking for.
 

hippy

Ex-Staff (retired)
Bit Variables

There is no error message, it just doesnt flash the leds, light the lasers or buzz the buzzer :D
This works for me on my hardware ...
Code:
#picaxe 18x
Symbol btn = pin0
Symbol led = outpin4
Do
  led = Not btn
Loop
The "led = Not btn" is a shorthand equivalent for "led = btn ^ $FFFF". Whether 'btn' were 1, 8 or 16 bit it doesn't matter in this case because the destination 'led' (outpin4) is a 1-bit variable so only the single lsb is used when assigning the resulting value. All internal maths is done at 16-bit then msb's are truncated before assignment ...

Code:
btn     acc    ^ $FFFF   lsb    led

 0     $0000    $FFFF     1      1
 1     $0001    $FFFE     0      0
If you wanted to reduce code space, "led = btn ^ 1" can be used but is not as readable nor as clear in intent as using "Not".

When using a variable, it could also be useful to have "b0 = btn ^ 1" (b0=0/1) rather than "b0 = Not btn" (b0=$FE/$FF), but the following does work as expected, due to the truncation on assignment -

b0 = Not btn : led = b0

Which is best to use depends on what one is doing with 'b0' elsewhere.
 
Last edited:

Ruzzz

Member
Thanks.
When I progam the hardware both examples below work, BUT when using the simulator in the programming editor the Not command doesnt work.

Code:
'terminator program picaxe 18x
' input sensors		l_arm = input 0
'			r_arm = input 1
'
' Outputs 		l_arm_led 	= output 0
' 			r_arm_led 	= output 1
'			Buzzer		= output 7
'			l_arm_laser	= output 4
'			r_arm_laser 	= output 5			

Symbol l_arm_led    	= outpin0 
Symbol r_arm_led    	= outpin1
Symbol l_arm_laser 	= outpin4
Symbol r_arm_laser 	= outpin5
Symbol buzzer 		= outpin7
Symbol l_arm = pin0
Symbol r_arm = pin1

main:

r_arm_laser = r_arm ^ 1
'r_arm_laser = Not r_arm........this doesnt work in the simulator
l_arm_laser = l_arm ^ 1
'l_arm_laser = Not l_arm........this doesnt work in the simulator				

l_arm_led = 0
r_arm_led = 0
buzzer = r_arm

gosub delay

r_arm_led = r_arm ^ 1
'r_arm_led = Not r_arm........this doesnt work in the simulator
l_arm_led = l_arm ^ 1
'l_arm_led = Not l_arm........this doesnt work in the simulator

buzzer = l_arm 

gosub delay
goto main

delay:

pause 350

return
try it :D
The version above works, but un comment the lines with the Not commands and comment out the lines above which should have the same effect, and the simulator doesnt work :(
 

hippy

Ex-Staff (retired)
Yes, this seems to be a simulator issue which we will investigate - It always helps to indicate whether code is being run on real hardware, simulated or within VSM.

As a workround use the "^ 1" and simulation should be as expected.
 
Top