Stepper motor now runs very slow....

I found this sketch from RoboJax, which is almost what I need.
Insted of the buttons latching on, and needing a stop button, I want them to be push to run, stop on release, so I changed the line:-

else if(digitalRead(switchStop) == LOW)

to:-

else if (digitalRead(switchCCW) == HIGH && digitalRead(switchCW) == HIGH)

It makes the buttons work the way that I want, but the motor now rotates at about 1/10 the speed that it does with the original sketch.

Why???

Thanks for your help.

 /*
 * This is Arduino code to control 28BYj-48 with ULN2003 board using
 3 push buttons.  
 * Written by Ahmad Shamshiri for Robojax Robojax.com
 * on Feb 20, 2019 at 19:34 in Ajax, Ontario, Canada
 Watch the video instruction for this sketch: https://youtu.be/q-H7GbyX5ho
 
You can get the wiring diagram from my Arduino Course at Udemy.com
Learn Arduino step by step with all library, codes, wiring diagram all in one place
visit my course now http://robojax.com/L/?id=62

If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can support me on Patreon http://robojax.com/L/?id=63
or make donation using PayPal http://robojax.com/L/?id=64

* 
 * Code is available at http://robojax.com/learn/arduino

 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. 

*/

int Pin1 = 10;//IN1 is connected to 10 
int Pin2 = 11;//IN2 is connected to 11  
int Pin3 = 12;//IN3 is connected to 12  
int Pin4 = 13;//IN4 is connected to 13 
int switchCW  =2;//define input pin for CW push button
int switchStop=3;//define input pin for Stop push button
int switchCCW =4;//define input pin for CCW push button

 
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 poleStep = 0; 
int  dirStatus = 3;// stores direction status 3= stop (do not change)

void setup() 
{
Serial.begin(9600);
 
 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(switchCW,INPUT_PULLUP);// CW push button pin as input
 pinMode(switchStop,INPUT_PULLUP);//Stop push button pin as input 
 pinMode(switchCCW,INPUT_PULLUP);//CCW push button pin as input

} 
 void loop() 
{ 
Serial.println("DIR-Status");
Serial.println(dirStatus);
  
  if(digitalRead(switchCCW) == LOW) 
  {
    dirStatus =1;
  }else if(digitalRead(switchCW) == LOW)
  {
   dirStatus  = 2;  
  }
  else if (digitalRead(switchCCW) == HIGH && digitalRead(switchCW) == HIGH)
  
  // else if(digitalRead(switchStop) == LOW)
  {
    dirStatus =3; 
  }
 if(dirStatus ==1){ 
   poleStep++; 
    driveStepper(poleStep);    
 }else if(dirStatus ==2){ 
   poleStep--; 
    driveStepper(poleStep);    
 }else{
  driveStepper(8);   
 }
 if(poleStep>7){ 
   poleStep=0; 
 } 
 if(poleStep<0){ 
   poleStep=7; 
 } 
 delay(1); 

}// loop



/*
 * @brief sends signal to the motor
 * @param "c" is integer representing the pol of motor
 * @return does not return anything
 * 
 * www.Robojax.com code June 2019
 */
void driveStepper(int c)
{
     digitalWrite(Pin1, pole1[c]);  
     digitalWrite(Pin2, pole2[c]); 
     digitalWrite(Pin3, pole3[c]); 
     digitalWrite(Pin4, pole4[c]);   
}//driveStepper end here

I suspect the problem is that most of the time the two switches will be HIGH and, as there is no code for dirStatus =3 nothing will happen

If you use the AutoFormat tool it will make your code much easier to read.

...R

Thanks for the reply

I suspect the problem is that most of the time the two switches will be HIGH and, as there is no code for dirStatus =3 nothing will happen

I think this is the same for the original and my modification, The motor runs one way when dirStatus =1 and the other way when dirStatus =2, so 3 is just not 1 or 2 and it stops.

Why would my modification make the motor run slow.??

If you use the AutoFormat tool it will make your code much easier to read.

Please explain, I am new to this.

BC

Barry_Cole:
so 3 is just not 1 or 2 and it stops.

Not really. If you watch the video he presses one p/b direction (momentarily).
After this both direction p/b's are pulled high and motor continues in that direction.

He pushes the other p/b direction (momentarily)and same applies only with motor in other direction.

To stop he pushes stop p/b, again momentarily.

Thanks again.

As I see it dirStatus will stay the same unless it is told otherwise. One button tells it to =1, One button tells it to =2, One button tells it to =3. I think 3 could be any number as long as it is not 1 or 2.

However, why does my modification to the sketch, slow down the motor.

BC

Try commenting out the 2 Serial.print()s on every loop:

//Serial.println("DIR-Status");
//Serial.println(dirStatus);

Barry_Cole:
As I see it dirStatus will stay the same unless it is told otherwise. One button tells it to =1, One button tells it to =2, One button tells it to =3. I think 3 could be any number as long as it is not 1 or 2.

Yes.

But these lines

else if (digitalRead(switchCCW) == HIGH && digitalRead(switchCW) == HIGH)
{
    dirStatus =3; 
}

effectively tell it to stay at 3 all the time.

Please explain, I am new to this.

In the Arduino IDE select Tools/Auto Format

...R

JCA79B:
Try commenting out the 2 Serial.print()s on every loop:

//Serial.println("DIR-Status");

//Serial.println(dirStatus);

That was indeed the problem, I actually took out:- Serial.begin(9600); and that sorted it.

Many thanks.

BC

Robin2:
Yes.

But these lines

else if (digitalRead(switchCCW) == HIGH && digitalRead(switchCW) == HIGH)

{
   dirStatus =3;
}



effectively tell it to stay at 3 all the time.

...R

NO.... IT is only 3 IF the two switch inputs are both OFF (High)

BC

Problem sorted, issue now closed.

Thanks to you all for your help.

Barry_Cole:
NO.... IT is only 3 IF the two switch inputs are both OFF (High)

Isn't that the situation 99% of the time?

Problem sorted, issue now closed.

Please explain how you solved the problem for the benefit of other readers.

...R

OK,
The slow running was sorted out by JCA79B, see his post above. I was not aware, but the println commands slow the whole program down, this was confirmed by Ahmed at robojax.

The (non) issue with the dirStatus being 3 most of the time is sort of correct, as the motor is normally stopped.

My modification was to change to directional push buttons from a "latching" function to a "Push to run"
in my sketch the motor only runs while the button is being held (dirStatus being 1 or 2), as soon as the buttons are released the motor stops. (dirStatus being 3).

Trust that is clear as mud.

Bye for now. BC

Thanks for the update.

...R