I have been playing with a stepper motor with the following sketch
// This Arduino example demonstrates bidirectional operation of a
// 28BYJ-48, using a ULN2003 interface board to drive the stepper.
// The 28BYJ-48 motor is a 4-phase, 8-beat motor, geared down by
// a factor of 68. One bipolar winding is on motor pins 1 & 3 and
// the other on motor pins 2 & 4. The step angle is 5.625/64 and the
// operating Frequency is 100pps. Current draw is 92mA.
////////////////////////////////////////////////
//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 = 15000; //variable to set stepper speed
int count = 0; // count of steps made
int countsperrev = 512; // 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));
}
What I want to do is have the motor start, rotate 360 degrees clockwise, 360 degrees anticlockwise, then delay for 4 minutes, then repeat.
How much Arduino and C programming do you know, this is good to know to give best possible help.
I know very little and am trying to learn.
I have bought the book Arduino for Dummies and have been working through it, but I am still having trouble comprehending the way the code is written.
I have tried to add a one second delay in various parts of the code but it seems to either not compile or will move two steps, delay for 1 sec then move two steps again.
I will try to work with what you have given me and experiment some more.
The code for the loop(), as written, is a little hard to grasp. Here’s a re-write that should preserve the original functionality but be easier to understand what’s going on. It’s important to know that the code in the loop() … loops. It runs from top to bottom, taking however long it takes to run but when it gets to the bottom … the last instruction in loop(), it goes back to the top and executes the 1’st instruction all over again. It does this until power is removed or you reset the Arduino.
In this case a variable, count, starts = 0 and counts how many time the loop() is passed through. Some function, called clockwise(), runs. Every time the loop is run through, top to bottom, this clockwise() function will run (so long as count is less than some limit) and the motor will move some amount in a clockwise direction. The counter, count, is then incremented by 1 at the bottom of my new loop(). After enough passes of the clockwise() function the motor will have turned a full 360 degrees and counter will have incremented to equal countsperrev. The original code did exactly the same thing.
Now a different function is run, anticlockwise(). Just as before the loop() is run over and over until the motor has gone a full 360 to get back to where it started. At this point the counter should have incremented to 2 times the limit, countsperrev. When the counter is equal to this value, it’s reset (= 0) and the whole process starts all over again.
The above code should make that process easier to see. Now if you want a delay and you want it to happen ONLY AFTER the full 360 clockwise and the full 360 anticlockwise, well there’s 1 place in the code where you know that’s the case and that’s where you should add your delay.
Ant001:
It seems wherever I insert the delay it either wont compile or the delay is within the loop which
moves the motor through one step then delays for two seconds, then moves the next step.
Regards Ant.
In either code, the original or my reshuffle, there's one and only one place to place a delay that acts after both complete rotations. Go back and reread what I said re: that. If you put your delay there, it should work. If it doesn't, post you code here and we'll see what's up.
I could not get the code to do what I wanted so I have found another that I was able to modify to do most of what I need.
Again thanks for the help and guidance, even though I couldn’t get it to work with the original code you gave a me a clearer understanding of the process of modifying it.
//Small Stepper Motor and Driver
/*-----( Import needed libraries )-----*/
#include <Stepper.h>
/*-----( Declare Constants, Pin Numbers )-----*/
#define STEPS 100 //Number of steps per revolution
/*-----( Declare objects )-----*/
/* create an instance of the stepper class, specifying
the number of steps of the motor and the pins it's
attached to. The pin connections need to be 4 pins connected
to Motor Driver In1, In2, In3, In4 and then the pins entered
here in the sequence 1-3-2-4 for proper sequencing*/
Stepper small_stepper(STEPS, 8, 10, 9, 11);
int Steps2Take = 0;
int relayPin1 = 12;
void setup(){
pinMode(relayPin1, OUTPUT);
}
void loop()
{
small_stepper.setSpeed(15);
digitalWrite (relayPin1, LOW);
Steps2Take = 2038;
// Rotate CW steps = 2038
small_stepper.step(Steps2Take);
digitalWrite (relayPin1, HIGH);
delay(4000);
// Pause between CW and CCW
small_stepper.setSpeed(15);
digitalWrite (relayPin1, LOW);
Steps2Take = -2038; // Rotate CCW steps =2038
small_stepper.step(Steps2Take);
digitalWrite (relayPin1, HIGH);
delay(10000);
}
/* --(end main loop )-- */