Hi,
I am a newbie or a beginner in Arduino programming and for the code below I am using an Arduino UNO and 28BYJ-48 motor and a ULN2003 driver board for a project that I am interested in. The code is given below.
const int CW =1;
const int CCW =2;
const int STOP =3;
int Pin1 = 8;//IN1 is connected to 8
int Pin2 = 9;//IN2 is connected to 9
int Pin3 = 10;//IN3 is connected to 10
int Pin4 = 11;//IN4 is connected to 11
int switchSTOP =2;//define input pin for STOP push button
int stopType=2;//1=normal stop, 2=hold stop (consumes power)
int speedFactor =1;//1=fastest, 2=slower or 3 more slower
long angles[] = {90,180,270};//angles of each push button
int pushButtons[] ={3, 4, 5};//digial pin for each push button
int directions[] ={CCW, CCW, CCW};//direction of eacch push button
int speedFactors[] = {1, 1, 1};//speed for each push button
int correction_CW = 150;//watch video for details
int correction_CCW = 150;//watch video for details
int poleStep = 0;
long stepVale =0;
const int SPR=64*64;
long goToAngle=0;
int activeButton=0;
int pole1[] ={0,0,0,0, 0,1,1,1, 0};//pole1, 8 step values
int pole2[] ={0,0,0,1, 1,1,0,0, 0};//pole2, 8 step values
int pole3[] ={0,1,1,1, 0,0,0,0, 0};//pole3, 8 step values
int pole4[] ={1,1,0,0, 0,0,0,1, 0};//pole4, 8 step values
int count=0;
int dirStatus = STOP;// stores direction status 3= stop (do not change)
void setup()
{
//Robojax.com Stepper Push button Any Angle STPB-5
Serial.begin(9600);
Serial.begin("Robojax Video for Stepper Motor STPB-2");
pinMode(Pin1, OUTPUT);//define pin for ULN2003 in1
pinMode(Pin2, OUTPUT);//define pin for ULN2003 in2
pinMode(Pin3, OUTPUT);//define pin for ULN2003 in3
pinMode(Pin4, OUTPUT);//define pin for ULN2003 in4
pinMode(switchSTOP,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(switchSTOP), stopMotor, FALLING );
// see this https://www.arduino.cc/reference/en/language/variables/utilities/sizeof/
for (byte i = 0; i < (sizeof(pushButtons) / sizeof(pushButtons[0])); i++) {
pinMode(i,INPUT_PULLUP);
}
} //setup
void loop()
{
stepVale = (SPR * goToAngle)/360 ;
//Robojax.com Stepper Push button Any Angle STPB-5
//see this https://www.arduino.cc/reference/en/language/variables/utilities/sizeof/
for (byte i = 0; i < (sizeof(angles) / sizeof(angles[0])); i++) {
if(digitalRead(pushButtons[i]) == LOW)
{
goToAngle =angles[i];
dirStatus =directions[i];
count =0;
activeButton =i;
}// if ends
}//for loop ends
if(dirStatus ==CCW){
poleStep++;
count++;
if(count+correction_CCW <= stepVale)
{
driveStepper(poleStep);
}else{
stopMotor();
}
//full explannation at Arduino Course on Udemy.com see link above
}else if(dirStatus ==CW){
poleStep--;
count++;
if(count+correction_CW <=stepVale)
{
driveStepper(poleStep);
}else{
stopMotor();
}
}else{
stopMotor();
}
if(poleStep>7){
poleStep=0;
}
if(poleStep<0){
poleStep=7;
}
delay(speedFactors[activeButton]);
//Robojax.com Stepper Push button Any Angle STPB-5
}// loop
/*
* @brief moves motor to specific angle
* @param "angle" is integer representing the angle
* @return does not return anything
*
* www.Robojax.com code Ap1il 19 2020 at 01:22 in Ajax, Ontario, Canada
*/
void driveStepper(int c)
{
//Robojax.com Stepper Push button Any Angle STPB-5
digitalWrite(Pin1, pole1[c]);
digitalWrite(Pin2, pole2[c]);
digitalWrite(Pin3, pole3[c]);
digitalWrite(Pin4, pole4[c]);
}//driveStepper ends here
/*
* @brief stops the motor immediately
* @param none
* @return does not return anything
*
* www.Robojax.com code June 09, 2020 at 11:09 in Ajax, Ontario, Canada
*/
void stopMotor()
{
//see this https://www.arduino.cc/reference/en/language/variables/utilities/sizeof/
for (byte i = 0; i < (sizeof(angles) / sizeof(angles[0])); i++) {
digitalWrite(pushButtons[i], HIGH);
}//for loop ends
dirStatus = STOP;
if( stopType ==2)
{
driveStepper(8);
}
}//stopMotor()
The code above helps the stepper motor to go to any specified angle in two directions. One is in Clockwise Direction and the other is CounterClockWise direction, you can find the abbreviation in the code as CW and CCW respectively.
The problem is that I want my code to go to the specified angle in the CW direction and come back to the initial position in the CCW direction with a help of a push-buttons.
For example, I want the stepper motor to go to 90 degrees in CW direction and then CCW back which means back to 0 degrees(initial position).
The problem here is that each push-button will only execute in CW or CCW but I want one push button to execute both CW and CCW direction and it should come back to its initial position after the motor has rotated to the specified angle.
For reference, you can use this link.(click here)
I personally think that changing of CW and CCW direction is possible in this segment of the above code.
if(dirStatus ==CCW){
poleStep++;
count++;
if(count+correction_CCW <= stepVale)
{
driveStepper(poleStep);
}else{
stopMotor();
}
//full explannation at Arduino Course on Udemy.com see link above
}else if(dirStatus ==CW){
poleStep--;
count++;
if(count+correction_CW <=stepVale)
{
driveStepper(poleStep);
}else{
stopMotor();
}
}else{
stopMotor();
}
I will also attach new.ino file if there are any issues in copy-pasting from the post itself.
Any help is much appreciated.
new.ino (3.93 KB)