BSMD PhaseI Code
Jump to navigation
Jump to search
The following is the Arduino code from Bipolar Step Motor Driver Phase I:
/* Analog Control of EasyDriver Stepper Motor Board The circuit: * Potentiometer attached to analog input 0 * Direction connected to digital pin 2 * Step connected to digital pin 3 Written by Chris Cember October 7, 2009 */ // Global Configuration int potPin = 0; // analog pin for potentiometer int dirPin = 2; // digital pin for direction control int stepPin = 3; // digital pin for step control int stepPulseSize = 1; // pulse length sent to step pin, in milliseconds // Program Variables int potValue = 0; // var to store potentiometer value boolean dir = 0; // var to store direction int loopDelay = 0; // var to store calculated delay time // Required Setup Function (init) void setup() { // declare step and direction pins as outputs pinMode(dirPin, OUTPUT); pinMode(stepPin, OUTPUT); } // Required Loop Function (main) void loop() { // read potentiometer value // bit shift right twice for 0-255 value (remove chatter) potValue = analogRead(potPin); potValue /= 4; // determine loopDelay from distance of potValue from center; // deadband from 100 to 170 loopDelay = 0; if (potValue < 100) { dir = HIGH; loopDelay = (int)(8.6 * potValue); if (loopDelay <= 0) loopDelay = 1; } if (potValue > 170) { dir = LOW; loopDelay = (int)(-11.7 * potValue + 2980); if (loopDelay <= 0) loopDelay = 1; } // set direction pin digitalWrite(dirPin, dir); // if not in deadband, pulse step pin if (loopDelay != 0) { digitalWrite(stepPin, HIGH); digitalWrite(stepPin, LOW); } // delay entire loop by calculated time, thus controlling motor step speed delayMicroseconds(25 * loopDelay); }