Difference between revisions of "BSMD PhaseI Code"

From Hive13 Wiki
Jump to navigation Jump to search
(Arduino code for Phase I experiment)
 
m
Line 6: Line 6:
 
  The circuit:
 
  The circuit:
 
  * Potentiometer attached to analog input 0
 
  * Potentiometer attached to analog input 0
  * Direction connected to digital pin 0
+
  * Direction connected to digital pin 2
  * Step connected to digital pin 1
+
  * Step connected to digital pin 3
 
   
 
   
 
  Written by Chris Cember
 
  Written by Chris Cember

Revision as of 13:03, 21 October 2009

/*

 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); 
            
}