Advertisement
Guest User

ULN2003A

a guest
Nov 25th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 512 moves is full spin
  2. int stepMotor_moves = 256;
  3. // How fast magnets will change in ms
  4. int stepMotor_delay = 5;
  5. // pins as IN1, IN2, IN3, IN4
  6. int stepMotor_pins[] = {8, 9, 10, 11};
  7.  
  8. void setup() {
  9.   // Arduino pin config...
  10.   pinMode(8, OUTPUT);
  11.   pinMode(9, OUTPUT);
  12.   pinMode(10, OUTPUT);
  13.   pinMode(11, OUTPUT);
  14.   // Run functons once
  15.   stepMotor_moveForward();
  16.   stepMotor_moveBackward();
  17. }
  18.  
  19. // Arduino loop...
  20. void loop() {
  21. }
  22.  
  23. void stepMotor_resetMagnets() {
  24.   digitalWrite(stepMotor_pins[0], LOW);
  25.   digitalWrite(stepMotor_pins[1], LOW);
  26.   digitalWrite(stepMotor_pins[2], LOW);
  27.   digitalWrite(stepMotor_pins[3], LOW);
  28. }
  29.  
  30. void stepMotor_moveForward() {
  31.   stepMotor_resetMagnets(); // reset magnets before looping step motor
  32.   for (int j = 0; j < stepMotor_moves; j++) {
  33.     // Forward - power on magnets in 1, 2, 3, 4 config with delay every change
  34.     for (int i = 0; i < 4; i++) {
  35.       if (i == 0) {
  36.         digitalWrite(stepMotor_pins[3], LOW);
  37.         digitalWrite(stepMotor_pins[0], HIGH);
  38.       } else if (i == 1) {
  39.         digitalWrite(stepMotor_pins[0], LOW);
  40.         digitalWrite(stepMotor_pins[1], HIGH);
  41.       } else if (i == 2) {
  42.         digitalWrite(stepMotor_pins[1], LOW);
  43.         digitalWrite(stepMotor_pins[2], HIGH);
  44.       } else if (i == 3) {
  45.         digitalWrite(stepMotor_pins[2], LOW);
  46.         digitalWrite(stepMotor_pins[3], HIGH);
  47.       }
  48.       delay(stepMotor_delay);
  49.     }
  50.   }
  51.   stepMotor_resetMagnets(); // release magnets
  52. }
  53.  
  54. void stepMotor_moveBackward() {
  55.   stepMotor_resetMagnets(); // reset magnets before looping step motor
  56.   for (int j = 0; j < stepMotor_moves; j++) {
  57.     // Forward - power on magnets in 4, 3, 2, 1 config with delay every change
  58.     for (int i = 0; i < 4; i++) {
  59.       if (i == 0) {
  60.         digitalWrite(stepMotor_pins[0], LOW);
  61.         digitalWrite(stepMotor_pins[3], HIGH);
  62.       } else if (i == 1) {
  63.         digitalWrite(stepMotor_pins[3], LOW);
  64.         digitalWrite(stepMotor_pins[2], HIGH);
  65.       } else if (i == 2) {
  66.         digitalWrite(stepMotor_pins[2], LOW);
  67.         digitalWrite(stepMotor_pins[1], HIGH);
  68.       } else if (i == 3) {
  69.         digitalWrite(stepMotor_pins[1], LOW);
  70.         digitalWrite(stepMotor_pins[0], HIGH);
  71.       }
  72.       delay(stepMotor_delay);
  73.     }
  74.   }
  75.   stepMotor_resetMagnets(); // release magnets
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement