Advertisement
AlexanderNorup

Arduino No Libary Stepper Motor

Oct 2nd, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. //For motor: Nema 17 Bipolar Stepper 17HS13-0404S1
  2. int motorPin1 = 8; //BLÅ (Blue)
  3. int motorPin2 = 9; //SORT (Black)
  4. int motorPin3 = 10; //GRØN (Green)
  5. int motorPin4 = 11; //RØD (Red)
  6. int delayTime = 5;
  7.  
  8. void setup() {
  9.   Serial.begin(9600);
  10.   pinMode(motorPin1, OUTPUT);
  11.   pinMode(motorPin2, OUTPUT);
  12.   pinMode(motorPin3, OUTPUT);
  13.   pinMode(motorPin4, OUTPUT);
  14. }
  15.  
  16. int stepmode = 0;
  17.  
  18. void steponce(){
  19.   switch(stepmode){
  20.     case 0:
  21.       digitalWrite(motorPin4, HIGH);
  22.       digitalWrite(motorPin3, LOW);
  23.       digitalWrite(motorPin2, LOW);
  24.       digitalWrite(motorPin1, LOW);
  25.       stepmode++;
  26.       break;
  27.     case 1:
  28.       digitalWrite(motorPin4, LOW);
  29.       digitalWrite(motorPin3, HIGH);
  30.       digitalWrite(motorPin2, LOW);
  31.       digitalWrite(motorPin1, LOW);
  32.       stepmode++;
  33.       break;
  34.     case 2:
  35.       digitalWrite(motorPin4, LOW);
  36.       digitalWrite(motorPin3, LOW);
  37.       digitalWrite(motorPin2, HIGH);
  38.       digitalWrite(motorPin1, LOW);
  39.       stepmode++;
  40.       break;
  41.     default:
  42.       digitalWrite(motorPin4, LOW);
  43.       digitalWrite(motorPin3, LOW);
  44.       digitalWrite(motorPin2, LOW);
  45.       digitalWrite(motorPin1, HIGH);
  46.       stepmode = 0;
  47.       break;
  48.   }
  49.   delay(delayTime);
  50.   //Serial.println("Stepped once");
  51. }
  52.  
  53. void steptimes(int n){
  54.   for(int i = 0; i < n; i++){
  55.     steponce();
  56.   }
  57.   Serial.print("Stepped ");
  58.   Serial.print(n);
  59.   Serial.println(" times");
  60. }
  61.  
  62. void loop() {
  63.   steptimes(100);
  64.   delay(1000);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement