<pre>
<font color="#434f54">//* Sumner sketch to control a stepper motor for a turntable with TB6600 stepper motor driver and Arduino without a library</font>
<font color="#434f54">/// 1st Arduino</font>
<font color="#434f54">// Define stepper motor connections and buttons:</font>
<font color="#00979c">const</font> <font color="#00979c">int</font> <font color="#000000">dirPin</font> <font color="#434f54">=</font> <font color="#000000">2</font> <font color="#000000">;</font> <font color="#434f54">// To Stepper Motor (DIR+)</font>
<font color="#00979c">const</font> <font color="#00979c">int</font> <font color="#000000">stepPin</font> <font color="#434f54">=</font> <font color="#000000">3</font> <font color="#000000">;</font> <font color="#434f54">// To Stepper Motor (PUL+)</font>
<font color="#00979c">const</font> <font color="#00979c">int</font> <font color="#000000">buttonPin1</font> <font color="#434f54">=</font> <font color="#000000">4</font> <font color="#000000">;</font> <font color="#434f54">// To hand controller clockwise roation (Blue Wire)</font>
<font color="#00979c">const</font> <font color="#00979c">int</font> <font color="#000000">buttonPin2</font> <font color="#434f54">=</font> <font color="#000000">5</font> <font color="#000000">;</font> <font color="#434f54">// To hand controller counter-clockwise roation (Yellow Wire)</font>
<font color="#00979c">const</font> <font color="#00979c">int</font> <font color="#000000">signalPin2</font> <font color="#434f54">=</font> <font color="#000000">7</font><font color="#000000">;</font> <font color="#434f54">// output pin 7 to 2nd Arduino LED pulse pin 5</font>
<font color="#00979c">const</font> <font color="#00979c">int</font> <font color="#000000">Pin8</font> <font color="#434f54">=</font> <font color="#000000">8</font><font color="#000000">;</font> <font color="#434f54">// From hand controller starts indexing sequence. (Purple Wire)</font>
<font color="#00979c">const</font> <font color="#00979c">int</font> <font color="#000000">Pin9</font> <font color="#434f54">=</font> <font color="#000000">9</font><font color="#000000">;</font> <font color="#434f54">// Input from Hall Effect.</font>
<font color="#00979c">const</font> <font color="#00979c">int</font> <font color="#000000">distance</font> <font color="#434f54">=</font> <font color="#000000">400</font><font color="#000000">;</font> <font color="#434f54">// Make this less or more to set the final rotation place.</font>
<font color="#434f54">// A0 // Reads the POT for rotation speed (Brown Wire)</font>
<font color="#434f54">// variables will change:</font>
<font color="#00979c">int</font> <font color="#000000">buttonState1</font> <font color="#434f54">=</font> <font color="#000000">0</font><font color="#000000">;</font> <font color="#434f54">// variable for reading the pushbutton status</font>
<font color="#00979c">int</font> <font color="#000000">buttonState2</font> <font color="#434f54">=</font> <font color="#000000">0</font><font color="#000000">;</font> <font color="#434f54">// variable for reading the pushbutton status</font>
<font color="#00979c">int</font> <font color="#000000">Stop</font> <font color="#434f54">=</font> <font color="#00979c">HIGH</font><font color="#000000">;</font>
<font color="#00979c">int</font> <font color="#000000">Start</font> <font color="#434f54">=</font> <font color="#00979c">LOW</font><font color="#000000">;</font>
<font color="#00979c">int</font> <font color="#000000">Rotate</font> <font color="#434f54">=</font> <font color="#000000">0</font><font color="#000000">;</font>
<font color="#00979c">int</font> <font color="#000000">i</font> <font color="#434f54">=</font> <font color="#000000">0</font><font color="#000000">;</font>
<font color="#00979c">void</font> <font color="#5e6d03">setup</font><font color="#000000">(</font><font color="#000000">)</font> <font color="#000000">{</font>
<b><font color="#d35400">Serial</font></b><font color="#434f54">.</font><font color="#d35400">begin</font><font color="#000000">(</font><font color="#000000">9600</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">pinMode</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">OUTPUT</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// Declare pins as output:</font>
<font color="#d35400">pinMode</font><font color="#000000">(</font><font color="#000000">dirPin</font><font color="#434f54">,</font> <font color="#00979c">OUTPUT</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// Declare pins as output:</font>
<font color="#d35400">pinMode</font><font color="#000000">(</font><font color="#000000">signalPin2</font><font color="#434f54">,</font> <font color="#00979c">OUTPUT</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// Declare pins as output:</font>
<font color="#d35400">pinMode</font><font color="#000000">(</font><font color="#000000">buttonPin1</font><font color="#434f54">,</font> <font color="#00979c">INPUT</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// initialize the pushbutton pin as an input:</font>
<font color="#d35400">pinMode</font><font color="#000000">(</font><font color="#000000">buttonPin2</font><font color="#434f54">,</font> <font color="#00979c">INPUT</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// initialize the pushbutton pin as an input:</font>
<font color="#d35400">pinMode</font><font color="#000000">(</font><font color="#000000">Pin8</font><font color="#434f54">,</font> <font color="#00979c">INPUT</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// Input from hand control</font>
<font color="#d35400">pinMode</font><font color="#000000">(</font><font color="#000000">Pin9</font><font color="#434f54">,</font> <font color="#00979c">INPUT</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// Input from Hall Effect</font>
<font color="#000000">Stop</font> <font color="#434f54">=</font> <font color="#00979c">HIGH</font><font color="#000000">;</font>
<font color="#000000">Start</font> <font color="#434f54">=</font> <font color="#00979c">LOW</font><font color="#000000">;</font>
<font color="#000000">Rotate</font> <font color="#434f54">=</font> <font color="#000000">0</font><font color="#000000">;</font>
<font color="#434f54">// Set initial state of stepper motor</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// Set stepping off</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">dirPin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// Set the spinning direction CW:</font>
<font color="#d35400">digitalWrite</font> <font color="#000000">(</font><font color="#000000">signalPin2</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// Set LED pulse signal to 2nd Arduino low</font>
<font color="#000000">}</font>
<font color="#00979c">void</font> <font color="#5e6d03">loop</font><font color="#000000">(</font><font color="#000000">)</font><font color="#000000">{</font>
<font color="#434f54">//// Index Track</font>
<font color="#000000">Start</font> <font color="#434f54">=</font> <font color="#d35400">digitalRead</font> <font color="#000000">(</font><font color="#000000">Pin8</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#000000">Start</font> <font color="#434f54">==</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">{</font>
<font color="#000000">Rotate</font> <font color="#434f54">=</font> <font color="#000000">1</font><font color="#000000">;</font>
<font color="#000000">}</font>
<font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#000000">Rotate</font> <font color="#434f54">==</font> <font color="#000000">1</font><font color="#000000">)</font><font color="#000000">{</font>
<font color="#000000">Stop</font> <font color="#434f54">=</font> <font color="#d35400">digitalRead</font> <font color="#000000">(</font><font color="#000000">Pin9</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Reads Pin9, Hall Effect, It goes low when it gets to the magnet. </font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">dirPin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// Set the spinning direction Clockwise</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">delayMicroseconds</font><font color="#000000">(</font><font color="#000000">500</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">delayMicroseconds</font><font color="#000000">(</font><font color="#000000">500</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#000000">Stop</font> <font color="#434f54">==</font> <font color="#00979c">LOW</font><font color="#000000">)</font> <font color="#000000">{</font> <font color="#434f54">// Hall effect is now low and turn table stops</font>
<font color="#5e6d03">for</font><font color="#000000">(</font><font color="#00979c">int</font> <font color="#000000">i</font> <font color="#434f54">=</font> <font color="#000000">0</font><font color="#000000">;</font> <font color="#000000">i</font><font color="#434f54"><</font><font color="#000000">distance</font><font color="#000000">;</font> <font color="#000000">i</font><font color="#434f54">++</font><font color="#000000">)</font> <font color="#000000">{</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">delayMicroseconds</font><font color="#000000">(</font><font color="#000000">5000</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">delayMicroseconds</font><font color="#000000">(</font><font color="#000000">5000</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#000000">}</font>
<font color="#000000">Rotate</font> <font color="#434f54">=</font> <font color="#000000">0</font><font color="#000000">;</font>
<font color="#000000">}</font>
<font color="#000000">}</font>
<font color="#434f54">//// Read Button for clockwise rotation and rotate if it is pressed.</font>
<font color="#000000">buttonState1</font> <font color="#434f54">=</font> <font color="#d35400">digitalRead</font><font color="#000000">(</font><font color="#000000">buttonPin1</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// read the state of the forward pushbutton value:</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">signalPin2</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#000000">buttonState1</font> <font color="#434f54">==</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">{</font> <font color="#434f54">// check if the pushbutton is pressed. </font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">dirPin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font><font color="#000000">}</font> <font color="#434f54">// Set the spinning direction Clockwise</font>
<font color="#434f54">//// Read Button for counter-clockwise rotation and rotate if it is pressed. </font>
<font color="#000000">buttonState2</font> <font color="#434f54">=</font> <font color="#d35400">digitalRead</font><font color="#000000">(</font><font color="#000000">buttonPin2</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// read the state of the reverse pushbutton value:</font>
<font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#000000">buttonState2</font> <font color="#434f54">==</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">{</font> <font color="#434f54">// check if the pushbutton is pressed. </font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">dirPin</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font><font color="#000000">}</font> <font color="#434f54">// Set the spinning direction counter-clockwise.}</font>
<font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#000000">buttonState1</font> <font color="#434f54">==</font> <font color="#00979c">HIGH</font> <font color="#434f54">||</font> <font color="#000000">buttonState2</font> <font color="#434f54">==</font> <font color="#00979c">HIGH</font> <font color="#000000">)</font> <font color="#000000">{</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">signalPin2</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">// Send LED pulse signal to 2nd Arduino high</font>
<font color="#00979c">int</font> <font color="#000000">x</font> <font color="#434f54">=</font> <font color="#000000">(</font> <font color="#d35400">analogRead</font><font color="#000000">(</font><font color="#000000">A0</font><font color="#000000">)</font> <font color="#000000">)</font><font color="#000000">;</font> <font color="#434f54">//Reads the analog value on pin A0 into x. Following code determines rotation speed. Set the delays for speed desired.</font>
<font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#000000">x</font> <font color="#434f54">></font> <font color="#000000">854</font><font color="#000000">)</font> <font color="#000000">{</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">delayMicroseconds</font><font color="#000000">(</font><font color="#000000">500</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">delayMicroseconds</font><font color="#000000">(</font><font color="#000000">500</font><font color="#000000">)</font><font color="#000000">;</font><font color="#000000">}</font>
<font color="#5e6d03">else</font> <font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#000000">x</font> <font color="#434f54">></font> <font color="#000000">683</font><font color="#000000">)</font> <font color="#000000">{</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">delayMicroseconds</font><font color="#000000">(</font><font color="#000000">1000</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">delayMicroseconds</font><font color="#000000">(</font><font color="#000000">1000</font><font color="#000000">)</font><font color="#000000">;</font><font color="#000000">}</font>
<font color="#5e6d03">else</font> <font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#000000">x</font> <font color="#434f54">></font> <font color="#000000">513</font><font color="#000000">)</font> <font color="#000000">{</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">delayMicroseconds</font><font color="#000000">(</font><font color="#000000">2000</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">delayMicroseconds</font><font color="#000000">(</font><font color="#000000">2000</font><font color="#000000">)</font><font color="#000000">;</font><font color="#000000">}</font>
<font color="#5e6d03">else</font> <font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#000000">x</font> <font color="#434f54">></font> <font color="#000000">400</font><font color="#000000">)</font> <font color="#000000">{</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">delayMicroseconds</font><font color="#000000">(</font><font color="#000000">3000</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">delayMicroseconds</font><font color="#000000">(</font><font color="#000000">3000</font><font color="#000000">)</font><font color="#000000">;</font><font color="#000000">}</font>
<font color="#5e6d03">else</font> <font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#000000">x</font> <font color="#434f54">></font> <font color="#000000">300</font><font color="#000000">)</font> <font color="#000000">{</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">delayMicroseconds</font><font color="#000000">(</font><font color="#000000">5000</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">delayMicroseconds</font><font color="#000000">(</font><font color="#000000">5000</font><font color="#000000">)</font><font color="#000000">;</font><font color="#000000">}</font>
<font color="#5e6d03">else</font> <font color="#5e6d03">if</font> <font color="#000000">(</font><font color="#000000">x</font> <font color="#434f54">></font> <font color="#000000">100</font><font color="#000000">)</font> <font color="#000000">{</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">HIGH</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">delayMicroseconds</font><font color="#000000">(</font><font color="#000000">16000</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font>
<font color="#d35400">delayMicroseconds</font><font color="#000000">(</font><font color="#000000">16000</font><font color="#000000">)</font><font color="#000000">;</font><font color="#000000">}</font>
<font color="#5e6d03">else</font> <font color="#000000">{</font><font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">stepPin</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font><font color="#000000">}</font><font color="#000000">}</font>
<font color="#5e6d03">else</font> <font color="#000000">{</font> <font color="#d35400">digitalWrite</font><font color="#000000">(</font><font color="#000000">signalPin2</font><font color="#434f54">,</font> <font color="#00979c">LOW</font><font color="#000000">)</font><font color="#000000">;</font> <font color="#000000">}</font> <font color="#434f54">/// Skip past the rotation speed</font>
<font color="#000000">}</font>
</pre>