Trying to learn but...

regpye

New Member
I have been working on several simple versions of this code, trying to learn as I go.
Some of you have helped me in the past recently and I appreciate that a lot, but I am still trying to learn other things so I am using the same project to do this.
While working through this code I came up with something rather strange that I can't work out why.
There are two parts of the code that are almost identical, one part works fine and the other part a section of it is really off.
I am referring to the w4 section of the code which is suppose to reduce by 10 on each cycle, but it does not. I have commented in the code where.
The best way to see what I am talking about is to run the code in the simulator using the c.1 slider to change the light value from say 110 to 90 to give a simulation of either day or night. It is the night time cycle that has the problem. I have commented out some of the values to a lower value of some settings to reduce the timing for simulation speed.

I just want to know why this is happening, not to have a complete re-coding done as I am using this example in my learning.

Code:
#Picaxe 08M2
;#Terminal 4800
#No_Data


;              .-----__-----.
;             -| V+      0V |-
;             -| SI     C.0 |---> Pump
;  Day LED <---| C.4    C.1 |<--- LDR
;             -| C.3    C.2 |---> Night LED
;              `------------'

let w1 = 4300
let w2 = 2000
let w3 = 4300
let w4 = 2000

main:
readadc C.1,b0     ; read LDR for night or day into variable b0
if b0 > 105 then Day
if b0 < 95 then night
if b0 > 96 and b0 < 104 then waiting

waiting:
wait 6
goto main

Day:
high c.0            ; pump is on, green LED is on
low c.2            ; yellow led off
low c.4            ; red led off
for b2 = 1 to 10;0     ; 100  commented to reduce for simulation time
pause 250            ; wait 25 seconds
sertxd("Day Time pump is on ", #b2,13,10)           
next b2
sertxd("Day Time pump is off ",13,10)
off_time_day:
readadc C.1,b0        ; read LDR for night or day into variable b0       
if b0 < 95 then Night    ; if b0 < 100 then do night

for b3 = 1 to 3;0        ; 30  commented to reduce for simulation time
gosub off_time
   
next b3

; turn pump on again for second time
high c.0            ; pump is on, green LED is on
low c.2            ; yellow led off
low c.4            ; red led off
for b2 = 1 to 10;0    ; 100 commented to reduce for simulation time
pause 250            ; wait 25 seconds
sertxd("Day Time pump is on ", #b2,13,10)   
readadc C.1,b0        ; read LDR for night or day into variable b0       
if b0 < 95 then Night    ; if b0 < 100 then do night
next b2
sertxd("Day Time pump is off ",13,10)
for b3 = 1 to 3;0        ; 5 cycles per hour so times by hours wanted 5x6 = 30 = 6 hours  commented to reduce for simulation time
gosub off_time
next b3

goto main


Night:
low c.0
for b3 = 1 to 50        ; 5 cycles per hour so times by hours wanted 5x10 = 50 =10 hours
gosub night_off_time
readadc C.1,b0        ; read LDR for night or day into variable b0       
if b0 > 104 then Day    ; if b0 > 104 then do Day
next b3
goto main

off_time:        ; 12 minute cycle

for b8 = 1 to 200
low c.0   
high c.4
pause w2
low c.4
pause w1
w1 = w1 - 15
w2 = w2 - 10 

sertxd(" day off Time  ",#w1, "  :  ", #w2, "  :  ",13,10)

next b8
return

night_off_time:        ; 12 minute cycle

for b9 = 1 to 200
low c.0   
high c.4
pause w4
low c.4
pause w3
w3 = w3 - 15
w4 = w4 - 10        ; SOMETHING WRONG HERE counts up randomly instead of down by 10

sertxd("night off Time  ",#w3, "  :  ",#w4, "  :  ",13,10)

next b9
return
 

Aries

New Member
w4 is made up of two byte variables, b8 (lower 8 bits) and b9 (upper 8 bits). You are using b9 as the counter in the loop as well as in w4. The result is that w4 is changing by +256 (from b9) and -10 (from w4 = w4 - 10), as well as being changed from your original value of 2000 when the loop starts.
 

papaof2

Senior Member
There is a list of the Bx to Wx assignments in PICAXE Manual 1. Search for General Purpose Variables.
 

lbenson

Senior Member
Generally, to avoid the mixing up of word variables and byte variables which are contained in those words, I start using byte variables at the lowest and work up, and word variables at the highest, and work down (omitting to use at least b0 so I can use the bit variables which make it up--bit0 through bit7--as flags as needed).

On the M2 parts, I also use the system word variables--s_w1 through s_w6--see page 16 in manual 2.
 

regpye

New Member
Generally, to avoid the mixing up of word variables and byte variables which are contained in those words, I start using byte variables at the lowest and work up, and word variables at the highest, and work down (omitting to use at least b0 so I can use the bit variables which make it up--bit0 through bit7--as flags as needed).

On the M2 parts, I also use the system word variables--s_w1 through s_w6--see page 16 in manual 2.
And thank you for your info too, much obliged
 

regpye

New Member
Thanks guys, I have it working good now after understanding how the variables work.
I have many manuals to read and also books too, but it is not easy to understand sometimes.
 

hippy

Technical Support
Staff member
What can be very good parctice is to not use raw 'b' or 'w' variables within code, but name them via SYMBOL commands at the top of the code, then only use those names. It's then fairly easy to keep tack of which have been used and what's available. For example ...
Code:
Symbol reserveW0 = w0 ; b1:b0
Symbol current   = w1 ; b3:b2
Symbol total     = w2 ; b5:b4
Symbol rawAdc    = b6
 

oracacle

Senior Member
What can be very good parctice is to not use raw 'b' or 'w' variables within code, but name them via SYMBOL commands at the top of the code, then only use those names. It's then fairly easy to keep tack of which have been used and what's available. For example ...
Code:
Symbol reserveW0 = w0 ; b1:b0
Symbol current   = w1 ; b3:b2
Symbol total     = w2 ; b5:b4
Symbol rawAdc    = b6
This is something I think that is slightly harmful to learning to code with picaxe. Most other languages you have to declare the you variables otherwise it assumes there is none.
 

regpye

New Member
What can be very good parctice is to not use raw 'b' or 'w' variables within code, but name them via SYMBOL commands at the top of the code, then only use those names. It's then fairly easy to keep tack of which have been used and what's available. For example ...
Code:
Symbol reserveW0 = w0 ; b1:b0
Symbol current   = w1 ; b3:b2
Symbol total     = w2 ; b5:b4
Symbol rawAdc    = b6
Thanks for that info, I am still learning, but it is also a lot of fun.
 

hippy

Technical Support
Staff member
This is something I think that is slightly harmful to learning to code with picaxe. Most other languages you have to declare the you variables otherwise it assumes there is none.
Unfortunately there is no simple answer to how variable naming should be handled in a programming language.

Having pre-defined 'b' and 'w' variables allows students to be introduced to things like 'b0 = b0 + 1' without having to understand about variable naming.

Some languages require variables to be declared before they can be used, but others allow them to be automatically created as they are assigned to. Some say that's the easiest and best approach but allowing that can lead to other mistakes being made by programmers.

Having only predefined variables, which can be given alternative names, greatly simplifies 'running out of variable space' on a constrained device and having 'w', 'b' and 'bit' variables allows things to be done which can be somewhat harder in other languages.

It does of course allow a programmer to reuse variables, or parts of other variables, when they shouldn't, and there's no way to easy detect when that's done in error.

It's probably fair to say it allows novices to more easily get to grips with programming but can make it more challenging as they advance to more complicated programs. But, as they advance, they should be in a better position to more easily understand what the issues are.
 

regpye

New Member
Can an example be given of how one would setup some named variables, apply some calculations to them in various ways and then execute them in some simple code just to show the workings of them?
I found the literature rather sparse in any explanations.
 

Aries

New Member
This is a bit simplistic, but it does show word and byte variables ("w" and "b") allocated to symbols, which are then used to do the calculations and print the results
Code:
symbol Var1 = w1
symbol Var2 = b4
symbol Var3 = b5

Var2 = 2
Var3 = Var2 * 3
Var1 = Var2 + Var3 + 4
sertxd(#Var1," ",#Var2," ",#Var3)
As you progress, you will discover that one of the most important use of symbols is in naming areas of memory, which are used to store values during the running of the program but are not used directly in calculations (that may not yet make sense to you, but persevere)
 

hippy

Technical Support
Staff member
My simple example would be ...
Code:
b0 = 123    : SerTxd("Variable 'b0' starts with value : ", #b0, CR, LF)
b0 = b0 + 1 : SerTxd("After adding one 'b0' becomes   : ", #b0, CR, LF)
Code:
Symbol myNumber = b0

myNumber = 123          : SerTxd("Variable 'myNumber' starts with value : ", #myNumber, CR, LF)
myNumber = myNumber + 1 : SerTxd("After adding one 'myNumber' becomes   : ", #myNumber, CR, LF)
Rather than using 'b0' or 'myNumber' you can give your variable any name which is appropriate to how it is used in the code. That can be SYMBOL defined to a 'w' or 'b' variable depending on whether it will hold a 16-bit value, 0-65535, or an 8-bit value, 0-255.
 

regpye

New Member
My simple example would be ...
Code:
b0 = 123    : SerTxd("Variable 'b0' starts with value : ", #b0, CR, LF)
b0 = b0 + 1 : SerTxd("After adding one 'b0' becomes   : ", #b0, CR, LF)
Code:
Symbol myNumber = b0

myNumber = 123          : SerTxd("Variable 'myNumber' starts with value : ", #myNumber, CR, LF)
myNumber = myNumber + 1 : SerTxd("After adding one 'myNumber' becomes   : ", #myNumber, CR, LF)
Rather than using 'b0' or 'myNumber' you can give your variable any name which is appropriate to how it is used in the code. That can be SYMBOL defined to a 'w' or 'b' variable depending on whether it will hold a 16-bit value, 0-65535, or an 8-bit value, 0-255.
Thanks for that example Hippy.
Can any mathematical equation be used as well as the plus sign or are there some restrictions?
 

AllyCat

Senior Member
Hi,

Yes, in principle you can use any of the 4 normal mathematical operators (i.e. + - * and / ) and there are actually many more "Logical" and "Unary" operators described near to the start of the "Programming" section of the PICaxe Manual. They can also be written in a sequence (or Expression) on a single line, such as b0 = b0 + 2 * 3 , but there are two restrictions: The calculation is always performed "From Left to Right" and Brackets are NOT permitted in PICaxe Basic (which are both a little unusual for a programming Language), which tends to limit the practical length of a single line.

But perhaps I should be pedantic and point out that it's not really an "equation" (because in mathematical terms b0 = b0 + 1 is not a soluble equality), it's typically called an "Assignment", thus the "=" symbol is better read as "becomes", i.e.: "The value of b0 becomes (the present value of) b0 plus 1" (also known as "INCrementing b0"). Thus, if b0 starts with the value of 1, the expression in the paragraph above is read as " b0 becomes 1 + 2 " (i.e. 3) and then "b0 becomes 3 * 3" , (i.e. 9), because of the Left to Right rule. A "normal" maths calculation would be 1 + (2 * 3) which equals 7 (whether the brackets are used or not) because of the "Rule of Precedence" that * (multiplication) has a higher priority than + and should be performed first. Also, I often say "The value in b0" to show that it's basically a "container" in which you can put various types of "object" (e.g. a Number or an Alphabetic Character), provided that you remember what type it's supposed to represent.

Cheers, Alan.
 

regpye

New Member
But perhaps I should be pedantic and point out that it's not really an "equation" (because in mathematical terms b0 = b0 + 1 is not a soluble equality), it's typically called an "Assignment", thus the "=" symbol is better read as "becomes", i.e.: "The value of b0 becomes (the present value of) b0 plus 1" (also known as "INCrementing b0").
Thanks Alan,
That is exactly the information I was referring to. Very useful information.
 

Aries

New Member
The full list of mathematical operators (there are more than just plus, minus, multiply and divide) are in the Picaxe Manual 2, under "Variables - Mathematics"
 
Top