Friday, June 10, 2016

Moving the Motor

To control the stepper motor I am using an arduino uno, an adafruit motor shield and a potentiometer. The motor has 9 pins, I connected 3 and 5 to M4 port on the motor shield and pin 4 to the M3 port, I am not sure why this configuration works but it does. I then have the middle wire of the potentiometer connected to A0 and the other wires connected to ground and 5V. The motor shield is connected to a protoboard which is supplying it with about 8V.



I wrote two different codes to control the motor, one uses if statements and the other switch/case statements. They both do the same thing but a little differently. Both codes can be used interchangeably. I will put both codes in the comments section. The library AFMotor.h can be found on the adafruit website here as well as examples of the library in use. 

The next step is to find a way to tell the motor to move to a certain angle and then do a sweep between angles.  

2 comments:

  1. #include
    //Motor will spin counter clockwise when the pot is turned to the left
    //and spin clockwise when the pot is turned the right
    //there is a deadzone is the middle where is does not spin

    //On motor connect pin 5 and 3 to M4 and pin 4 to M3
    //Use potentiometer with one wire in ground, the other in 5V and the middle in analog pin 0


    AF_Stepper motor(20, 2);
    int potpin = 0;
    int val = 0;


    void setup() {
    Serial.begin(9600);
    Serial.println("stepper test!");
    motor.setSpeed(100);
    }

    void loop() {
    val = analogRead(potpin);
    Serial.println(val);
    if (val > 680)
    {
    motor.step(1, FORWARD, INTERLEAVE);
    }
    else if (val < 340)
    {
    motor.step(1, BACKWARD, INTERLEAVE);
    }
    else {
    motor.step(0, FORWARD, INTERLEAVE);
    }

    }

    ReplyDelete
  2. #include
    //On motor,conect pin 5 and 3 to M4 and pin 4 to M3
    //Use potentiometer with one wire in ground, the other in 5V and the middle in analog pin 0

    //Motor will spin counter clockwise when the pot is all the way to the right
    //and clockwise when the pot is turned all the way to the left
    //motor will not spin when the pot is in the middle

    AF_Stepper motor(20, 2);
    int potpin = 0;
    int val = 0;


    void setup() {
    Serial.begin(9600);
    Serial.println("stepper test!");
    motor.setSpeed(100);
    }

    void loop() {
    val = analogRead(potpin);
    Serial.println(val);
    switch (val){
    case 1023:
    motor.step(1,FORWARD, INTERLEAVE);
    break;
    case 0:
    motor.step(1,BACKWARD, INTERLEAVE);
    break;
    }


    }

    ReplyDelete