SparkFun Forums 

Where electronics enthusiasts find answers.

General project discussion / help
Did you make a robotic coffee pot which implements HTCPCP and decafs unauthorized users? Show it off here!
By jndipworm
#170766
Hello all, new here. I've been messing around with arduino for about a year now and have a lot to learn. I have a 12v NEMA17 4 wire stepper that I'm driving with the BED. It works great. For my project I need to 'release' the stepper so I can turn the motor by hand from time to time. I found the following code on youtube from NYC CNC which was a big help. I was looking for some help on how the code should look. Thank you in advance.
Code: Select all
int Fire; //fire button
int Ttable; //turn table button

int feedspeed = 400; //feed stpper speed less is faster
int totalsteps = 2000; //total number of steps set for distance
int stepcount = 0; //
int Firestate = 0; //
int Ttablestate = 0; //

void setup()
{
  pinMode(4,INPUT); // fire button pin
  pinMode(5,INPUT); // turn table button pin
  pinMode(10,OUTPUT); // stepper direction pin
  pinMode(11,OUTPUT); //stepper step pin
  digitalWrite(10,LOW); //
  digitalWrite(11,LOW); //
}

void loop() {
  Fire = digitalRead(4); //reads pin 4
  Ttable = digitalRead(5); //reads pin 5
  
    if (Ttable == LOW && Ttablestate == 0)
    {
      while(stepcount < totalsteps)
      {
      digitalWrite(10,LOW);
      digitalWrite(11,HIGH);
      delayMicroseconds(feedspeed);
      digitalWrite(11,LOW);
      delayMicroseconds(feedspeed);
      stepcount++;
      }
      stepcount = 0;
      Ttablestate = 1;
    }
    
    if (Fire == LOW && Firestate == 0)
    {
      while(stepcount < totalsteps)
      {
      digitalWrite(10,HIGH);
      digitalWrite(11,HIGH);
      delayMicroseconds(feedspeed);
      digitalWrite(11,LOW);
      delayMicroseconds(feedspeed);
      stepcount++;
      }
      stepcount = 0;
      Firestate = 1;
    }
    
    
  if(Fire == HIGH && Firestate == 1)
    {
      Firestate = 0;
    }
   
  if(Ttable == HIGH && Ttablestate == 1)
    {
      Ttablestate = 0;
    }
    
}
User avatar
By Ross Robotics
#170801
Please specify what you mean by "how the code should look?"
By jndipworm
#170818
I have an Adafruit Motor Shield and the code to release the motor is motor.run(RELEASE); . I was hoping the BED had similar code that would allow me to 'release' the motor so I could turn it by hand if needed and to keep the motor from building heat while sitting idle.
User avatar
By Ross Robotics
#170833
I use the AccelStepper library for controlling my BED. This library has a release() function. How it achieves this is it sets all the pins to 0.
Code: Select all
void AF_Stepper::release(void) {
  if (steppernum == 1) {
    latch_state &= ~_BV(MOTOR1_A) & ~_BV(MOTOR1_B) &
      ~_BV(MOTOR2_A) & ~_BV(MOTOR2_B); // all motor pins to 0
    MC.latch_tx();
  } else if (steppernum == 2) {
    latch_state &= ~_BV(MOTOR3_A) & ~_BV(MOTOR3_B) &
      ~_BV(MOTOR4_A) & ~_BV(MOTOR4_B); // all motor pins to 0
    MC.latch_tx();
  }
}
By jndipworm
#170856
Thanks. I can't say I understand any of this code. I've never seen this before, but I haven't been working with code for very long. It would help me understand more if I could see all the code. At least I know it can be done, I'll just have to figure it out.
User avatar
By Ross Robotics
#170875
jndipworm wrote: It would help me understand more if I could see all the code.
I gave you the link to the library..
By jndipworm
#170899
I had the library. I downloaded it when I bought the Adafruit Motor Shield. I looked thru all the accelstepper examples but saw nothing that looked even remotely close to your code. I tried to find online examples of your code but found nothing. I stated in my first post that I was just learning how to write and understand code. I don't want someone to give me the 'answers' to my questions, but I am going to need a little help as to why it works the way it does and just where in the code it goes. This is so frustrating. I'm sorry but it is easy when you know what you're doing and very frustrating when you don't. I don't give up easily and I will figure it out. I will take what you showed me and I will learn how to write it into my code. I do appreciate the help.
By Mee_n_Mac
#170903
I've not looked at the BED code but if you want to turn the motor shaft by hand, you'll need to turn off the driver. The heart of the BED is the Allegro A4988 IC and it has an enable (active low) pin, that can be controlled by the micro. See if the code has an enable/disable function mentioned, that should release the shaft, assuming the enable pin is connected to the micro. That pin is pin1 on both the JP6 and JP13 headers. You can see them in the upper left corner of the pic below.
Image
If the BED code doesn't explicitly call out an enable/disable function and that pin is connected to your Arduino, then there must be some code somewhere that does a digitalWrite(pin, LOW) to that pin. When you want a release, do a digitalWrite(pin, HIGH) but remember to re-enable the BED with a LOW when you're done turning the shaft manually.
User avatar
By Ross Robotics
#170913
The piece of code that I posted was from the library's core files. You can call the release() function in your sketch to turn off the BED when you need to. I did look through their examples and was surprised that they didn't show this function. I actually had to read through the core files to find it. I find it also surprising that the writer of the library didn't give any clues this function existed in any of their documentation.

But Mac does bring up a good point. His method would work just as nice.
By jndipworm
#170951
Thanks guys. codlink, I checked your link in the previous posts and it takes me to the Adafruit Motor Sheild downloads which I have that as well. That's what had me confused, I didn't see your code in accelstepper.cpp it's in afmotor.cpp. I'm assuming these are what you refer to as core files. I will start looking thru these files for help. Thanks for your patients.