Arduino Forum

Using Arduino => Motors, Mechanics, Power and CNC => Topic started by: copachino on Oct 16, 2014, 10:55 pm

Title: step dir motor and speed control without library
Post by: copachino on Oct 16, 2014, 10:55 pm
hi does anybody have a step dir code a simple one a have a driver bases on the DRV8818 chip, and i know its active low, but i dont know or have any example on how to program it to be use with the motor on arduino, any help?
Title: Re: step dir code
Post by: MarkT on Oct 17, 2014, 02:04 am
The step and direction inputs to the DRV8818 are standard, positive-edge triggered
step.  The AccelStepper library is probably a good starting point.

You probably can just hard-wire the other logic inputs.
Title: Re: step dir code
Post by: Robin2 on Oct 17, 2014, 10:06 am
Just put the direction pin HIGH or LOW as required and this code will create a single step

Code: [Select]
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);


repeat as required.

...R
Title: Re: step dir code
Post by: copachino on Oct 17, 2014, 04:05 pm

Just put the direction pin HIGH or LOW as required and this code will create a single step

Code: [Select]
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);


repeat as required.

...R


like this??, this has two push bottoms to change the spin of the stepper, i still have no tried but, seems ok as you said

Code: [Select]
#include <configMe.h>
#include <stepper.h>


#define MI_PIN_STEP 5
#define MI_PIN_DIR 2
const int  ccw = 10;   
const int cw = 13;

void setup()
{
  //Wire.begin ();
  Serial.begin ( 57600 );
 
  pinMode ( MI_PIN_STEP, OUTPUT );
  pinMode ( MI_PIN_DIR, OUTPUT );
 
  pinMode(ccw,INPUT);
  pinMode(cw, INPUT);


 
}

void loop()
{
 
 
// {

if(cw==HIGH)
{
  digitalWrite ( MI_PIN_DIR, HIGH );

}
  if(ccw==HIGH)
{
digitalWrite ( MI_PIN_DIR, LOW ); // chage rotation

}

  // steps
  digitalWrite ( MI_PIN_STEP, LOW );
  delay ( 10 );
  digitalWrite ( MI_PIN_STEP, HIGH );
   delay ( 10 );
}
Title: Re: step dir code
Post by: Robin2 on Oct 17, 2014, 08:01 pm
Code: [Select]
  digitalWrite ( MI_PIN_STEP, LOW );
  delay ( 10 );
  digitalWrite ( MI_PIN_STEP, HIGH );
   delay ( 10 );


My code didn't have any delay between the HIGH and LOW - why have you got one?

Put all the delay between the steps. The actual pulse can be very short and the digitalWrite() function is slow enough on its own.

The Arduino is a great system for learning-by-doing.

...R
Title: Re: step dir code
Post by: copachino on Oct 17, 2014, 09:51 pm

Code: [Select]
  digitalWrite ( MI_PIN_STEP, LOW );
  delay ( 10 );
  digitalWrite ( MI_PIN_STEP, HIGH );
   delay ( 10 );


My code didn't have any delay between the HIGH and LOW - why have you got one?

Put all the delay between the steps. The actual pulse can be very short and the digitalWrite() function is slow enough on its own.

The Arduino is a great system for learning-by-doing.

...R


they are just to avoid the microcontroller to overload data, or thats the idea, i see i dont need two bottoms just one HIGH or LOW will give me HIGH or LOW DIR steps, i need to add a revolutions counter to the loos, and a potenciometer to change the speed, i still have no tried the code mybe will do tomorrow
Title: Re: step dir code
Post by: copachino on Oct 17, 2014, 10:11 pm
Code: [Select]
#include <configMe.h>

#include <Stepper.h>


#define MI_PIN_STEP 5
#define MI_PIN_DIR 2
const int  gir = 10;  
const int stepsPerRevolution = 200;
int stepCount = 0;

void setup()
{
 //Wire.begin ();
 Serial.begin ( 57600 );
 
 pinMode ( MI_PIN_STEP, OUTPUT );
 pinMode ( MI_PIN_DIR, OUTPUT );
 
 pinMode(gir,INPUT);
 
 



 
}

void loop()
{
 
 int sensorReading = analogRead(A0);
 int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
 
 if (motorSpeed > 0) {
   myStepper.setSpeed(motorSpeed);
   // step 1/100 of a revolution:
   myStepper.step(stepsPerRevolution / 100);
 }
 
 
// {

if(gir==HIGH)
{
 digitalWrite ( MI_PIN_DIR, HIGH );

}
 if(gir==LOW)
 {
digitalWrite ( MI_PIN_DIR, LOW );

}

 // pasos
 digitalWrite ( MI_PIN_STEP, LOW );
 
 digitalWrite ( MI_PIN_STEP, HIGH );
 
 
 
}

Arduino: 1.5.8 (Windows 8), Placa:"Arduino Uno"

sketch_oct16a.ino: In function 'void loop()':
sketch_oct16a.ino:36:5: error: 'myStepper' was not declared in this scope
Error de compilación

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.



i dont see where its the problem with the prpgram i just tried to add a potenciometer to set speed, but i cant see the errors
Title: Re: step dir code
Post by: Robin2 on Oct 17, 2014, 10:37 pm

they are just to avoid the microcontroller to overload data, or thats the idea, i see i dont need two bottoms just one HIGH or LOW will give me HIGH or LOW DIR steps, i need to add a revolutions counter to the loos, and a potenciometer to change the speed, i still have no tried the code mybe will do tomorrow


I assume you use the word "they" to refer to the 2 instances of delay(10). I did not say to remove both of them - just the first one, and then make the second one delay(20) to compensate.

Doing it my way will NOT overload anything.

I don't understand the rest of the sentence after the word "idea"


sketch_oct16a.ino:36:5: error: 'myStepper' was not declared in this scope

i dont see where its the problem with the prpgram i just tried to add a potenciometer to set speed, but i cant see the errors


The error message says it all. You should have a line Stepper myStepper(xxx); before setup() and you have omitted it.

However if you are just trying to move the stepper without using the stepper library then the error is that you still have lines with myStepper.setSpeed(motorSpeed); etc which are no longer needed.

...R
Title: Re: step dir code
Post by: copachino on Oct 17, 2014, 10:46 pm
im almost like copy-page from the igor code on the exmples, and the example compiles ok, but mine have this problem in the setup its not declared nothing on the original code, so maybe my lib its wrong??
Title: Re: step dir code
Post by: Robin2 on Oct 17, 2014, 11:15 pm

im almost like copy-page from the igor code on the exmples, and the example compiles ok, but mine have this problem in the setup its not declared nothing on the original code, so maybe my lib its wrong??


I don't understand.

Did you read my Reply #7 ?
Did you try my suggestions?

...R
Title: Re: step dir code
Post by: copachino on Oct 17, 2014, 11:50 pm


im almost like copy-page from the igor code on the exmples, and the example compiles ok, but mine have this problem in the setup its not declared nothing on the original code, so maybe my lib its wrong??


I don't understand.

Did you read my Reply #7 ?
Did you try my suggestions?

...R


ys i tried, but mystepper. its somethingon the Stepper.h library, so i doesnt need to be declared, i am calling stepper.h lib but doesnt work on this code, im trying to just implement the stepper speed control with a potenciometer that its on the data base of steppers examples. but software keeps asking to declare Mystepper function.

this its my code:
Code: [Select]
#include <configMe.h>

#include <Stepper.h>


#define MI_PIN_STEP 5
#define MI_PIN_DIR 2
const int  gir = 10;   
const int stepsPerRevolution = 200;
int stepCount = 0;

void setup()
{
  //Wire.begin ();
  Serial.begin ( 57600 );
 
  pinMode ( MI_PIN_STEP, OUTPUT );
  pinMode ( MI_PIN_DIR, OUTPUT );
 
  pinMode(gir,INPUT);
 
 



 
}

void loop()
{
 
  int sensorReading = analogRead(A0);
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
 
  if (motorSpeed > 0) {
    myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution /200);
  }
 
 
// {

if(gir==HIGH)
{
  digitalWrite ( MI_PIN_DIR, HIGH );

}
  if(gir==LOW)
  {
digitalWrite ( MI_PIN_DIR, LOW );
}

  // pasos
  digitalWrite ( MI_PIN_STEP, LOW );
 
  digitalWrite ( MI_PIN_STEP, HIGH );
 
 
   



and this its the one on the examples
Code: [Select]

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor


// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int stepCount = 0;  // number of steps the motor has taken

void setup() {
  // nothing to do inside the setup
}

void loop() {
  // read the sensor value:
  int sensorReading = analogRead(A0);
  // map it to a range from 0 to 100:
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
  // set the motor speed:
  if (motorSpeed > 0) {
    myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution / 100);
  }
}



as you see mystepper doesnt need to be declared, but i dont know why the compliler keeps asking to declare it
Title: Re: step dir code
Post by: copachino on Oct 17, 2014, 11:57 pm
i see my error, but still does not complie

Code: [Select]
#include <configMe.h>

#include <Stepper.h>


#define MI_PIN_STEP 5
#define MI_PIN_DIR 2
const int  gir = 10;   
const int stepsPerRevolution = 200;
int stepCount = 0;
Stepper myStepper(stepsPerRevolution, 5, 3,);

void setup()
{
  //Wire.begin ();
  Serial.begin ( 57600 );
 
  pinMode ( MI_PIN_STEP, OUTPUT );
  pinMode ( MI_PIN_DIR, OUTPUT );
 
  pinMode(gir,INPUT);
 
 



 
}

void loop()
{
 
  int sensorReading = analogRead(A0);
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
 
  if (motorSpeed > 0) {
    myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution / 100);
  }
 
 
// {

if(gir==HIGH)
{
  digitalWrite ( MI_PIN_DIR, HIGH ); // Poner una dirección

}
  if(gir==LOW)
  {
digitalWrite ( MI_PIN_DIR, LOW ); // Poner la otra dirección

}

  // pasos
  digitalWrite ( MI_PIN_STEP, LOW );
 
  digitalWrite ( MI_PIN_STEP, HIGH );
 
 
   
}
Title: Re: step dir code
Post by: runaway_pancake on Oct 18, 2014, 06:05 am

i see my error, but still does not complie


And what message/s are you getting back from the IDE about this?
Title: Re: step dir code
Post by: Robin2 on Oct 18, 2014, 01:04 pm

ys i tried, but mystepper. its somethingon


The step code that I suggested is used without the Stepper library - it has nothing to do with the Stepper library. You can use the Stepper library OR my code but not both.

And, as @Runaway Pancake has asked, what error message are you getting?

...R
Title: Re: step dir code
Post by: copachino on Oct 18, 2014, 03:58 pm


ys i tried, but mystepper. its somethingon


The step code that I suggested is used without the Stepper library - it has nothing to do with the Stepper library. You can use the Stepper library OR my code but not both.

And, as @Runaway Pancake has asked, what error message are you getting?

...R


im getting the same error from myStepper

Code: [Select]
 This report would have more information with
 "Show verbose output during compilation"
 enabled in File > Preferences.
Arduino: 1.0.6 (Windows 7), Board: "Arduino Uno"
sketch_oct18a:11: error: expected constructor, destructor, or type conversion before '(' token
sketch_oct18a.ino: In function 'void loop()':
sketch_oct18a:37: error: 'myStepper' was not declared in this scope



also im using this driver, and the lib that comes with itm that lib have in the step and dir funtions that im using so, calling stepper,h wont e a problem, but still IDE dont recognizes mystepper funtion
Title: step dir motor and speed control without library
Post by: copachino on Oct 18, 2014, 06:36 pm
hi im trying to program a stepper to move with a potenciometer and change the speed, also with a switch to control to rotation, cww or cw, but i have some trouble with the library, some how seems not to work fine, and IDE its asking me to declare the funtions on the library itself

Code: [Select]
#include <configMe.h>

#include <Stepper.h>


#define MI_PIN_STEP 5
#define MI_PIN_DIR 2
const int  gir = 10;   
const int stepsPerRevolution = 200;
int stepCount = 0;

void setup()
{
  //Wire.begin ();
  Serial.begin ( 57600 );
 
  pinMode ( MI_PIN_STEP, OUTPUT );
  pinMode ( MI_PIN_DIR, OUTPUT );
 
  pinMode(gir,INPUT);
 
 



 
}

void loop()
{
 
  int sensorReading = analogRead(A0);
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
 
  if (motorSpeed > 0) {
    setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    step(stepsPerRevolution / 100);
  }
 
 
// {

if(gir==HIGH)
{
  digitalWrite ( MI_PIN_DIR, HIGH ); // Poner una dirección

}
  if(gir==LOW)
  {
digitalWrite ( MI_PIN_DIR, LOW ); // Poner la otra dirección

}

  // pasos
  digitalWrite ( MI_PIN_STEP, LOW );
 
  digitalWrite ( MI_PIN_STEP, HIGH );
 
 
   
}



Code: [Select]
Arduino: 1.0.6 (Windows 7), Board: "Arduino Uno"
D:\arduino-1.0.6\hardware\tools\avr\bin\avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -ID:\arduino-1.0.6\hardware\arduino\cores\arduino -ID:\arduino-1.0.6\hardware\arduino\variants\standard -ID:\arduino-1.0.6\libraries\Stepper C:\Users\MILAU\AppData\Local\Temp\build6855970105847057216.tmp\sketch_oct18a.cpp -o C:\Users\MILAU\AppData\Local\Temp\build6855970105847057216.tmp\sketch_oct18a.cpp.o

sketch_oct18a.ino: In function 'void loop()':
sketch_oct18a:36: error: 'setSpeed' was not declared in this scope
sketch_oct18a:38: error: 'step' was not declared in this scope
Title: Re: step dir motor and speed control without library
Post by: Robin2 on Oct 18, 2014, 07:10 pm
I have been trying to answer this question in your other Thread. (http://forum.arduino.cc/index.php?topic=273039) Why have you started a new Thread just to waste people's time?

I am asking the Moderator to merge this with the other Thread.

...R
Title: Re: step dir code
Post by: Robin2 on Oct 18, 2014, 07:24 pm

i see my error, but still does not complie


I tried to compile the code in Reply #11 and the only error was on this line
Code: [Select]
Stepper myStepper(stepsPerRevolution, 5, 3,);
and the problem is that there is comma after the '3'.

When I delete that comma it compiles. That does not mean it would work because it may be that there should be a number after that extra comma.

HOWEVER ...
That code does NOT give the error you have quoted
Quote
sketch_oct18a:37: error: 'myStepper' was not declared in this scope

which leads me to the conclusion that you have not posted the code that is actually causing you a problem.

AND ...
You started another Thread about the same problem - controlling a stepper without a library. I have already explained how to do that in this Thread. If there is part of my explanation that you don't understand please let me know.

...R
Title: Re: step dir motor and speed control without library
Post by: copachino on Oct 18, 2014, 08:31 pm

I have been trying to answer this question in your other Thread. (http://forum.arduino.cc/index.php?topic=273039) Why have you started a new Thread just to waste people's time?

I am asking the Moderator to merge this with the other Thread.

...R


you can you wharever you want to do...
Title: Re: step dir motor and speed control without library
Post by: nickgammon on Oct 18, 2014, 10:38 pm
Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

- Moderator
Title: Re: step dir motor and speed control without library
Post by: copachino on Oct 19, 2014, 12:31 am
nick you can delete the post, i have found the way to solve my problem, so this its closed
Title: Re: step dir motor and speed control without library
Post by: nickgammon on Oct 19, 2014, 01:09 am
We don't just delete threads because they are solved. This forum is supposed to be a knowledge-base, to help other people.
Title: Re: step dir motor and speed control without library
Post by: copachino on Oct 19, 2014, 01:11 am
i know, but i never solved the problem with the IDE, and my bad programming, so i just wrote i new code with accelstepper, but this thread its not working save since there its not good information here
Title: Re: step dir motor and speed control without library
Post by: Robin2 on Oct 19, 2014, 10:42 am

i know, but i never solved the problem with the IDE, and my bad programming, so i just wrote i new code with accelstepper, but this thread its not working save since there its not good information here


It would help others if you post your working code and explain why the information here was not suitable for your project and how you figured out your solution.

I would also like to learn so that my advice can be better focused next time.

...R
Title: Re: step dir motor and speed control without library
Post by: copachino on Oct 19, 2014, 05:37 pm


i know, but i never solved the problem with the IDE, and my bad programming, so i just wrote i new code with accelstepper, but this thread its not working save since there its not good information here


It would help others if you post your working code and explain why the information here was not suitable for your project and how you figured out your solution.

I would also like to learn so that my advice can be better focused next time.

...R


ok here its the working code, and i could not figure out what was wrong in the library but i suppect there was something wrong on my object declarations, this code im posting uses accelstepper, the lib its way huge improve of the original stepper.h, and its perfect to work with step/dir drivers.

Code: [Select]
#include <AccelStepper.h>

// Define the stepper and the pins it will use
AccelStepper stepper1(1, 9, 8);

// Define our three input button pins
#define  LEFT_PIN  4 // left and right its one switch a spdt on/on with center pin at ground
#define  STOP_PIN  3
#define  RIGHT_PIN 2

// Define our analog pot input pin
#define  SPEED_PIN 0

// Define our maximum and minimum speed in steps per second (scale pot to these)
#define  MAX_SPEED 4000
#define  MIN_SPEED 0.1

void setup() {
  // The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
  stepper1.setMaxSpeed(10000.0);
 
  // Set up the three button inputs, with pullups
  pinMode(LEFT_PIN, INPUT_PULLUP);
  pinMode(STOP_PIN, INPUT_PULLUP);
  pinMode(RIGHT_PIN, INPUT_PULLUP);
}

void loop() {
  static float current_speed = 0.0;         // Holds current motor speed in steps/second
  static int analog_read_counter = 1000;    // Counts down to 0 to fire analog read
  static char sign = 0;                     // Holds -1, 1 or 0 to turn the motor on/off and control direction
  static int analog_value = 0;              // Holds raw analog value.
 
  // If a switch is pushed down (low), set the sign value appropriately
  if (digitalRead(LEFT_PIN) == 0) {
    sign = 1;
  }
  else if (digitalRead(RIGHT_PIN) == 0) {   
    sign = -1;
  }
  else if (digitalRead(STOP_PIN) == 0) {
    sign = 0;
  }

  // We only want to read the pot every so often (because it takes a long time we don't
  // want to do it every time through the main loop). 
  if (analog_read_counter > 0) {
    analog_read_counter--;
  }
  else {
    analog_read_counter = 3000;
    // Now read the pot (from 0 to 1023)
    analog_value = analogRead(SPEED_PIN);
    // Give the stepper a chance to step if it needs to
    stepper1.runSpeed();
    //  And scale the pot's value from min to max speeds
    current_speed = sign * ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
    // Update the stepper to run at this new speed
    stepper1.setSpeed(current_speed);
  }

  // This will run the stepper at a constant speed
  stepper1.runSpeed();
 
 

}
Title: Re: step dir motor and speed control without library
Post by: Robin2 on Oct 19, 2014, 09:20 pm

ok here its the working code,


Thanks.

I guess I had assumed from the title of your Thread that you didn't want to use a library.

...R