Program ignores "count" variable value

DeonSteyn

New Member
Hi
First time I am posting here. Would appreciate any help since this is now driving me crazy. After 4 days I just cannot see the error I made in my program.

I wrote this rather simple code for 14M2 to count pulses for a duration of 1 second. The "count" value is placed in the variable b1. I then test with IF statements to switch on different LED's (one of eight) depending the value of b1 (the "count" value). Ironically the pwm generation and pulse count works fine (if checked with the debug) but the easier part (at least I thought so!) is a problem.

Now the problem:

For any value of 13 or less for b1 (as set in the little "Generic" window of the "simulation" panel) the correct "IF" is executed, i.e. the correct LED is switched on. However for any value 14 or above the program executes the "IF" of the third LED as well as the following one (the fourth LED)! If you increase the value more to let say something that should only switch only the fifth LED, all IF statements are executed, from the IF of the third LED to the IF of the fifth LED! This in now all in the Simulator (I am using Picaxe Programming Editor).

In real life (the circuit) only the LED up to the third one is switched on even for b1 values (as confirmed in the DEBUG window) that should switch on the fifth LED.

Please help. Will appreciate. Thanks in advance.

Below is the code:
Code:
main:

pwmout B.2, 23, 60  ; this statement just generates pulses for the IR LED that is driven for the IR receiver which generates the pulses for count (which gives me the rpm)
count 3,1000,b1
if b1 > 3 and b1 < 5 then           ; LED NUMBER 1
	high C.4 
	pause 500
	low C.4
	end if
	
	if b1 = 5 or b1 > 5 and b1 < 8 then    ; LED NUMBER 2
	high C.2
	pause 500
	low C.2
	end if
	
	if b1 > 8 or b1 = 8 and b1 < 12 then    ; LED NUMBER 3
	high C.1
	pause 500
	low C.1
	end if
	
	if b1 > 13 or b1 = 13 and b1 < 17 then    ; LED NUMBER 4
	high C.0
	pause 500
	low C.0
	end if
	
	if b1 > 17 or b1 = 17 and b1 < 20 then    ; LED NUMBER 5
	high B.5
	pause 500
	low B.5
	end if
	
	if b1 > 20 or b1 = 20 and b1 < 25 then    ; LED NUMBER 6
	high B.4
	pause 500
	low B.4
	end if
	
	if b1 > 25 or b1 = 25 and b1 < 31 then    ; LED NUMBER 7
	high B.3
	pause 500
	low B.3
	end if
	
	if b1 > 31  then               ; LED NUMBER 8
	high B.1
	pause 500
	low B.1
	end if
	   
debug
b1 = 0

goto main
 
Last edited by a moderator:

g6ejd

Senior Member
main:

pwmout B.2, 23, 60 ; this statement just generates pulses for the IR LED that is driven for the IR receiver which generates the pulses for count (which gives me the rpm)
count 3,1000,b1
if b1 > 3 and b1 < 5 then ; LED NUMBER 1
high C.4
pause 500
low C.4
end if

if b1 >= 5 and b1 < 8 then ; LED NUMBER 2
high C.2
pause 500
low C.2
end if

if b1 >= 8 and b1 < 12 then ; LED NUMBER 3
high C.1
pause 500
low C.1
end if

if b1 >= 13 and b1 < 17 then ; LED NUMBER 4
high C.0
pause 500
low C.0
end if

if b1 >= 17 and b1 < 20 then ; LED NUMBER 5
high B.5
pause 500
low B.5
end if

if b1 >= 20 and b1 < 25 then ; LED NUMBER 6
high B.4
pause 500
low B.4
end if

if b1 >= 25 and b1 < 31 then ; LED NUMBER 7
high B.3
pause 500
low B.3
end if

if b1 > 31 then ; LED NUMBER 8
high B.1
pause 500
low B.1
end if

debug
b1 = 0

goto main

~~~~~

Will help or use the CASE command instead
 

darb1972

Senior Member
Hello Deon

You might tend to find that if you assign symbols to the bytes not only will others find the program easier to follow, but if you have to revisit it later you will also find it easier to refresh your memory on the program flow. Eg: b1 = led_number etc.

Also, maybe add a few more rem statements to clarify your intentions.

I realise when you write a program it seems unnecessary at the time (particularly for small programs), but, as programs grow and become far more complex it is a really good habit to form.

As the other response also suggested, maybe take a look at the "select case" command. I'm by no means a programming wiz at this stuff but I think that statement might help tidy things up a bit.
 

Goeytex

Senior Member
1. You are using the count pin (3) for count as well as for an LED (b.3) . Instead of using a single digit for the count pin, use the modern method of B.3 instead of just "3".
this means you need to use a different pin for one or the other. I used c.3 for count in the example below.

2. PWMOUT should not be in the MAIN loop since every time it is called the PWM is reset and there may be a glitch. Initialize the PWM before the main loop

3. Think logically about IF statements. For example your first IF: "if b1 > 3 and b1 < 5 then" . The only possible way this can be "true" is if B1 = 4. Is that what you really intended? If so then use "IF b1 = 4" instead.

4. Use "select case" instead of "IF then" for long lists of conditional statements.

Example:
Code:
#Picaxe 14M2
#no_data

pwmout B.2, 23, 60 

MAIN: 

count c.3, 1000, b1

Select Case b1

   Case 4
      high C.4
      pause 500
      low c.4
   
   Case 5 to 7 
      high c.2
      pause 500
      low c.2         
   
   Case 8 to 11
      high c.1
      pause 500
      low c.1
   
   Case 12 to 16
      high C.0
      pause 500
      low C.0   
   
   Case 17 to 19
      high B.5
      pause 500
      low B.5

   Case 20 to 24
      high B.4
      pause 500
      low B.4   
   
   Case 25 to 30
      high B.3
      pause 500
      low B.3
          
   Case > 30
      high B.1
      pause 500
      low B.1
   
   end select 

Goto main
 

DeonSteyn

New Member
Thanks everyone

Thanks everyone. Found positive attitude and practicality of advice extremely positive.
It is working now! The implementation of the CASE command and "count c.3" instead of "count 3" did it.
Regards
Deon


Hi
First time I am posting here. Would appreciate any help since this is now driving me crazy. After 4 days I just cannot see the error I made in my program.

I wrote this rather simple code for 14M2 to count pulses for a duration of 1 second. The "count" value is placed in the variable b1. I then test with IF statements to switch on different LED's (one of eight) depending the value of b1 (the "count" value). Ironically the pwm generation and pulse count works fine (if checked with the debug) but the easier part (at least I thought so!) is a problem.

Now the problem:

For any value of 13 or less for b1 (as set in the little "Generic" window of the "simulation" panel) the correct "IF" is executed, i.e. the correct LED is switched on. However for any value 14 or above the program executes the "IF" of the third LED as well as the following one (the fourth LED)! If you increase the value more to let say something that should only switch only the fifth LED, all IF statements are executed, from the IF of the third LED to the IF of the fifth LED! This in now all in the Simulator (I am using Picaxe Programming Editor).

In real life (the circuit) only the LED up to the third one is switched on even for b1 values (as confirmed in the DEBUG window) that should switch on the fifth LED.

Please help. Will appreciate. Thanks in advance.

Below is the code:
Code:
main:

pwmout B.2, 23, 60  ; this statement just generates pulses for the IR LED that is driven for the IR receiver which generates the pulses for count (which gives me the rpm)
count 3,1000,b1
if b1 > 3 and b1 < 5 then           ; LED NUMBER 1
	high C.4 
	pause 500
	low C.4
	end if
	
	if b1 = 5 or b1 > 5 and b1 < 8 then    ; LED NUMBER 2
	high C.2
	pause 500
	low C.2
	end if
	
	if b1 > 8 or b1 = 8 and b1 < 12 then    ; LED NUMBER 3
	high C.1
	pause 500
	low C.1
	end if
	
	if b1 > 13 or b1 = 13 and b1 < 17 then    ; LED NUMBER 4
	high C.0
	pause 500
	low C.0
	end if
	
	if b1 > 17 or b1 = 17 and b1 < 20 then    ; LED NUMBER 5
	high B.5
	pause 500
	low B.5
	end if
	
	if b1 > 20 or b1 = 20 and b1 < 25 then    ; LED NUMBER 6
	high B.4
	pause 500
	low B.4
	end if
	
	if b1 > 25 or b1 = 25 and b1 < 31 then    ; LED NUMBER 7
	high B.3
	pause 500
	low B.3
	end if
	
	if b1 > 31  then               ; LED NUMBER 8
	high B.1
	pause 500
	low B.1
	end if
	   
debug
b1 = 0

goto main
 

john2051

New Member
Hi I just looked at the count command, and it says to use a word variable, could this be causing problems.
regards john
 

The bear

Senior Member
Hi Everyone,
As a beginner, I'm always interested in peoples programs. Loaded Geoytex's version, its not doing anything. I've put various numbers against b1 in the 'Watch' panel, e.g. = 4 to 22
What am I overlooking?
Regards, Bear..
 

Goeytex

Senior Member
I imagine it is just understanding how to use PE6 Simulator.

I opened up PE6 and after about 15 minutes of trying to figure out how to enter a value for B1 in the simulator I gave up. Looked for the PE6 Manual/ Help file and there doesn't seem to be one. Click on the help Icon and all you get are irrelevant links. This is why I generally do not use PE6. Lots of fancy animated windows, bells and whistles but not much on documentation.

Try it in PE5 instead.
 

Technical

Technical Support
Staff member
b1 is RAM address 1.

So to change it - in the memory panel - double click over the appropriate cell (second cell on top line), enter a new value and hit return.
See page 68 of the manual for this and other info: http://www.picaxe.com/docs/picaxe_manual1.pdf

The watch panel breakpoint value is a completely different function - this will 'break' (pause) the simulation when the particular value entered beside a variable is reached by that variable during the simulation.
 

tony_g

Senior Member
I imagine it is just understanding how to use PE6 Simulator.

I opened up PE6 and after about 15 minutes of trying to figure out how to enter a value for B1 in the simulator I gave up. Looked for the PE6 Manual/ Help file and there doesn't seem to be one. Click on the help Icon and all you get are irrelevant links. This is why I generally do not use PE6. Lots of fancy animated windows, bells and whistles but not much on documentation.

Try it in PE5 instead.
i too have found that i cant get along with the PE6 simulator and often end up altering bits of my code to be able to test it in pieces using PE5, but technical has pointed out the manual has some info so i shall have a read and try to get along with the new sim :eek:
 

The bear

Senior Member
@ Technical,
Thank you for the 'Memory' info. But still confused, it doesn't take much.
If you hover the mouse over b1, its a variable isn't it? (PE6)
Regards, Bear..
 
Top