Accel Library problem Solved!

Hello

I'm having difficulty using the Accel stepper library. Using plain code I can get my motor spinning just fine so I know it's hooked up right. I just can't get Accel lub to work for me! I've tried the examples and I don't even get a humm.

Here is the example I'm using (slightly modified).

#include <AccelStepper.h>

AccelStepper stepper1(1, 2, 3); // Define stepper and the pins we will use

void setup()
{
int enPin = 4; // Enable Pin
pinMode(enPin, OUTPUT); // set enable pin to output
digitalWrite(enPin, HIGH); // turn on enable
stepper1.setMaxSpeed(11000.0); // set speed
stepper1.setAcceleration(10000.0); // set accel
stepper1.moveTo(6000); // set position
stepper1.enableOutputs();

}

void loop()
{
// Change direction at the limits
if (stepper1.distanceToGo() == 0)
stepper1.moveTo(-stepper1.currentPosition());

stepper1.run();

}

And here is the working plain code.

// Set up the Pins
int dirPin = 2;
int stepperPin = 3;
int enPin = 4;
int ledPin = 9;

void setup() {
pinMode(dirPin, OUTPUT);
pinMode(stepperPin, OUTPUT);
pinMode(enPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}

void step(boolean dir,int steps){
digitalWrite(dirPin,dir);
delay(50);
for(int i=0;i<steps;i++){
digitalWrite(stepperPin, HIGH);
delayMicroseconds(100);
digitalWrite(stepperPin, LOW);
delayMicroseconds(100);
}
}

void loop(){
digitalWrite(ledPin, HIGH); // set the LED on
step(true,16005);
delay(500);
step(false,1600
5);
delay(500);
}

Any help would be appreciated.

Submicro

AccelStepper(1, Step = 2, Direction = 3);

int dirPin = 2;
int stepperPin = 3;

Notice a difference? :slight_smile:

Wow do I feel stupid :roll_eyes: Thanks I couldn't find any description of how to use it with 2 pins instead of 4. I was just guessing! I'll try switching them.

Submicro

Tried switching them around and still nothing, not even a buzz, twitch, or stutter..... :~

SubMicro:
Tried switching them around and still nothing, not even a buzz, twitch, or stutter..... :~

Is it possible that the Enable pin is Active-LOW? I notice that in the code that works you never actually write to the enPin.

Is it possible that the Enable pin is Active-LOW? I notice that in the code that works you never actually write to the enPin.

I tried the enPin low as well still doesn't work, but now the motor humms and the rotor is locked! I'm sure it's something obvious, you know starring me in the face kind of thing. I just cannot figure it out!

Submicro

Remember that starring me in the face comment? Well I rechecked the code, and I forgot to save changes made when I swapped the step & dir pins..... DOH! Now with the pins swapped and the enable set for active low, taaadaaa!

Thanks a bunch for your help John, but I can't help thinking I should been able to figure this one out! I guess I had a case of temporary dumb azzz!

Thanks again
Submicro

P.S. Shouldn't I be able to set enable with the library? There are no examples of it!

I believe you can tell the library which pin is the Enable Pin. Something like stepper1.setEnablePin(enPin);

Yes I got that but I haven't figured how to get the setPinsInverted function working. To bring the enablePin low.

Here is the modified code that works, but I've commented out a few lines that I shouldn't need if I can figure out how to use this library properly. If I uncomment those lines then the code works.

#include <AccelStepper.h>



AccelStepper stepper1(1, 3, 2);  // Define stepper and the pins we will use

void setup()
{  
    //int enPin = 4; // Enable Pin  
    //pinMode(enPin, OUTPUT); // set enable pin to output
    //digitalWrite(enPin, LOW); // turn on enable
    stepper1.setMaxSpeed(20000.0); // set speed
    stepper1.setAcceleration(10000.0); // set accel
    stepper1.moveTo(600); // set position
    stepper1.setEnablePin(4);
    stepper1.enableOutputs();
    
}

void loop()
{
    // Change direction at the limits
    if (stepper1.distanceToGo() == 0)
      stepper1.moveTo(-stepper1.currentPosition());
    
    stepper1.run();
   
}

submicro

SubMicro:
Yes I got that but I haven't figured how to get the setPinsInverted function working. To bring the enablePin low.

If you need to invert the Enable pin:

 	setPinsInverted (/*direction*/ false, /*step*/ false, /*enable*/ true);

I see I was trying to do just the ensble pin. So even if I'm not inverting the other pins, I have to include them in the function call.
I'llgive that a try. I guess I'm getting the syntax wrong because it's not working.

setPinsInverted(direction false, step false, enable true);

Thanks

SubMicro:
I see I was trying to do just the ensble pin. So even if I'm not inverting the other pins, I have to include them in the function call.
I'llgive that a try. I guess I'm getting the syntax wrong because it's not working.

setPinsInverted(direction false, step false, enable true);

Maybe because you took out the comment marks. What I wrote was:

setPinsInverted (/*direction*/ false, /*step*/ false, /*enable*/ true);

Ok I put it just as you wrote it and got this error

ConstantSpeed_test.cpp: In function ‘void setup()’:
ConstantSpeed_test.cpp:19:74: error: ‘setPinsInverted’ was not declared in this scope

Sorry I'm so bad at this, it's been awhile since I tried Arduino.

SubMicro

Try:

stepper1.setPinsInverted(false, false, true);

Hello I went to the Accellstepper google group and they gave me an example of how to use the function. Now I'm good to go :grin:
It was what you showed me but 4 some reason it didn't work, but this time it did.
In case your interested here is the working test code.

#include <AccelStepper.h>
AccelStepper stepper1(1, 3, 2);  // Define stepper and the pins we will use

void setup()
{  
    stepper1.setMaxSpeed(20000.0); // set speed
    stepper1.setAcceleration(10000.0); // set accel
    stepper1.moveTo(1160000); // set position
    stepper1.setEnablePin(4);
    stepper1.setPinsInverted(false, false, true);
    stepper1.enableOutputs();
    
}

void loop()
{
    // Change direction at the limits
    if (stepper1.distanceToGo() == 0)
      stepper1.moveTo(-stepper1.currentPosition());
    
    stepper1.run();
   
}

Well...I have the following code in Arduino 1.0.5:

// ConstantSpeed.pde
// -*- mode: C++ -*-
//
// Shows how to run AccelStepper in the simplest,
// fixed speed mode with no accelerations
/// \author  Mike McCauley (mikem@airspayce.com)
// Copyright (C) 2009 Mike McCauley
// $Id: ConstantSpeed.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $

#include <AccelStepper.h>

AccelStepper stepper(AccelStepper::DRIVER, 2, 3); // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

void setup()
{  
   stepper.setEnablePin( 4 );
   stepper.setPinsInverted( false, false, true);
   stepper.setMaxSpeed(1000);
   stepper.setSpeed(200);	
}

void loop()
{  
   stepper.runSpeed();
}

And this is what I get when I compile:

ConstantSpeed.pde: In function ‘void setup()’:
ConstantSpeed:17: error: call of overloaded ‘setPinsInverted(int, int, int)’ is ambiguous
/home/mac/arduino-1.0.5/libraries/AccelStepper/AccelStepper.h:389: note: candidates are: void AccelStepper::setPinsInverted(bool, bool, bool)
/home/mac/arduino-1.0.5/libraries/AccelStepper/AccelStepper.h:397: note: void AccelStepper::setPinsInverted(bool, bool, bool, bool, bool)

   stepper.setPinsInverted( (bool)false, (bool)false, (bool)true);

You have to tell the compiler that true and false ARE bools.

stepper.setPinsInverted( (bool)false, (bool)false, (bool)true);

The above produces the same error.

I did get it to work by doing the following cludge:

In AccelStepper.h I changed the declaration to:

 void    setPinsInverted(bool directionInvert = false, bool stepInvert = false, bool enable = false, int dummy = 0);

In AccelStepper.cpp I changed the definition to:

 void AccelStepper::setPinsInverted(bool directionInvert, bool stepInvert, bool enableInvert, int dummy)

And my call to:

   stepper.setPinsInverted( false, false, true, 0);

Good evening, Brazilian greetings, I'm starting on arduino and I learned a lot by reading these posts I would like, and I'm studying the stepper compartments using a register library, I need the engine to turn
clockwise to be able to give an example in the programming please