Stepper motor Code

Hi Guy’s

I’m trying to get my stepper motor to turn at 1rpm in a clockwise direction.

I’ve found a code that I seem to have managed to slow the motor down to almost 1rpm but I don’t need to go anticlockwise, how do I alter the sketch properly to ditch that part (my efforts so far don’t seem to work properly).

Here is my driver and motor - http://www.thelittlebritishrobotcompany.co.uk/index.php?route=product/product&product_id=64

Here is the code -

//declare variables for the motor pins
int motorPin1 = 8; // Blue - 28BYJ48 pin 1
int motorPin2 = 9; // Pink - 28BYJ48 pin 2
int motorPin3 = 10; // Yellow - 28BYJ48 pin 3
int motorPin4 = 11; // Orange - 28BYJ48 pin 4
// Red - 28BYJ48 pin 5 (VCC)

int motorSpeed = 14000; //variable to set stepper speed
int count = 0; // count of steps made
int countsperrev = 10000; // number of steps per full revolution
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};

//////////////////////////////////////////////////////////////////////////////
void setup() {
//declare the motor pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
Serial.begin(9600);
}

//////////////////////////////////////////////////////////////////////////////
void loop(){
if(count < countsperrev )
clockwise();
else if (count == countsperrev * 2)
count = 0;
else
anticlockwise();
count++;
}

//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 1 to 4
//delay “motorSpeed” between each pin setting (to determine speed)
void anticlockwise()
{
for(int i = 0; i < 8; i++)
{
setOutput(i);
delayMicroseconds(motorSpeed);
}
}

void clockwise()
{
for(int i = 7; i >= 0; i–)
{
setOutput(i);
delayMicroseconds(motorSpeed);
}
}

void setOutput(int out)
{
digitalWrite(motorPin1, bitRead(lookup[out], 0));
digitalWrite(motorPin2, bitRead(lookup[out], 1));
digitalWrite(motorPin3, bitRead(lookup[out], 2));
digitalWrite(motorPin4, bitRead(lookup[out], 3));
}

my efforts so far don’t seem to work properly

What were they? Deleting the function anticlockwise() would be the first step. Deleting the call to it would be the second step. Preventing the condition that leads to calling the function would then be the third step.

Why are you counting steps at all? The only reason that I can see is to reverse direction at some point.

Hmmm, maybe I got led up the wrong path there, I got that code off another site (something someone come up with for a similar project).

All I need to the motor to do is got in a clockwise direction at 1rpm.

Is this code not what I should be using? (I'm completely new to this so I'm not clued up at all about most of these codes etc.)

I tried using the example code in the Arduino program but the motor just vibrates (i guess the sequence is in the wrong order)

Have deleted a few bits of the code I don’t need (hopefully :sweat_smile:)

Have this now -

//declare variables for the motor pins
int motorPin1 = 8; // Blue - 28BYJ48 pin 1
int motorPin2 = 9; // Pink - 28BYJ48 pin 2
int motorPin3 = 10; // Yellow - 28BYJ48 pin 3
int motorPin4 = 11; // Orange - 28BYJ48 pin 4
// Red - 28BYJ48 pin 5 (VCC)

int motorSpeed = 14000; //variable to set stepper speed
int count = 0; // count of steps made
int countsperrev = 10000; // number of steps per full revolution
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};

//////////////////////////////////////////////////////////////////////////////
void setup() {
//declare the motor pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
Serial.begin(9600);
}

//////////////////////////////////////////////////////////////////////////////
void loop(){
if(count < countsperrev )
clockwise();
else if (count == countsperrev * 2)
count = 0;
else
count++;
}

//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 1 to 4
//delay “motorSpeed” between each pin setting (to determine speed)

void clockwise()
{
for(int i = 7; i >= 0; i–)
{
setOutput(i);
delayMicroseconds(motorSpeed);
}
}

void setOutput(int out)
{
digitalWrite(motorPin1, bitRead(lookup[out], 0));
digitalWrite(motorPin2, bitRead(lookup[out], 1));
digitalWrite(motorPin3, bitRead(lookup[out], 2));
digitalWrite(motorPin4, bitRead(lookup[out], 3));
}

Nope that didn't work lol It worked for a while them stopped....

What do you think count++ is doing?

...R

You don’t need to be counting steps if you don’t plan to reverse the stepping. So, get rid of count and everything to do with it.