Go Down

Topic: Adafruit Motor Shield v2 Stepper Follow Encoder for DTG Base open sauce  (Read 1 time) previous topic - next topic

Mydakota


I have created the basics for this code to work but if others want to use this code please share your changes so others can use and help grow the code.

this code.

1. Printer turns encoder wheel / Paper feed shaft
2. Dc Motor will follow paper shaft (Timings will need playing with)
3. Uses the Adafruit Motor Shield

feel free to add more code for the community to use I myself are going down a different path for  driving my base with out the Adafruit Motor Shield v2. but for others who want to use i found it hard to get this shield to work so enjoy the basics.
If i get some time I will come back to this project but for now happy playing



Code: [Select]
// Madmax/Mydakota Motor Shield Mod to Follow encoder


#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

    int state, prevstate = 500, count = 500;
    int nextEncoderState[4] = { 2, 0, 3, 1 };
    int prevEncoderState[4] = { 1, 3, 0, 2 };


    // Number of steps per turn of encoder
    int forward=1;
    int backward=1;

    //Used to calculate direction
    int direct=0;

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
    // Connect a stepper motor
    // to motor port #1 #2 (M3 and M4)
    Adafruit_StepperMotor *myMotor = AFMS.getStepper(48, 1);

    void setup() {
     AFMS.begin();
    myMotor->setSpeed(10);
 

      pinMode(10, INPUT);
      pinMode(2, INPUT);
      pinMode(9, OUTPUT);
      digitalWrite(9, LOW);
      digitalWrite(2, HIGH);
      digitalWrite(10, HIGH);
      Serial.begin(115200);

    }

    void loop() {
       
       state = (digitalRead(10) << 1) | digitalRead(2);
      if (state != prevstate) {
        if (state == nextEncoderState[prevstate]) {
           count++;
           direct= count-1;
           
          if (count > direct){
         
          myMotor->step(FORWARD, SINGLE);
           
           
       Serial.println("________forward____________________");// used for testing purpose
       direct=0;
     
       }
           
        } else if (state == prevEncoderState[prevstate]) {
           count--;
           direct= count+1;
           
            if (count < direct){
       
           myMotor->step(100, BACKWARD, SINGLE);
           
           Serial.println("__________backward_________________");// used for testing purpose
          direct=0;
         
        }
        }
        Serial.print("count =");
        Serial.println(count, DEC);
        Serial.println(direct, DEC);
        Serial.print("prev count =");
       
     
        prevstate = state;
     
        }


    }

Go Up