set pwm frequency for stepper motor

Hi,
I am trying to control a stepper motor with arduino using A4988 Stepper Motor Driver. I am OK running the motor using Bit-banging Pulse Width Modulation as described in : https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM

But my program has other interrupts so I can not use this option and I believe I need to use ATmega PWM registers directly.

I am able to use fastpwm to control de dimming of a led for example but I do not know how to control the frequency so I can step the motor at different speeds.

This library seems to do what I want by using the function SetPinFrequencySafe but in arduino1.6.7 the installation of this library reports an error of "invalid library".
Any alternative solutions.
Thanks

Post a link to the stepper driver you are using. Typically, a stepper driver simply needs to have its step pin set HIGH then LOW to make the stepper step. Doing that with PWM is not the way to do it.

but in arduino1.6.7 the installation of this library reports an error of "invalid library".

Bullshit. The message CLEARLY says WARNING.

I am currently using this:

You are right, I simply need to turn high and then low for each step, the problem I have is doing it at the correct frequency, especially when the arduino is also doing other task.

the problem I have is doing it at the correct frequency

Using a PWM pin is not the correct solution.
Diddling with the PWM frequency will be pointless.

especially when the arduino is also doing other task.

That "other task" must be made non-blocking, so that you CAN tell the motor to step often enough.

The rules for posting in this section of the forum require that you post your code (correctly). If you haven't read the rules, now is the time to do so.

Why are you not simply using one of the existing stepper libraries, like AccelStepper?

Regards,
Ray L.

This is an example code that doesn't works, the "if" with the software serial read screws up the frequency of the stepper,

#include <SoftwareSerial.h>
// software serial #1: TX = digital pin 10, RX = digital pin 11
SoftwareSerial portOne(10, 11);
String str="Hola";
int stepPin=2;

void setup() {
 
  pinMode(stepPin, OUTPUT);

  // Start each software serial port
  portOne.begin(9600);

}

void loop() {
  digitalWrite(stepPin, HIGH);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(80);

  portOne.listen();
  if(portOne.available() ) {
     str = portOne.readStringUntil('\n');
  }

}

if I comment these lines

  portOne.listen();
  if(portOne.available() ) {
     str = portOne.readStringUntil('\n');
  }

the stepper works perfectly.

PaulS, regarding the pwm library error this is the message I get:
fatal error: PWM.h: No such file or directory
#include <PWM.h>
^
compilation terminated.
exit status 1
Error de compilación
Libreria invalidad encontrada en /home/gelu/Arduino/libraries/pwm: /home/gelu/Arduino/libraries/pwm

Ray L., thanks, I'll look into AccelStepper, I wasn't using it because I didn't know about it.. thanks.
I have given it a try and again, strangely, just creating a softwareseral port breaks the example in ConstantSpeed. Doing, for example:

#include <AccelStepper.h>

#include <SoftwareSerial.h>
// software serial #1: TX = digital pin 10, RX = digital pin 11
SoftwareSerial portOne(10, 11);

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

int pos = 3600;

void setup()
{  
  stepper.setMaxSpeed(3000);
  stepper.setAcceleration(1000);
   portOne.begin(9600);
}

void loop()
{
  
  if (stepper.distanceToGo() == 0)
  {
    delay(500);
    pos = -pos;
    stepper.moveTo(pos);
  }
  stepper.run();
}

makes the code to not work properly..
Any pointers to what I am doing incorrectly.

SoftwareSerial and stepper motor control are fundamentally incompatible. If you want good stepper control, you cannot use things like SoftwareSerial that WILL screw up timing.

Regards,
Ray L.

the "if" with the software serial read screws up the frequency of the stepper,

I did say to not write (or use) blocking code. readStringUntil() is a blocking function.

This library seems to do what I want by using the function SetPinFrequencySafe but in arduino1.6.7 the installation of this library reports an error of "invalid library".

This library? Maybe you know what "this library" is, but we don't.

Thanks RayLivingston. Would the use of pwm "Using the ATmega PWM registers directly" (as explained in https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM) avoid this software conflict?
I thought that if I managed to control the frequency of the "hardware" pwm cycle I might be able to control the stepper.

I am sorry PaulS, somehow I didn't post the link to the pwm library:
http://forum.arduino.cc/index.php?topic=117425.0

Would the use of pwm "Using the ATmega PWM registers directly" (as explained in https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM) avoid this software conflict?

No. PWM is NOT how to drive stepper motors.

Now would be a good time to explain what your project is all about.

The way to avoid the software conflict is to use HardwareSerial for reading the serial data. That may require that you get a Mega or Due that have 4 hardware serial ports each.

PaulS, could you detail why PWM can not be used for stepper motors? I thought that basically pwm would generate exactly the digital square signal required. See for example: http://www.instructables.com/id/Arduino-Hardware-PWM-for-stepper-motor-drives
From that link It seems that the Timer1 is another library that could be used for that purpose, although I would like to know your opinion on why it won't work.

My project involves controlling a stepper motor (changing the speed and direction) from an embedded system (nvidia jetson tk1). Because the jetson is doing other tasks I can not do the control directly so I am using an Arduino as a "middle man" between the jetson and the polulu A4988 driver.

The jetson communicates through the serial port with the Arduino, which in response to the serial commands controls the speed and direction pins of the driver.

Preferably, due to size constrains in my project I wanted to use an attiny85 as a replacement for the Arduino using this approach http://highlowtech.org/?p=1695 and that is the reason I would need to use softwareserial for the communications.
The arduino (and attiny) are able to do the work using the software pwm but whenever I activate software serial it gets screwed up as I showed in my previous post. I though using a hardware PWM would be the solution but it seems from your comment that it is not the way forward, I would like to know why?
Many thanks for your time.

CapaArdu:
installation of this library reports an error of "invalid library".
Any alternative solutions.
Thanks

This may help: I've found out that the message almost certainly has nothing to do with the program just loaded. It seems to be a background monitoring thing which looks in the sub directories of the Arduino IDE to make sure core third party (but Arduino approved?) libraries confirm to their standards.

Have a look in the directory where the Aurduino IDE EXE is installed...

HI Giovanni,
thanks for your message but I don't understand what you mean, can you be more specific, I am running on Linux so I do not have any Arduino IDE exe.

Using Timer1 library I am indeed able to run the stepper motor..
And it seems to be unaffected by the SoftwareSerial Library, as long as I use for RX/TX pins that are not tied to timer1.
The following code reads from SoftwareSerial and whenever it receives the "FAST" string changes the speed of the stepper to fast.
It is important that the software serial pins are correct, for example changing RX to 10 screws up the pwm.
Now I am going to check if timer1 code can be ported to Attiny85..

#include <SoftwareSerial.h>
#include "TimerOne.h"
// software serial #1: TX = digital pin 10, RX = digital pin 11
SoftwareSerial portOne(12, 11);
String str="SLOW";

int ledPin=13;
int stepPin=3;
int pwmPin=9;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  // Open serial communications and wait for port to open:

  Timer1.initialize(800);         // initialize timer1, and set a 1/2 second period
  Timer1.attachInterrupt(callback);  // attaches callback() as a timer overflow interrupt
  
  Serial.begin(9600);

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // Start each software serial port
  portOne.begin(9600);

}

void callback()
{
  digitalWrite(stepPin, digitalRead(stepPin) ^ 1);
}

void loop() {
  
  
  // By default, the last intialized port is listening.
  // when you want to listen on a port, explicitly select it:
  portOne.listen();
  // while there is data coming in, read it
  if(portOne.available() ) {
     str = portOne.readStringUntil('\n');
     str.trim();
  }

 if (str=="FAST"){ 
    Timer1.pwm(pwmPin, 512,400);                // setup pwm on pin 9, 50% duty cycle
    digitalWrite(ledPin, HIGH);
    Serial.print("Running : ");
    Serial.println(str);
  } else { // Unless we receive FAST from the software serial port stepp the motor slow
    Timer1.pwm(pwmPin, 512,800);                // setup pwm on pin 9, 50% duty cycle
    digitalWrite(ledPin, LOW);
    Serial.print("Running : ");
    Serial.println(str);
  }
}

CapaArdu:
HI Giovanni,
I am running on Linux...

I imagine, but don't know, that Linux Arduino stuff is organised in roughly the same way as on a Windows PC. there are two main directories, where "the program is" and where your own sketches and other third party libraries are stored. If you put a library inside libraries (purple arrow in the image) then I think that is the cause of the

Invalid library found in C:\Programmi\Arduino\libraries...

message. Here's an example on an Italian XP system...

dear giovanni,
thanks, you are right, the directory structure is similar. But what I don't understand is how this can solve the problem of the library not loading? To use the library I need to place the library inside the libraries folder, and because the library has some incompatibility with the new IDE, it doesn't load so the program gives an error and arduino IDE issues the message of invalid library.