Hi another newby her with a problem. First I have been researching this problem for about 2-3 months now and have found code that does not work.
The problem is I wish to display angles in degrees and decimal fractions. Example would be 45.76 etc. I have found code that will read the pulse count but the code that I have found will not read the pulses and convert them to angles or decimal angles.
I eventually hope to attach an encoder to a Murata DSD-40BCD series display and be able to read an angle to 1/100 degree precision. Can any of you please help me with the code and if you have any hardware ideas or input.
Do the math. for an encoder to read with .01 degree resolution it would need to have 36000 steps. If you have a 3 inch knob - and making some other simplifications - the knob would turn about .0005 inches per step.
I know how encoders work and have the resolutions I need.
The problem is the software to count the pulses and tell me the amount of movement in degree and fraction of degree. Example might be something like 3,600 ppr. I move the encoder shaft 36 pulses. I want to see a display that says I changed the arm position .01 degrees.
The problem is in coding the Arduino Uno to read the pulses and them display them as degrees and decimal fractions of degrees.
One solution is to use floating point, but floating point values are never accurate. If you want accuracy, hold an integral number of your most granular measurement (one pulse) in an int or long variable.
As to how to convert this value into digits of a decimal number - well, that's a bit of division and multiplication. If there's 4321 clicks to a degree, and you want to display to two decimal places, then you multiply by 100 and then divide by 4321, being sure to use a variable that will not overflow (a long or long long). You then convert that to a string using any one of the standard libraries (itoa, ltoa), and jam a decimal point in front of the last two digits.
Beats me what anyone is finding difficult about this.
I think you may want to specify your problem more concretely, by way of specifying what a function that solves your problem would do. "I need a function to take a long value denominated in 4321ths of a degree, which fills a globally-scoped string with that value converted to degrees with two decimal places. The value may be any long int. The output should be between -179.99 and 180.00".
What we need to know is do you want the position of the encoder to represent actual degrees? Or can you spin the encoder 20 times (for example) to get the full 360 degree reading.
JohnSexton:
Example might be something like 3,600 ppr. I move the encoder shaft 36 pulses. I want to see a display that says I changed the arm position .01 degrees.
36 / 3600.0 = 0.01
That's 0.01 revolution of the encoder but you can call that 0.01 degree if you want 360 turns of the encoder to represent 1 turn of the arm.
Thanks all. I was able to glean some help out of your answers. I believe I am at a point that I do not know what I need to know or that I don't know. Maybe too big of a task for my current knowledge base.
As I am no programmer it is truly a foreign language to me and I was always bad at languages. heeeeheeeee.
Thanks again I guess I will have to study more programming them maybe be able to ask the right questions.
That won't work since .println() suppresses leading zeroes. Counting from 1.99 to 2.11 would get you:
1.99
2.0
2.1 (correct answer is 2.01)
2.2 (correct answer is 2.02)
...
2.9 (correct answer is 2.09)
2.10
2.11
This will take some floating-point math but at least it will display correctly:Serial.println(count/100.0, 2);