Helpful ReplyHot!value to move in variable size steps

Author
brownt
Super Member
  • Total Posts : 292
  • Reward points : 0
  • Joined: 2015/11/21 14:58:09
  • Location: 0
  • Status: offline
2018/02/09 02:33:30 (permalink)
0

value to move in variable size steps

I have a variable that changes from 0 - 127. And I would like an option to set a step size so that it increases by 2's or 3's etc.

I guess it is an easy problem, but I can't work it out. It is to occur in an interrupt, so no loops. On a single pass it must be determined whether the value is between an upper and lower boundary, such as 0 - 9 for a step size of 10, and then display the lower boundary of zero. Or if it is within the next step, 10 - 19, then display the lower boundary of ten. etc
#1
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: value to move in variable size steps 2018/02/09 02:43:31 (permalink)
5 (1)
brownt
I have a variable that changes from 0 - 127. And I would like an option to set a step size so that it increases by 2's or 3's etc.

Just add the contents of another variable to it, and set THAT variable to 1, 2, 3, etc.
 

...
On a single pass it must be determined whether the value is between an upper and lower boundary, such as 0 - 9 for a step size of 10, and then display the lower boundary of zero. Or if it is within the next step, 10 - 19, then display the lower boundary of ten. etc

You could just do an integer divide by 10, then multiply by 10, but than may not be the quickest way to do it.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#2
brownt
Super Member
  • Total Posts : 292
  • Reward points : 0
  • Joined: 2015/11/21 14:58:09
  • Location: 0
  • Status: offline
Re: value to move in variable size steps 2018/02/09 03:42:12 (permalink)
0
thanks.
what have I done here. This was an earlier attempt, it worked but it was like some sort of filter that let the value slowly wind its way up to the highest step.
I actually like that delay for another feature, if I can make that delay variable. Can you tell me why this code winds up slowly to the highest step. About 1.5 seconds with a 10mS sample time.
if(value >  prevValue + stepSize) {prevValue += stepSize; } 
else if(value <= prevValue- stepSize) {prevValue -= stepSize; }
value = prevValue;
#3
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: value to move in variable size steps 2018/02/09 03:50:40 (permalink)
5 (1)

...
Can you tell me why this code winds up slowly to the highest step. About 1.5 seconds with a 10mS sample time.

 
You need to reveal a few more details
e.g. definitions for each of the variables,
what value you have in stepSize
the actual values in prevValue and value when you tested this.
Are you sure it is being called every 10ms?
 
Most of these questions would be answered by just showing ALL the code.

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#4
1and0
Access is Denied
  • Total Posts : 8396
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: value to move in variable size steps 2018/02/09 04:07:08 (permalink)
4 (1)
... and what is "the highest step"?
 
#5
brownt
Super Member
  • Total Posts : 292
  • Reward points : 0
  • Joined: 2015/11/21 14:58:09
  • Location: 0
  • Status: offline
Re: value to move in variable size steps 2018/02/09 04:37:16 (permalink)
0
127
#6
brownt
Super Member
  • Total Posts : 292
  • Reward points : 0
  • Joined: 2015/11/21 14:58:09
  • Location: 0
  • Status: offline
Re: value to move in variable size steps 2018/02/09 04:43:20 (permalink)
0
int value is variable from 0 - 127
int prevValue initialises at zero and then takes its values within the code.
 
step size is say 5, but it doesn't matter much what it is, it still winds up slowly unless the step size is high such as 100 where there is only one step.
 
10mS yes, it is in the interrupt and I am watching a flag in on the scope.
 
too much code to paste.
#7
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: value to move in variable size steps 2018/02/09 04:48:24 (permalink) ☄ Helpfulby brownt 2018/02/11 01:03:58
5 (1)
If step size is 5, then it's going to get from 0 127 in 26 steps, so 260ms
If it doesn't then there's something wrong with your testing method.
Neglecting to qualify variables written in the ISR as "volatile", or some other beginner mistake.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#8
brownt
Super Member
  • Total Posts : 292
  • Reward points : 0
  • Joined: 2015/11/21 14:58:09
  • Location: 0
  • Status: offline
Re: value to move in variable size steps 2018/02/09 04:56:37 (permalink)
0
Yes, testing method was flawed as it relied on my memory of events. The delay is about 270mS for a stepSize of 5, and of course 1.27 seconds for a step size of 1. So, I can see what creates the delay. thanks for that.
 
There are no variables in the interrupt. But there is a function that is called from the interrupt that has some variables in it that are not declared as volatile. what is the issue there?
 
The function takes 5mS to execute, and the interrupt is called every 10mS.
post edited by brownt - 2018/02/09 05:03:57
#9
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: value to move in variable size steps 2018/02/09 05:00:56 (permalink)
5 (1)
If a variable is changed by code executing inside an interrupt, and is read by code outside the interrupt, then you MUST qualify it as "volatile".
Otherwise the compiler optimiser won't know that the value could change "behind the scenes", and could optimise away reads of the variable.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#10
brownt
Super Member
  • Total Posts : 292
  • Reward points : 0
  • Joined: 2015/11/21 14:58:09
  • Location: 0
  • Status: offline
Re: value to move in variable size steps 2018/02/09 05:09:47 (permalink)
0
I see. And what does optimising reads away mean?
#11
Gort2015
Klaatu Barada Nikto
  • Total Posts : 2660
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: value to move in variable size steps 2018/02/09 07:57:50 (permalink)
0
What are you trying to do?
 
Not seeing the big picture.

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#12
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: value to move in variable size steps 2018/02/09 15:06:40 (permalink) ☄ Helpfulby brownt 2018/02/11 01:03:32
0
brownt
I see. And what does optimising reads away mean?

If the compiler sees you load a variable with a value, and then never change it, it won't bother actually reading the value, it will just hard code whatever you initially loaded into it.
If the variable is changed, but only in an interrupt service, the compiler won't know about it unless you add the "volatile" qualifier to it.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#13
brownt
Super Member
  • Total Posts : 292
  • Reward points : 0
  • Joined: 2015/11/21 14:58:09
  • Location: 0
  • Status: offline
Re: value to move in variable size steps 2018/02/14 23:03:23 (permalink)
0
does that apply to global variables too, or are they volatile by default?
#14
NKurzman
A Guy on the Net
  • Total Posts : 16440
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: value to move in variable size steps 2018/02/14 23:21:19 (permalink)
0
They are not volatile by default.
But they maybe optimized differently.
#15
Jump to:
© 2018 APG vNext Commercial Version 4.5