Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 512 moves is full spin
- int stepMotor_moves = 256;
- // How fast magnets will change in ms
- int stepMotor_delay = 5;
- // pins as IN1, IN2, IN3, IN4
- int stepMotor_pins[] = {8, 9, 10, 11};
- void setup() {
- // Arduino pin config...
- pinMode(8, OUTPUT);
- pinMode(9, OUTPUT);
- pinMode(10, OUTPUT);
- pinMode(11, OUTPUT);
- // Run functons once
- stepMotor_moveForward();
- stepMotor_moveBackward();
- }
- // Arduino loop...
- void loop() {
- }
- void stepMotor_resetMagnets() {
- digitalWrite(stepMotor_pins[0], LOW);
- digitalWrite(stepMotor_pins[1], LOW);
- digitalWrite(stepMotor_pins[2], LOW);
- digitalWrite(stepMotor_pins[3], LOW);
- }
- void stepMotor_moveForward() {
- stepMotor_resetMagnets(); // reset magnets before looping step motor
- for (int j = 0; j < stepMotor_moves; j++) {
- // Forward - power on magnets in 1, 2, 3, 4 config with delay every change
- for (int i = 0; i < 4; i++) {
- if (i == 0) {
- digitalWrite(stepMotor_pins[3], LOW);
- digitalWrite(stepMotor_pins[0], HIGH);
- } else if (i == 1) {
- digitalWrite(stepMotor_pins[0], LOW);
- digitalWrite(stepMotor_pins[1], HIGH);
- } else if (i == 2) {
- digitalWrite(stepMotor_pins[1], LOW);
- digitalWrite(stepMotor_pins[2], HIGH);
- } else if (i == 3) {
- digitalWrite(stepMotor_pins[2], LOW);
- digitalWrite(stepMotor_pins[3], HIGH);
- }
- delay(stepMotor_delay);
- }
- }
- stepMotor_resetMagnets(); // release magnets
- }
- void stepMotor_moveBackward() {
- stepMotor_resetMagnets(); // reset magnets before looping step motor
- for (int j = 0; j < stepMotor_moves; j++) {
- // Forward - power on magnets in 4, 3, 2, 1 config with delay every change
- for (int i = 0; i < 4; i++) {
- if (i == 0) {
- digitalWrite(stepMotor_pins[0], LOW);
- digitalWrite(stepMotor_pins[3], HIGH);
- } else if (i == 1) {
- digitalWrite(stepMotor_pins[3], LOW);
- digitalWrite(stepMotor_pins[2], HIGH);
- } else if (i == 2) {
- digitalWrite(stepMotor_pins[2], LOW);
- digitalWrite(stepMotor_pins[1], HIGH);
- } else if (i == 3) {
- digitalWrite(stepMotor_pins[1], LOW);
- digitalWrite(stepMotor_pins[0], HIGH);
- }
- delay(stepMotor_delay);
- }
- }
- stepMotor_resetMagnets(); // release magnets
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement