Stole this shamelessly from the arduino web site and added it to MyGlobals.
You can use it to map a value from one set of numbers to another set.
Say you get a value from a potentiometer and want a duty cycle for PWM out.
Or use it to map spindle speed to the PWM out.
for instance:
SetPWMDuty_1(2,map(GlobalGet("SpindleSpeed"),8000,18000,0.1,100));
Assuming you have a range of 8000 to 18000 rpm on your spindle.
The above would set PWM(2) to the correct range for your spindle if the PWM value was linear to the spindle speed.
The output is an integer value if you use all integer numbers so you won't have finite control over the spindle in that case.
Make sure your Val coming in is a float or one of the other number is at least.
I checked and Pokey can handle a value like 12.3333 in the PWM output.
Code: Select all
global map = function(Val,inMin,inMax,outMin,outMax)
{
result = (Val - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
print ("Map Result " + result);
return result;
};
Thanks
Glenn
You would have to know what PWM value output the spindle will start at.
Edit:
Another note the Val needs to be a larger number than the inMin so you don't get a negative number.
Will need to test for that before calling the map function.
for instance
Code: Select all
SSpeed=GlobalGet("SpindleSpeed");
if (SSpeed>= 8000){SetPWMDuty_1(2,map(SSpeed,8000,18000,0.1,100));};
Stole this shamelessly from the arduino web site and added it to MyGlobals.
You can use it to map a value from one set of numbers to another set.
Say you get a value from a potentiometer and want a duty cycle for PWM out.
Or use it to map spindle speed to the PWM out.
for instance:
SetPWMDuty_1(2,map(GlobalGet("SpindleSpeed"),8000,18000,0.1,100));
Assuming you have a range of 8000 to 18000 rpm on your spindle.
The above would set PWM(2) to the correct range for your spindle if the PWM value was linear to the spindle speed.
The output is an integer value if you use all integer numbers so you won't have finite control over the spindle in that case.
Make sure your Val coming in is a float or one of the other number is at least.
I checked and Pokey can handle a value like 12.3333 in the PWM output.
[code]global map = function(Val,inMin,inMax,outMin,outMax)
{
result = (Val - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
print ("Map Result " + result);
return result;
};[/code]
Thanks
Glenn
You would have to know what PWM value output the spindle will start at.
Edit:
Another note the Val needs to be a larger number than the inMin so you don't get a negative number.
Will need to test for that before calling the map function.
for instance
[code]
SSpeed=GlobalGet("SpindleSpeed");
if (SSpeed>= 8000){SetPWMDuty_1(2,map(SSpeed,8000,18000,0.1,100));};
[/code]