Push buton, stepper + dc

Hi

I’m trying to to figure out hove to turn on a stepper motor and a dc motor by using push button. The idea is that when i press the button the stepper motor an dc motor turns on and when i press it again they turn off. I can’t get it to work. The code that i’m trying to get to work is belowe.

#include <Stepper.h> // Include the header file
// change this to the number of steps on your motor
#define STEPS 64
// create an instance of the stepper class using the steps and pins
Stepper stepper(STEPS, 8, 10, 9, 11);


int w1 = 5; // wiatrak pwm
int w2 = 4;
int w3 = 7;
int val = 100;

//buttons

const int on = 2;
//const int plus = A1;
//const int minus = A2;
//int kon = 13; //pin kontrolny   

int vel2 = 300; // naped
int vel1 = 150; //wiatrak

int buttonState = 0;
int flag=0;

void setup()

{
Serial.begin(9600);
pinMode(w1, OUTPUT);
// pinMode(on, INPUT);
pinMode(on, INPUT_PULLUP); 
// pinMode(plus, INPUT);
// pinMode(minus, INPUT);   


}

void loop()
{ 
 buttonState = digitalRead(on);
 
  //stepper.step(val+10);
  
 //If button pressed...
 if (buttonState == LOW) { 
   //...ones, turn led on!
   if ( flag == 0){
     Serial.println("Both Motor goes ON");
     analogWrite(w1, vel1); // Sets speed variable via PWM
     digitalWrite(w2, HIGH);
     digitalWrite(w3, LOW);
     stepper.setSpeed(vel2);
     stepper.step(val+10);
     flag=1; //change flag variable
}
 else if ( flag == 1){
Serial.println("Both Motor goes OFF");
     analogWrite(w1, 0); // Sets speed variable via PWM
     digitalWrite(w2, LOW);
     digitalWrite(w3, LOW);
     stepper.setSpeed(0);
     stepper.step(0); 
  flag=0; //change flag variable again 
 }
 //delay(200); //Small delay
}

}

The next step is to use two more buttons to increase and decrease rotation speed.

Please help

The standard Stepper library may not be suitable for what you want because stepper.step(val+10); will block the Arduino until it completes which would prevent another button press from stopping the motors.

If you want to be able to interrupt the motion you should use the AccelStepper library

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code looks like this and is easy to copy to a text editor. See How to use the Forum

...R

Thanks. I will try that library. Sorry for howe i posted code, i whose looking for that option.

AccelStepper has a stop() method useful for cancelling the current movement.

Doing stepper.move(1000000000L) is likely to be sufficent for most purposes in starting
motion (almost) indefinitely till the next stop().

Ok. So I didyn’t use AccelStepper library but i manage to get it to work with regular Stepper library.

#include <Stepper.h> // Include the header file
// change this to the number of steps on your motor
#define STEPS 32
// create an instance of the stepper class using the steps and pins
Stepper stepper(STEPS, 8, 10, 9, 11);


int w1 = 5; // wiatrak pwm
int w2 = 4;
int w3 = 7;
int val = 100;

//buttons

const int on = A0;
const int plus = A1;
const int minus = A2;
//int kon = 13; //pin kontrolny   

int vel2 = 700; // naped
int vel1 = 150; //wiatrak

int buttonState = 0;
int flag=0;
int buttonflag=LOW;

int buttonStatePlus = 0;
int flagPlus=0;
int buttonFlagPlus=LOW;

int buttonStateMinus = 0;
int flagMinus=0;
int buttonFlagMinus=LOW;

void setup()

{
 Serial.begin(9600);
 pinMode(w1, OUTPUT);
// pinMode(on, INPUT);
pinMode(on, INPUT_PULLUP); 
pinMode(plus, INPUT);
pinMode(minus, INPUT);   
 

}

void loop()
{     
  buttonflag=buttonState;   // zapamiętanie poprzedniego stanu porzycisku 
  
  
  buttonState = digitalRead(on);

  buttonflag=buttonStatePlus;
  buttonflag=buttonStateMinus;
  buttonStatePlus = digitalRead(plus);
  buttonStateMinus = digitalRead(minus);

 if (buttonFlagPlus!=buttonStatePlus)   //sprawdzamy czy nastąpiła zmiana stanu przycisku
   {
    if (buttonStatePlus == HIGH) flagPlus=!flagPlus;
   }
   // }
    //...ones, turn led on!
    if ( flagPlus == 1){
    Serial.println("Plus");
    vel1 = vel1 + 10;
    analogWrite(w1, vel1);
    vel2 = vel2 + 50;
    stepper.setSpeed(vel2);
    flagPlus=!flagPlus;
   //   flag=1; //change flag variable
}



 else if (buttonFlagMinus!=buttonStateMinus)   //sprawdzamy czy nastąpiła zmiana stanu przycisku
   {
    if (buttonStateMinus == HIGH) flagMinus=!flagMinus;
   }
   // }
    //...ones, turn led on!
    if ( flagMinus == 1){
    Serial.println("Minus");
    vel1 = vel1 - 10;
    analogWrite(w1, vel1);
    vel2 = vel2 - 50;
    stepper.setSpeed(vel2);
    flagMinus=!flagMinus;
   //   flag=1; //change flag variable
    
  }
  
   delay(5);
   if (buttonflag!=buttonState)   //sprawdzamy czy nastąpiła zmiana stanu przycisku
   {
    if (buttonState == HIGH) flag=!flag;
   }
   // }
    //...ones, turn led on!
    if ( flag == 1){
      Serial.println("Both Motor goes ON");
      analogWrite(w1, 250);
      analogWrite(w1, vel1); // Sets speed variable via PWM
      digitalWrite(w2, HIGH);
      digitalWrite(w3, LOW);
      
      stepper.setSpeed(vel2);
      stepper.step(val+10);
      digitalWrite(8, HIGH);
      digitalWrite(9, HIGH);
      digitalWrite(10, HIGH);
      digitalWrite(11, HIGH);
   //   flag=1; //change flag variable
}
 else if ( flag == 0){
Serial.println("Both Motor goes OFF");
      analogWrite(w1, 0); // Sets speed variable via PWM
      digitalWrite(w2, LOW);
      digitalWrite(w3, LOW);
      
      stepper.setSpeed(0);
      //stepper.step(0); 
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
      digitalWrite(11, LOW);
  // flag=0; //change flag variable again 
  }

The only problem is that it dosen’t responde to off signal. In serial monitor it seams that it is reciving the signal but immediately changes it to on.

A nother thing is tat the stepper motor is geting hot but i assume thats normal.

What stepper motor are you using?

What stepper motor driver are you using?

How are you powering the motor? (power supply volts and amps)

...R

I'm using 28BYJ-48 stepper motor with ULN2003 driver. Power supply 10v 2A.

My 28BYJ-48 is rated for 5v. Is yours different? (I think there may be 12v versions).

...R

Driver is rated 5-12v so i assumed that motor is also. There are two versions 28BYJ-48-5 5V and 28BYJ-48-12 12V. I have to check it.

I hecked the stepper motor and it’s 5v, so I conected it that way and it’s ok. Also I modified the code for AccelStepper library.

#include <AccelStepper.h> // Include the header file
// change this to the number of steps on your motor

// create an instance of the stepper class using the steps and pins
AccelStepper stepper(4,2,9,10,12); 


int w1 = 5; // wiatrak pwm
int w2 = 4;
int w3 = 7;
//int val = 50;

//buttons

const int on = A0;
const int plus = A1;
const int minus = A2;  

int vel2 = 500; // naped
int vel1 = 58; //wiatrak

int buttonState = 0;
int flag=0;
int buttonflag=LOW;

int buttonStatePlus = 0;
int flagPlus=0;
int buttonFlagPlus=LOW;

int buttonStateMinus = 0;
int flagMinus=0;
int buttonFlagMinus=LOW;

void setup()

{
 Serial.begin(9600);
 pinMode(w1, OUTPUT);
// pinMode(on, INPUT);
pinMode(on, INPUT); 
pinMode(plus, INPUT);
pinMode(minus, INPUT);
stepper.setMaxSpeed(1000);
stepper.setSpeed(vel2);
stepper.setAcceleration(500);

}

void loop()
{     

 
  buttonflag=buttonState;   // zapamiętanie poprzedniego stanu porzycisku 
  
  
  buttonState = digitalRead(on);



  
   //delay(100);
   if (buttonflag!=buttonState)   //sprawdzamy czy nastąpiła zmiana stanu przycisku
   {
    if (buttonState == HIGH) flag=!flag;
   }
   // }
    //...ones, turn led on!
    if ( flag == 1){
      Serial.println("Both Motor goes ON");
      analogWrite(w1, vel1); // Sets speed variable via PWM
      digitalWrite(w2, HIGH);
      digitalWrite(w3, LOW);
      stepper.setSpeed(vel2);  
      stepper.runSpeed();
}
 else if ( flag == 0){
Serial.println("Both Motor goes OFF");
      analogWrite(w1, 0); // Sets speed variable via PWM
      digitalWrite(w2, LOW);
      digitalWrite(w3, LOW);
   
     stepper.stop();
     digitalWrite(2, LOW);
     digitalWrite(9, LOW);
     digitalWrite(10, LOW);
     digitalWrite(12, LOW);
  }
  
   
    if ( digitalRead(plus)){
    Serial.println("Plus");
    vel1 = vel1 + 5;
    analogWrite(w1, vel1);
    //vel2 = vel2 + 50;
   stepper.setSpeed(vel2+50);
   //stepper.runSpeed();
    delay(100);
}
 


 else if (digitalRead(minus)) //sprawdzamy czy nastąpiła zmiana stanu przycisku
{
    Serial.println("Minus");
    vel1 = vel1 - 5;
    analogWrite(w1, vel1);
    //vel2 = vel2 - 50;
   stepper.setSpeed(vel2-50);
    //stepper.runSpeed();
     delay(100);
  }
   
  
}

Its working fine. Turns on and off. But when i want to ajust speed te dc motor is responding but the stepper dosent.

pea_not:
But when i want to ajust speed te dc motor is responding but the stepper dosent.

Please post the program that demonstrates that problem.

What are you using to indicate to the Arduino that the speed should change?

...R

It whose in previous post

#include <AccelStepper.h> // Include the header file
// change this to the number of steps on your motor

// create an instance of the stepper class using the steps and pins
AccelStepper stepper(4,2,9,10,12);


int w1 = 5; // wiatrak pwm
int w2 = 4;
int w3 = 7;
//int val = 50;

//buttons

const int on = A0;
const int plus = A1;
const int minus = A2; 

int vel2 = 500; // naped
int vel1 = 58; //wiatrak

int buttonState = 0;
int flag=0;
int buttonflag=LOW;

int buttonStatePlus = 0;
int flagPlus=0;
int buttonFlagPlus=LOW;

int buttonStateMinus = 0;
int flagMinus=0;
int buttonFlagMinus=LOW;

void setup()

{
 Serial.begin(9600);
 pinMode(w1, OUTPUT);
// pinMode(on, INPUT);
pinMode(on, INPUT);
pinMode(plus, INPUT);
pinMode(minus, INPUT);
stepper.setMaxSpeed(1000);
stepper.setSpeed(vel2);
stepper.setAcceleration(500);

}

void loop()
{     

 
  buttonflag=buttonState;   // zapamiętanie poprzedniego stanu porzycisku
 
 
  buttonState = digitalRead(on);



 
   //delay(100);
   if (buttonflag!=buttonState)   //sprawdzamy czy nastąpiła zmiana stanu przycisku
   {
    if (buttonState == HIGH) flag=!flag;
   }
   // }
    //...ones, turn led on!
    if ( flag == 1){
      Serial.println("Both Motor goes ON");
      analogWrite(w1, vel1); // Sets speed variable via PWM
      digitalWrite(w2, HIGH);
      digitalWrite(w3, LOW);
      stepper.setSpeed(vel2); 
      stepper.runSpeed();
}
 else if ( flag == 0){
Serial.println("Both Motor goes OFF");
      analogWrite(w1, 0); // Sets speed variable via PWM
      digitalWrite(w2, LOW);
      digitalWrite(w3, LOW);
   
     stepper.stop();
     digitalWrite(2, LOW);
     digitalWrite(9, LOW);
     digitalWrite(10, LOW);
     digitalWrite(12, LOW);
  }
 
   
    if ( digitalRead(plus)){
    Serial.println("Plus");
    vel1 = vel1 + 5;
    analogWrite(w1, vel1);
    //vel2 = vel2 + 50;
   stepper.setSpeed(vel2+50);
   //stepper.runSpeed();
    delay(100);
}
 


 else if (digitalRead(minus)) //sprawdzamy czy nastąpiła zmiana stanu przycisku
{
    Serial.println("Minus");
    vel1 = vel1 - 5;
    analogWrite(w1, vel1);
    //vel2 = vel2 - 50;
   stepper.setSpeed(vel2-50);
    //stepper.runSpeed();
     delay(100);
  }
   
 
}

The buttons plus minus. When i press plus button it jumps by 5 for dc motor and bu 50 for stepper and the other way around for minus button.

If you want the AccelStepper library to work properly you should not have any delay()s in your code. The calls to runSpeed() should happen as frequently as possible. If you need an interval use millis() to manage timing without blocking as illustrated in Several Things at a Time.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

I suggest that you make a copy of the program and take out all the stuff for the DC motor so that you can concentrate on the stepper motor.

I also suggest that you create a separate function that reads all the switches once in each iteration of loop() and saves their values for use elsewhere in the program. I would also create separate functions that control each motor as that makes it much easier to test the different parts separately. Have a look at Planning and Implementing a Program

Using the AutoFormat tool to indent your code consistently will make it much easier to read and to see at a glance where each block starts and ends.

...R

pea_not:
Driver is rated 5-12v so i assumed that motor is also. There are two versions 28BYJ-48-5 5V and 28BYJ-48-12 12V. I have to check it.

Can't assume that at all. In fact an ULN2003 is rated for upto 50V, not just 12V.

Running a 5V motor at 12V will probably burn it up, as nearly 6 times as much heat is
dissipated in the windings. Perhaps the motor is 12V and all is OK?

Yes I fixed that

I hecked the stepper motor and it's 5v, so I conected it that way and it's ok.

. A mistake that could burn stepper, luckily it didn't.

A nother thing the stepper i currently rotating in much lower speed than it should. I checked it on accelstepper example and it worked fine.