Progetto stepper per Arduino

L’amico Luigi sta iniziando a fare piccoli esperimenti con una scheda Arduino, e dei motori passo-passo.
Questo è un esempio di uno sketch che fa avanzare il motorino passo-passo, conoscendo la sua velocità di rotazione, il numero di avanzamenti, e a quanti gradi corrisponde un avanzamento…


/*
Stepper Motor Control - one step at a time
This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 - 11 of the Arduino.

The motor will step one step at a time, VERY SLOWLY ...

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

The program tells the stepper to do a desired number of steps
only once. Arduino keeps looping, but does nothing after the
first loop is completed.

logical stream:

% ++++++++++ %
% i_do = 1 %
% ++++++++++ %
|
|<---------------------------------------------------|
V |
% ++++++++++++++++++++++++++++++++++++++++++++++++ % |
% if i_do = 1 : motor does steps AND set i_do = 0 % ->-|
% else : do nothing %
% ++++++++++++++++++++++++++++++++++++++++++++++++ %

i_do is initialized to 1, so that Arduino tells the stepper
to do the desired number of steps. Then i_do is set to 0,
so that nothing is done in the next loops.

Only the first call to loop() is active, while the following
are in idle.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2016/11/19

*/

// include --------------------------------------------------
// include functions for the Stepper
#include <Stepper.h>

// constants ------------------------------------------------
// define constant parameters. They are integer CONSTANTS
// ( it is not possible to change the value of a variable declared
// with the const attribute. It is a read-only variable. Compiler
// returns an error if you try to change its value. )
// ( rpm is a long integer, a 32-bit integer, instead of 16-bit )
// Constants are :
// stepsPerRevolution : n. of steps in 360°
// numSteps : n. of desired steps
// rpm : motor speed (in rpm)
const int stepsPerRevolution = 200; // 360° / 1.8°
const int numSteps = 50;
const long int rpm = 150;

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

// initialize counters and "logical" variables
int loopCount = 0; // initialized to 0 // number of non-idling loops
int i_do = 1; // initialized to 1 // if i_do = 1 --> do steps
// if i_do = 0 --> do nothing (but Arduino keeps looping)

void setup() {

// initialize the serial port:
Serial.begin(9600);

// set stepper speed (rpm):
myStepper.setSpeed(rpm);

}

void loop() {

// i_do = 1 only the first time loop() is called (because it has been initialized
// to 1). Before leaving loop(), i_do is set to 0, so that the program enters the
// if statement only once.

if ( i_do == 1 ) // entered the first time only
{
// increase the "non-idling loop" counter: from 0 to 1
loopCount = loopCount+1 ;

// tell the motor to do numSteps steps
myStepper.step(numSteps);
// serial print
Serial.print("steps: " );
Serial.println(loopCount*numSteps);

// set logical variable i_do to 0. Nothing is done in the following loops
i_do = 0 ; // set i_do = 0 to do it once

}

}

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

14 − 7 =