Stepping Motor (Stepper Class)

This library enables control of stepping motors connected to the GR-COTTON board. To use, import as a library, specifying #include <stepper.h>.

Stepper Constructor

Description

This constructor creates Stepper Class instances.

Syntax

Stepper(steps, pin1, pin2)Stepper(steps, pin1, pin2, pin3, pin4)

Parameters

steps: The number of steps in one revolution (360 degrees) of the motor. 100 indicates 3.6 degrees per step.
(int) pins 1 - 4: Pins for connection to a motor.

Returns

None

setSpeed

Description

Sets the motor speed in rotations per minute (RPMs).

Syntax

setSpeed(int rpms)

Parameters

rpms: The speed at which the motor should turn in rotations per minute [rpm]

Returns

None

step

Description

Turns the motor a specific number of steps.

Syntax

step(int steps)

Parameters

steps: The number of steps to turn the motor - positive to turn one direction, negative to turn the other

Returns

None

getRpms

Description

Gets current rpm of the motor.

Syntax

int getRpms()

Parameters

None

Returns

Current rpm

getPosition

Description

Gets current position motor's rpm.

Syntax

int getPosition()

Parameters

None

Returns

Current rpm position (number of steps).


Sample Program


        #include <Arduino.h>
        #include <Stepper.h>
        // change this to the number of steps on your motor
        #define STEPS 100
            
        // create an instance of the stepper class, specifying
        // the number of steps of the motor and the pins it's
        // attached to
        Stepper stepper(STEPS, 8, 9, 10, 11);
            
        // the previous reading from the analog input
        int previous = 0;
            
        void setup()
        {
            // set the speed of the motor to 30 RPMs
            stepper.setSpeed(30);
        }
            
        void loop()
        {
            // get the sensor value
            int val = analogRead(A0);
            
            // move a number of steps equal to the change in the
            // sensor reading
            stepper.step(val - previous);
            
            // remember the previous value of the sensor
            previous = val;
        }