How to control Stepper Motor using a Joystick ( Easy Driver )




Arduino Sketch


//Declare pin functions on Redboard
#define stp 2
#define dir 3
#define MS1 4
#define MS2 5
#define EN  6

//Declare variables for functions
char user_input;
int x;
int y;
int state;

int joyX = A0;
int joyVal;


void setup() {
 
  pinMode(stp, OUTPUT);
  pinMode(dir, OUTPUT);
  pinMode(MS1, OUTPUT);
  pinMode(MS2, OUTPUT);
  pinMode(EN, OUTPUT);

  digitalWrite(stp, LOW);
  digitalWrite(dir, LOW);
  digitalWrite(MS1, LOW);
  digitalWrite(MS2, LOW);
  digitalWrite(EN, HIGH);
 
  Serial.begin(9600); //Open Serial connection for debugging
 
}

//Main loop
void loop() {
 
   while( (analogRead(joyX) == 0 ) || (analogRead(joyX) > 1000 )){
           joyVal = analogRead(joyX);
           digitalWrite(EN, LOW);
   if( joyVal == 0){
           
      digitalWrite(dir, LOW);
 
   }
   else if( joyVal > 1000){
         
      digitalWrite(dir, HIGH);
   
   }
      digitalWrite(stp,HIGH); //Trigger one step forward
      delay(1);
      digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
      delay(1);
 
   } // while loop

  digitalWrite(EN, HIGH);
}

ความคิดเห็น