Is there a way to add more modes to this?

Prototype

New Member
Hello I have been working on a piece of codes that gives me 4 modes; 0,1,2,3. I was just wondering if there was any way I could add a few more modes instead of just 4?

I have read through manual 2 and can’t find anything that relates to what I’m trying to do
(Can anybody point me to a page number?)

The segment of code that I am using that gives me 4 modes is:
Code:
   mode = mode + 1 & %00000011 ' gives me mode = 0,1,2,3 only
Could there be any way of altering that line to give me a few more modes?

Thanks.


Prototype
 

Svejk

Senior Member
Oh, this font that you use is realy hard to read.

Anyway: what you are trying to do is bit masking, use more 1's at the end and you get more "modes". I'm leting you figure out how many for each 1.
 

Pauldesign

Senior Member
A question well defined or asked is a question half solved.

Can you please elaborate more and please use standard font size otherwise you'll have to supply reading glasses:)cool:) if you need correct answers from forum members.:confused:
 

Chavaquiah

Senior Member
Look at the MAX operator.

Code:
symbol MaxMode = 13
symbol mode = b0

mode = mode + 1 MAX MaxMode
MAX prevents the result from going higher than the value you provide. Same as your bitmasking currently is doing for 3 (%00000011).
 

Prototype

New Member
Thanks for your help,

I have done this code now
Code:
mode = mode + 1 & %00000111
This now enters mode 5 smoothly but once the button is pressed and released to change modes it cycles through every mode without stopping is there anyway to stop this?

Thanks.

Prototype
 

Svejk

Senior Member
Now you have 8 modes (counting to 111 binary is 0 to 7 decimal). If you want to cycle through a different number use a test:

Code:
mode = mode + 1
if mode > 5 then 
  mode = 0
endif
will cycle through 0 to 5
 

Pauldesign

Senior Member
Add delays (using wait or pause commands) or add test conditions (using if commands) between successive modes.

E.g if this mode equals a particular value; then gosub (to a subroutine) which will perform other things depending or what you're planning to achieve.

You can also use the halt, stop, end and other commands; depending on your application.
 

hippy

Ex-Staff (retired)
The most optimal code is -

mode = mode + 1 // 5

This will cycle 1, 2, 3 , 4, 0, 1, 2, 3, 4, 0 etc

The "// N" number is the number of options, in this case 5, the values 0, 1, 2, 3 and 4.

Where the "// N" number happens to be a 'power of two' that can be replaced by "& (N-1)", for example -

mode = mode + 1 // 2
mode = mode + 1 & 1

mode = mode + 1 // 4
mode = mode + 1 & 3

mode = mode + 1 // 8
mode = mode + 1 & 7

mode = mode + 1 // 16
mode = mode + 1 & 15

And of course this works the other way. The original "mode = mode + 1 & %00000011" is the equivalent of "mode = mode + 1 // 4".
 

Prototype

New Member
Cheer’s for all of your help I have decided to go with 8 modes, modes 0, 1, 2, 3, 4, 5, 6, 7.

I also have another issue that I can’t seem to get my head around, in my interrupt I have a status LED to show which mode I am currently in.
Code:
symbol StatusLED = 4
symbol counter = b1   

interrupt:
   for counter = 2 to Mode
      low StatusLED
      pause 250 
      high StatusLED
      pause 250
      next counter
What I am trying to achieve is to make my status led low in mode 0, (this will show me that the program is off, well not off but not doing anything)

When entered into mode 1 I want the status LED to come on.

When entered into mode 2 I want the status LED to go low then high then low then high. (so it blinks twice.)

And when in mode 3 the Status led should blink 3 times and so on for the rest of the modes.

Does anybody have any suggestions?
Thanks.

Prototype
 
Last edited:

Pauldesign

Senior Member
Below is a subroutine code for PICaxe28X2 (40x2) to blink once a LCD or LED

By changing the counter value (b0<1) sets the number of times the LCD or LED will blink; changing the wait delays set the duration of ON and OFF.

You may modify the code to suit your use.


Blink_LCD:

do while b0<1 ;process loop as long as b0 value is less than 1
inc b0 ; increase b0 by 1
high 1 ;turn ON LED on pin 1
wait 3 ;delay LCD or LED on for 3S
low 1 ;turn OFF LED on pin 1
wait 1 ;delay LCD or LED on for 1 sec
loop ;repeat do condition
clearbit b0,0 ;reset LSB 0 of b0 after completion so as to initialiseds the counter when receiving new codes
wait 3 ;for 3S
return ;return from this subroutine to stack
 

Pauldesign

Senior Member
Note, the bottom wait 3 delay is not necessary for your use and alternatively, u may use the toggle command (depending on what picaxe u’re using) if you just need a 50% duty cycle (on then off at 1 intervals) delay and this also save coding space.
 
Top