Monday, February 9, 2015

£1000 Give away

You read it correctly!
We are giving away £1000 worth of kits
Read details on Facebook here

Friday, February 6, 2015

2 steppers controlled with 2 L298N and 1 Arduino

We received the following comment on youtube:-

"Hello! Nice video. The sketch on your blog helped me a lot. I tried it,works well.
But I would like to control two steppers with 2 L298 drivers and 1 arduino board I have.
How can I modify this sketch to make them work? Or can you send me a new sketch for them,please?  I appreciate your help!"


The following sketch will do just that, do check out the delays in void loop though, at 1000, there is a 1 second pause between each section, reduce this to 0 for no delay.
The sections for forward and backwards can be repositioned to make the steppers do what you want, when you want.
As always, feel free to modify the code as much as needed
 
// Highlight from HERE

///This is a simple sketch to allow the Arduino to control
// two bi-polar stepper motors
// By Andrew Mallinson
// Mallinson-electrical.com
// The Arduino is hooked up to two L798N H bridge
// This allows higher voltage and current to be applied to
// the stepper motor, without damaging the Arduino

// Use pins 4,5,6,7 for one H bridge(stepper 2)
// Use pins 8,9,10,11 for the other (stepper 1)
//
// Define the Arduino pins
int RED = 8; // L298N  In1
int YELLOW = 9;//L298N In2
int GREEN = 10;//L298N In3
int BLACK = 11;//L298N In4
int RED2 = 4; // L298N  In1 second H bridge
int YELLOW2 = 5;//L298N In2 second H bridge
int GREEN2 = 6;//L298N In3 second H bridge
int BLACK2 = 7;//L298N In4 second H bridge
// Define some variables
int d = 3; //define delay - adjust this to give smooth running, lower number = higher speed
int s = 200;//steps per rev of stepper
float fwd = 1.5; // number of revs forward required - {Can be any number or increment of 0.02
float rvs = 3.0; // number of revs reverse required - {sequences are 4 steps, 50x 4 steps = 1 rev, so 4 steps=0.02 revs
float fwd2 = 1.5; // number of revs forward required - {Can be any number or increment of 0.02
float rvs2 = 3.0; // number of revs reverse required - {sequences are 4 steps, 50x 4 steps = 1 rev, so 4 steps=0.02 revs

void setup()
{
  pinMode(RED,OUTPUT); // set the pins to be giving signal not receiving
  pinMode(YELLOW,OUTPUT);
  pinMode(GREEN,OUTPUT);
  pinMode(BLACK,OUTPUT);
  pinMode(RED2,OUTPUT);
  pinMode(YELLOW2,OUTPUT);
  pinMode(GREEN2,OUTPUT);
  pinMode(BLACK2,OUTPUT);

}
// There are four different steps, because there are 4 permutations of the poles on a bipolar stepper
// Pair Red/Yellow and Pair Green/Black

void Astep() {     // This is a subroutine to tell arduino the output which creates the first step
  digitalWrite (RED,1);  // +ve voltage out of red
  digitalWrite (YELLOW,0);// so yellow is -ve
  digitalWrite (GREEN,0);// sets green to -ve
  digitalWrite (BLACK,1);// so black must have +ve
}
void Bstep() {
  digitalWrite (RED,1);
  digitalWrite (YELLOW,0);
  digitalWrite (GREEN,1);
  digitalWrite (BLACK,0); 
}void Cstep() {
  digitalWrite (RED,0);
  digitalWrite (YELLOW,1);
  digitalWrite (GREEN,1);
  digitalWrite (BLACK,0);
}void Dstep() {
  digitalWrite (RED,0);
  digitalWrite (YELLOW,1);
  digitalWrite (GREEN,0);
  digitalWrite (BLACK,1); 
}
void A2step() {    
  digitalWrite (RED2,1); 
  digitalWrite (YELLOW2,0);
  digitalWrite (GREEN2,0);
  digitalWrite (BLACK2,1);
}
void B2step() {
  digitalWrite (RED2,1);
  digitalWrite (YELLOW2,0);
  digitalWrite (GREEN2,1);
  digitalWrite (BLACK2,0); 
}void C2step() {
  digitalWrite (RED2,0);
  digitalWrite (YELLOW2,1);
  digitalWrite (GREEN2,1);
  digitalWrite (BLACK2,0);
}void D2step() {
  digitalWrite (RED2,0);
  digitalWrite (YELLOW2,1);
  digitalWrite (GREEN2,0);
  digitalWrite (BLACK2,1); 
}
void forwd()// this is a subroutine to define the sequence of steps to drive the stepper forward
{
  Astep(); // Carry out the instruction of a subroutine we created called Astep
  delay(d); // wait for a length of time defined in the setup section
  Bstep();
  delay(d);
  Cstep();
  delay(d);
  Dstep();
  delay(d);
}
void forwd2()// define the sequence of steps to drive the second stepper forward
{
  A2step();
  delay(d);
  B2step();
  delay(d);
  C2step();
  delay(d);
  D2step();
  delay(d);
}
void rvrs()// define the sequence of steps to drive the stepper in reverse
{
  Dstep();
  delay(d);
  Cstep();
  delay(d);
  Bstep();
  delay(d);
  Astep();
  delay(d);
}
void rvrs2()// define the sequence of steps to drive the second stepper in reverse
{
  D2step();
  delay(d);
  C2step();
  delay(d);
  B2step();
  delay(d);
  A2step();
  delay(d);
}


void loop()
{
  int f=(fwd*s/4);// create a new value f based on forward turns required, steps/rev and divide by four (4 seperate steps)
  for(int i=0; i<f; i++)// setup a counter called i, if it is less than f(above), we will do this again, so add 1 to i and carry out the instruction below
  {
  forwd(); // carry out the instructions we defined in subroutine forwd
  }
  delay(1000); // wait for one second - the number is milliseconds, 1000ms = 1s
   int f2=(fwd2*s/4);// create a new value f2 based on forward turns required for second stepper, steps/rev and divide by four (4 seperate steps)
  for(int i=0; i<f2; i++)// setup a counter called i, if it is less than f2(above), we will do this again, so add 1 to i and carry out the instruction below
  {
  forwd2(); // carry out the instructions we defined in subroutine forwd2, to drive 2nd stepper
  }
  delay(1000);
  int r=(rvs*s/4);
  for(int i=0;i<r;i++)
  {
    rvrs();// 1st stepper reverse
  }
  delay (1000);
  int r2=(rvs2*s/4);
  for(int i=0;i<r2;i++)
  {
    rvrs2();// 2nd stepper reverse
  }
  delay (1000);
}

// Highlight to HERE