*UPDATED Turret Control Program:

/*When you turn the rotary encoder clockwise, the angular displacement is increased;

when you turn it counterclockwise, the displacement is decreased. If you press the switch on the rotary encoder, the readings will return to zero Original code can be found here://Email: support@sunfounder.com //Website: www.sunfounder.com //2015.5.7/

**Although this is in the Public Domain, I have created a "Creative Commons License" for this work, the reason is, I made enough mod's and put in many hours of work to make this program accurate and efficient as part of a much larger project @ Hackaday.com, the DAV5 3D Printable Raman Spectrometer.

There are individuals there that have engaged in "intellectual theft" and not bothered to give credit where credit was certainly due, so I believe I have no choice but to go this route since I have put in 2 years of very hard work on this.**

The creative commons license can be viewed here: http://thequantumhub.blog/

I have modified this code to accomodate my diffraction grating turret for the DAV5 Raman 3D printable Spectrometer (http://hackaday.io/project/18126-dav5-v301-raman-spectrometer)

Written and modified by David H Haffner SR 10/18/2017

Full project details can be found here @ https://hackaday.io/project/18126-dav5-v301-raman-spectrometer

This program is also in the public domain @ https://playground.arduino.cc/Main/KY-040EncodeUsedToControlAHolographicDiffractionGratingForARamanSpectrometer

It utilizes a KY-040 encoder to control the holographic diffraction grating at steps of 1 & -1, each pin (SW,DT and CLK) has a 0.1uf ceramic cap for decoupling, this is more effective for rise time spike removal. There are 2 LED indicators, a red for encoder step movements and a yellow for home position indication that will light when the shaft docks back to a pre-determined home position,(I'm still working on it,)This is accomplished by adjusting both "StepsToTake and RotoryPosition," they are in direct relation to steps per revolution of the internal shaft according to the type of stepper motor you are using (mine is a 28-YBJ-48).Fine tuning can be done by playing between STEPS (per revolution of shaft) and Speed, this will ensure smoother transitions when turning the encoder for fewer misses. If you press the switch on the rotary encoder, the readings will return to approx zero 0.21 percent error. a 0.1uf ceramic Cap is placed @ PinSW (4) to + (the reason for the pin pullup).

The formula in this sketch is derived from the specifications of the 28-YBJ-48 stepper MTR which has a step angle of 5.62deg, to convert degrees into nanometers you 1st have to convert degrees into radians {RAD}, we do this for the 1st specification which is 5.625deg = 0.09817474 {RAD} this # is then divided by the equitorial radius of the Earth which is 3,963.2 miles.

Our new value would then be, 0.000024772 miles (NM), convert this into centimeters, (3.986667)

Now with this #, we multiply it by the constant of 1 (NM) = 1609344e+012 miles *(Variable)/ 7 gives us our converting factor from encoder position count to nanometer counts.

Sketch uses 9202 bytes (3%) of program storage space. Maximum is 253952 bytes. Global variables use 519 bytes (6%) of dynamic memory, leaving 7673 bytes for local variables. Maximum is 8192 bytes.

- include <digitalWriteFast.h>
- include <math.h>
- include <Wire.h>
- include <LiquidCrystal_I2C.h>
- include "Stepper.h"
- define STEPS 64 // Number of steps for one revolution of Internal shaft

// 2048 steps for one revolution of External shaft LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address

double double_x = 3.986667 * 1.609344e+012 * 0 / 7;

volatile boolean TurnDetected; // need volatile for Interrupts volatile boolean rotationdirection;...

Read more »
This is great! That too with 80% 3d printed objects!

Cool stuff sir!