USB host shield with stepper motor
Connecting a stepper to USB host shield!
Code:
//declare pins int potPin = 1; int Step_X = 13; int Dir_X = 12; int Enable_X = 8;
//declare values
int Speed_X = 0; //step speed (delay between steps)
int val= 0;
int j = 0;
void setup() { pinMode(Step_X, OUTPUT); pinMode(Dir_X, OUTPUT); pinMode(Enable_X, OUTPUT);
//Serial.begin(9600); // note that serial comm can be used to debug // but it will slow down the code and slow down the stepper motor alot //(and be confusing to me) }
void loop() {
val = analogRead(potPin); // read the value from the sensor
j = val - 517; // 517 is center positions - how far from center? j = abs(j); //absolute value Speed_X = 70000/j; //This math inverts the value and scales as needed //(value found through trial and error) // The delay between steps will determine the speed of the motor // So, delay up = speed down
if (val >= 520){ digitalWrite(Enable_X,LOW); // enable digitalWrite(Dir_X, HIGH); // Set direction digitalWrite(Step_X,HIGH); delayMicroseconds(2); digitalWrite(Step_X,LOW); delayMicroseconds(Speed_X);
} if (val <= 510) { digitalWrite(Enable_X,LOW);// enable digitalWrite(Dir_X, LOW); // Other direction digitalWrite(Step_X,HIGH); delayMicroseconds(2); digitalWrite(Step_X,LOW); delayMicroseconds(Speed_X); }
if (val <=520 && val >= 510) {
digitalWrite(Enable_X,HIGH); // disable the stepper motor if the joystic is in the center
}
}