Need some help with code using the v1 Motorsheild

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
SolderMeBad
 
Posts: 4
Joined: Wed Dec 10, 2014 11:44 pm

Need some help with code using the v1 Motorsheild

Post by SolderMeBad »

Attached is my current working sketch... everything is working fine, but I am having trouble expanding additional functionality.

As it sits, I have an adafruit stepper which is controlled by a rotary encoder... this functions great and the sketch seems to be working great. The rotary encoder allows me to jog 1 step at a time on the stepper... so far so good.

I wanted to add about 2-3 momentary switches on the digital inputs so I can have the stepper run continuously at a faster speed until it is depressed. Additional switches are either reverse direction or have two different speeds I can run so I don't have to keep jogging the bulk of the travel.

Can someone help me with the formatting of the code... I gave it a couple tries and both broke my sketch every time.

Attached is my WORKING sketch without any code for the switches I want to use.
Attachments
sketch.txt
(1.55 KiB) Downloaded 109 times

User avatar
SolderMeBad
 
Posts: 4
Joined: Wed Dec 10, 2014 11:44 pm

Re: Need some help with code using the v1 Motorsheild

Post by SolderMeBad »

Code: Select all

#include <AccelStepper.h>
#include <AFMotor.h>

#define encoderPinA  2
#define encoderPinB  3

int encoderPos = 0;
int encoderPinALast = LOW;
int encoderPinBLast = LOW;
int n = LOW;

AF_Stepper motor2(200, 2);

// wrappers for the motor! SINGLE is default, can also use INTERLEAVE DOUBLE MICROSTEP
void forwardstep2() {  
  motor2.onestep(FORWARD, SINGLE);
}
void backwardstep2() {  
  motor2.onestep(BACKWARD, SINGLE);
}

// Motor shield has two motor ports, now we'll wrap them in an AccelStepper object
AccelStepper stepper2(forwardstep2, backwardstep2);

void setup()
{  
    stepper2.setMaxSpeed(500.0);
    stepper2.setAcceleration(100.0);
    stepper2.moveTo(0);
    
    pinMode(encoderPinA, INPUT_PULLUP); 
    pinMode(encoderPinB, INPUT_PULLUP);
}

void loop()
{  
    n = digitalRead(encoderPinA);
   if ((encoderPinALast != n )) {
     if (((n == HIGH) && (digitalRead(encoderPinB) == LOW)) || 
         ((n == LOW) && (digitalRead(encoderPinB) == HIGH))) {
       motor2.step(1, BACKWARD, INTERLEAVE);
     } else {
       motor2.step(1, FORWARD, INTERLEAVE);
     }
   } else {
     n = digitalRead(encoderPinB);
     if ((encoderPinBLast != n )) {
       if (((n == HIGH) && (digitalRead(encoderPinA) == LOW)) || 
           ((n == LOW) && (digitalRead(encoderPinA) == HIGH))) {
         motor2.step(1, FORWARD, INTERLEAVE);
       } else {
         motor2.step(1, BACKWARD, INTERLEAVE);
       }
     }
   }
   encoderPinALast = digitalRead(encoderPinA);
   encoderPinBLast = digitalRead(encoderPinB);
    
}

User avatar
adafruit_support_bill
 
Posts: 88139
Joined: Sat Feb 07, 2009 10:11 am

Re: Need some help with code using the v1 Motorsheild

Post by adafruit_support_bill »

Attached is my WORKING sketch without any code for the switches I want to use.
Post photos of your system showing how the switches are connected.

User avatar
SolderMeBad
 
Posts: 4
Joined: Wed Dec 10, 2014 11:44 pm

Re: Need some help with code using the v1 Motorsheild

Post by SolderMeBad »

Took me a while since it was the first time using this software but I think this should help... see attached file.

I will post the code later after I give it another shot, but I am not sure how to add additional code to the void setup and loop to expand control of the stepper over the existing code.

It must be formatting, but every time I try to define to switch, the sketch for the encoder stops working.
Attachments
EncoderStepperProject_bb.jpg
EncoderStepperProject_bb.jpg (865.92 KiB) Viewed 150 times

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”