Robin2:
I don't know if you have the step sequence correct. For my 28BYJ-48 stepper motor I use this sequence
1100
0110
0011
1001
I suggest you don't pass the step position to the stepMotor() function - it is something that is entirely local to the motor movement and is irrelevant to rest of the program. Instead just calculate it within the function. Increment the position for forward motion and decrement it for reverse.
...R
Thanks again Robin - I re-wrote the code as you suggested, copied below. It still didn't work, so I added some print lines to see what was going.
In the loops, I've added some print lines, like this:
Serial.print("|1001|");
With the print added, the motor turns perfectly. Without the print lines, the motor does nothing!
What's that about then?! I presume my original code would have worked with these lines in it too, but what gives? Why would a serial.print solve it?
/* The sequence of control signals for 4 control wires is as follows:
*
* Step C0 C1 C2 C3
* 1 1 0 1 0
* 2 0 1 1 0
* 3 0 1 0 1
* 4 1 0 0 1
*/
//=======Libraries======================================================
#include <IRremote.h>
#include <IRremoteInt.h>
#include "Arduino.h"
#include "IRremote.h"
//=======End Libraries==================================================
//===Initialise=========================================================
//Constants
//---stepper motor--------------------------------------------
const int number_of_steps = 2048; //number of steps for the stepper motor
const int motor_pin_1 = 8; //Signal Pin of Stepper Motor
const int motor_pin_2 = 10; //Signal Pin of Stepper Motor
const int motor_pin_3 = 9; //Signal Pin of Stepper Motor
const int motor_pin_4 = 11; //Signal Pin of Stepper Motor
const int direction = 0; // motor direction
//---IR receiver constants----------------------------------------------
//const int receiver = 11; // Signal Pin of IR receiver when added
//variables
long roles_Per_Minute = 6; //6 RPM to start - variable type to take decimals?? needs to increment by 0.1
int step_number = 0; // which step the motor is on
int last_step_time = 0; // time stamp in us of the last step taken
int steps_to_move;
long step_delay; //step delay - not sure why this isn't declared in the stepper library example
//Functions
//----Step Function-----------------------------------------
void stepper(long RPM)
{
roles_Per_Minute = RPM;
steps_to_move = number_of_steps; //number of steps declared as a constant
int steps_left = abs(steps_to_move); // how many steps to take
step_delay = 60L * 1000L * 1000L / number_of_steps / roles_Per_Minute; //eventually write this line into IR code to update step delay with IR Controller
// decrement the number of steps, moving one step each time:
while (steps_left > 0)
{
unsigned long now = micros();
// move only if the appropriate delay has passed:
if (now - last_step_time >= step_delay)
{
// get the timeStamp of when you stepped:
last_step_time = now;
// increment or decrement the step number,
// depending on direction:
if (direction == 1)//Anti-Clockwise
{
steps_left--;
for(int step_number = 0; step_number < 4; step_number += 1)
{
switch (step_number) {
case 0: // 1010
Serial.print("|1010|");
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, HIGH);
digitalWrite(motor_pin_4, LOW);
break;
case 1: // 0110
Serial.print("|0110|");
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_3, HIGH);
digitalWrite(motor_pin_4, LOW);
break;
case 2: //0101
Serial.print("|0101|");
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, HIGH);
break;
case 3: //1001
Serial.print("|1001|");
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, HIGH);
break;
}//end switch
}//end for loop
}//end if
else
{
steps_left--;
for(int step_number = 3; step_number >= 0; step_number -= 1)
{
switch (step_number) {
case 0: // 1010
Serial.print("|1010|");
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, HIGH);
digitalWrite(motor_pin_4, LOW);
break;
case 1: // 0110
Serial.print("|0110|");
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_3, HIGH);
digitalWrite(motor_pin_4, LOW);
break;
case 2: //0101
Serial.print("0101");
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, HIGH);
break;
case 3: //1001
Serial.print("|1001|");
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, HIGH);
break;
}
}
}
}
}
}
//--------translateIR Function---------------------
//Add function to translate IR codes here
//void translateIR(TIMESTAMP) // takes action based on IR code received
//IF timestamp + 500ms < NOW then
// SKIP CODE
//ELSE
//Execute IR button press
//--------End Functions----------------------
//create objects
//Add IR objects here
//IRrecv irrecv(receiver); // create instance of 'irrecv'
//decode_results results; // create instance of 'decode_results'
//======Setup============================================================
//runs once
void setup() {
//Pin order: 8, 10, 9, 11);
//steps Per Revolution = 2048; // change this to fit the number of steps per revolution
//Revs Per Minute = 6; // Adjustable range of 28BYJ-48 stepper is 0~17 rpm
Serial.begin(9600);
//Serial.println("IR Receiver Button Decode");
//irrecv.enableIRIn(); // Start the receiver
// setup the pins on the microcontroller:
pinMode(motor_pin_1, OUTPUT);
pinMode(motor_pin_2, OUTPUT);
pinMode(motor_pin_3, OUTPUT);
pinMode(motor_pin_4, OUTPUT);
}
//======End Setup=======================================================
//=====Main Loop==================
void loop() {
Serial.println("loop");
stepper(roles_Per_Minute);
}
//=====End Main Loop===============