how to inc or dec number by multiple of 10, 100, 1000....?

General discussion on mikroPascal.
Post Reply
Author
Message
Briansw
Posts: 82
Joined: 28 Jul 2008 16:25

how to inc or dec number by multiple of 10, 100, 1000....?

#1 Post by Briansw » 23 Dec 2008 16:47

Been trying to figure out a good way to either inc or dec a variable by some multiple. Anyone have a good suggestion? Things I've been trying just have not worked as the variable gets mutiplied for instance x10 but the contents of variable gets multiplied not incremented by 10.
i:=0
inc(i);
i:=i*10

i now = 10
so we do this several more times and results are
i=110
i=1110
i=11110
just don't do what I need. I understand why.

I'm trying to do increments as such 210,220,230,240 etc....
and so on

I need to perform this with dec also using negative numbers so I can decrement a number as such 240,230,220,210 etc...

This is for my encoder project which is working great but only inc or dec a value by one. I need to change to different multiples to make setting the final value faster. multiples of 1, 10, 100, 1000, 10000, 100000....
A keypad entry will be added shortly but for now got to get the encoder to do it's job.

Thanks, help is appreciated.
Brian

rmteo
Posts: 1330
Joined: 19 Oct 2006 17:46
Location: Colorado, USA

#2 Post by rmteo » 23 Dec 2008 17:03

I'm not a Pascal (BASIC) user, but would this work?

Code: Select all

i:= 0
i:= i+10
i:= i-10

i:= i+210
Looks like you are multiplying instead of incrementing (or decrementing).
Why pay for overpriced toys when you can have
professional grade tools for FREE!!! :D :D :D

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#3 Post by Dany » 23 Dec 2008 17:22

Hi, you could do something like this:

Code: Select all

var MyNumber, Delta: integer;
  ...
  Delta = -1000;
  ...
  ...
  inc(MyNumber, Delta);
  ...
  ...
  inc(MyNumber, Delta);
  ...
  ...    
I am no sure though if this is what you had in mind... :oops:

Addendum: in following posts it became clear that "Inc" only accepts one paremeter: the variable to be incremented. The increment value is always "1". Sorry for the inconvenience. :oops:
But: one can always write ones own "Inc" and "Dec" functions or procedures (which will have to have another name of course...). :D
Last edited by Dany on 23 Dec 2008 20:56, edited 1 time in total.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

rmteo
Posts: 1330
Joined: 19 Oct 2006 17:46
Location: Colorado, USA

#4 Post by rmteo » 23 Dec 2008 17:32

Dany, interesting that mP supports that construct.

Unfortunately (for me), mB does not. :oops:
Why pay for overpriced toys when you can have
professional grade tools for FREE!!! :D :D :D

Briansw
Posts: 82
Joined: 28 Jul 2008 16:25

#5 Post by Briansw » 23 Dec 2008 17:53

Dany wrote:Hi, you could do something like this:

Code: Select all

var MyNumber, Delta: integer;
  ...
  Delta = -1000;
  ...
  ...
  inc(MyNumber, Delta);
  ...
  ...
  inc(MyNumber, Delta);
  ...
  ...    
I am no sure though if this is what you had in mind... :oops:
Wow, thanks, didn't know it could do that either.
Thanks much,

edited:
Tried this but the compiler is not accepting it. :cry:
expects a ) but found a ,

inc(MyNumber, Delta);
.....................^.................
would have been nice though.
Brian

Briansw
Posts: 82
Joined: 28 Jul 2008 16:25

#6 Post by Briansw » 23 Dec 2008 18:12

rmteo wrote:I'm not a Pascal (BASIC) user, but would this work?

Code: Select all

i:= 0
i:= i+10
i:= i-10

i:= i+210
Looks like you are multiplying instead of incrementing (or decrementing).
Looks like you are right. Guess I should not be programming so early in the morning. I did mult and suppose to add :shock:
I'll see if I can correct this now.

Thanks much,

Brian

rmteo
Posts: 1330
Joined: 19 Oct 2006 17:46
Location: Colorado, USA

#7 Post by rmteo » 23 Dec 2008 18:19

Looks like mP has the same limitations as mB as far as INC and DEC are concerned. Pity. :oops:

BTW, I also use another BASIC compiler that does support the construct that Dany suggested. :lol:
Why pay for overpriced toys when you can have
professional grade tools for FREE!!! :D :D :D

Briansw
Posts: 82
Joined: 28 Jul 2008 16:25

#8 Post by Briansw » 23 Dec 2008 19:30

Well I got it working. Now just have to get a button to cycle through what ever multipiler I want to use.
Curious as whether you can run two interrupt routines at the same time?
Or as my encoder is using 2 interrupts (detects change on B port pins) if I can attach a button(switch) also to one of those same port pins?
Thanks
Brian

rmteo
Posts: 1330
Joined: 19 Oct 2006 17:46
Location: Colorado, USA

#9 Post by rmteo » 23 Dec 2008 19:47

You can have as many interrupts as you want (within limits). Just poll the interrupt flags in the ISR to determine the source of the interrupt. Which PIC are you using?

With the PIC24s and dsPICs, it is a lot nicer and easier as these devices have vectored interrupts.
Why pay for overpriced toys when you can have
professional grade tools for FREE!!! :D :D :D

Briansw
Posts: 82
Joined: 28 Jul 2008 16:25

#10 Post by Briansw » 23 Dec 2008 20:51

Its an 18f4620. Just I think I might be hardpressed to use another one because my encoder and Lcd share PortB. I could move if I have to the lcd over to another port but would have to make an interface board for it to plug into the Easypic5 board. Not a difficult thing as I've done most of my external devices like that. I'm still fairly new to using interrupts but starting to get a feel for them. I think I'll hold off on using pic24s and dspics for now until I get a better grasp of the ones I have.

Thanks again,
Brian

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#11 Post by Dany » 23 Dec 2008 20:54

rmteo wrote:Dany, interesting that mP supports that construct.
Unfortunately (for me), mB does not. :oops:
Well, apparently mP cannot either. I was blinded and/or mislead by the other Pascal compiler I use (for PC): Delphi. Sorry for the inconvenience. :oops:
But: one can always write ones own "Inc" and "Dec" functions or procedures (which will have to have another name of course...). :D
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

rmteo
Posts: 1330
Joined: 19 Oct 2006 17:46
Location: Colorado, USA

#12 Post by rmteo » 23 Dec 2008 21:05

No problem, Dany. I use 4 dialects/versions of BASIC (3 on PICs, and VB for Windows/PC) and it is really easy to get confused. :twisted: :twisted: :twisted:
Why pay for overpriced toys when you can have
professional grade tools for FREE!!! :D :D :D

Post Reply

Return to “mikroPascal General”