28byj48 stepper motor speed select?

hello can someone take a quick look at this code trying to select speed via switch that latches i know i have something wrong but i cant see??it

/*
 
 28-byj-48 stepper motor control uln2003 driver board 
arduino uno ky040 encoder 
this will use a switch on pin7 - gnd to select  run fast for course selection and slow for fine control 
*/

#include "Stepper.h"
#define STEPS  32   // Number of steps for one revolution of Internal shaft
                    // 2048 steps for one revolution of External shaft
 
volatile boolean TurnDetected;  // need volatile for Interrupts
volatile boolean rotationdirection;  // CW or CCW rotation
 
const int PinCLK=2;   // Generating interrupts using CLK signal
const int PinDT=3;    // Reading DT signal
const int PinSW=4;    // Reading Push Button switch

float buttonPin = 7;    // the pin that the pushbutton is attached to select speed

int RotaryPosition=0;    // To store Stepper Motor Position

int PrevPosition;     // Previous Rotary position Value to check accuracy

int StepsToTake;
 
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button


// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4
Stepper small_stepper(STEPS, 8, 10, 9, 11);
 
// Interrupt routine runs if CLK goes from HIGH to LOW
void isr ()  {
  delay(4);  // delay for Debouncing
  if (digitalRead(PinCLK))
    rotationdirection= digitalRead(PinDT);
  else
    rotationdirection= !digitalRead(PinDT);
  TurnDetected = true;
}
 
void setup ()  {
 
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);//to select slow step
  digitalWrite(buttonPin, HIGH); // Pull-Up resistor for switch
  
   
  
pinMode(PinCLK,INPUT);
pinMode(PinDT,INPUT);  
pinMode(PinSW,INPUT);
digitalWrite(PinSW, HIGH); // Pull-Up resistor for switch
attachInterrupt (0,isr,FALLING); // interrupt 0 always connected to pin 2 on Arduino UNO
}
 
void loop ()  {

 // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
   if(buttonPin); (HIGH); small_stepper.setSpeed(70);
  (buttonPin); (LOW); small_stepper.setSpeed(200);
    
  //small_stepper.setSpeed(200);
  //small_stepper.setSpeed(70); //Max seems to be 700
  if (!(digitalRead(PinSW))) {   // check if button is pressed
    if (RotaryPosition ==0) {  // check if button was already pressed
    } else {
        small_stepper.step(-(RotaryPosition*20));
        RotaryPosition=0; // Reset position to ZERO
      }
    }
 
  // Runs if rotation was detected
  if (TurnDetected)  {
    PrevPosition = RotaryPosition; // Save previous position in variable
    if (rotationdirection) {
      RotaryPosition=RotaryPosition-1;} // decrease Position by 1
    else {
      RotaryPosition=RotaryPosition+1;} // increase Position by 1
 
    TurnDetected = false;  // do NOT repeat IF loop until new rotation detected
  }
  
  
  // Which direction to move Stepper motor
    if ((PrevPosition + 1) == RotaryPosition) { // Move motor CW
  
     StepsToTake=1; // fine tune steps and same below
      small_stepper.step(StepsToTake);
     
    }
 
    if ((RotaryPosition + 1) == PrevPosition) { // Move motor CCW
     
      StepsToTake=-1;
      small_stepper.step(StepsToTake);
     
    }
  }

Looking at the code the configuration uses:

  1. a push button at pin 4 (to let the stepper run)
  2. a switch at pin 7 (to select another speed)
  3. a rotary encoder at interrupt pins 2 and 3
  4. the stepper motor at pins 8, 10, 9, 11

First check that you wired everything that way.
At that point I would normally suggest to use an external power supply for the motor, but I think the motor draws only less than 250mA, so that would be ok powering from +5V of the Arduino for doing first tests. But carefully watch the temperature of Arduino's built-in voltage regulator (bottom side of the pcb). If you extend your tests for quite some time it might get hot and then you should go with powering the stepper with an external 5V power supply.

The overall speed will still be very low, even in the fastest mode as the stepper comes with built in gear reduction ( read the comment in your(?) code which says that it needs 2048 steps for one full revolution of the external shaft) - so don't expect it running fast.

float buttonPin = 7;    // the pin that the pushbutton is attached to select speed

Is the number 7 a float ? Will the value of buttonPin ever change in the program ?

Why not

const byte buttonPin = 7;    // the pin that the pushbutton is attached to select speed

While you are at it, make all the other pin numbers const byte as well.

firstly thanks for the reply the code was scrapped got start from scratch using accelstepper library instead so thanks will keep learning

hello got my sketch to work but need a little help if possible i want to add a second speed choice on push to latch switch
i played around with a few ideas but brain fryed any suggestions

code

/*
simple push button control of 28 BYJ-48 stepper motor and uln2003 driver all 5v
for any manual control application
all works ok but how to add second speed ???
*/

#include <AccelStepper.h>
#define HALFSTEP 8

//declare variables for the motor pins
int motorPin1 = 8; // Blue - 28BYJ48 pin 1
int motorPin2 = 9; // Pink - 28BYJ48 pin 2
int motorPin3 = 10; // Yellow - 28BYJ48 pin 3
int motorPin4 = 11; // Orange - 28BYJ48 pin 4
// Red - 28BYJ48 pin 5 (VCC)

// The sequence 1-3-2-4 required for proper sequencing of 28BYJ48
//AccelStepper stepper2(FULLSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

AccelStepper stepper2(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

int cwpin = 3; //digital pin number for the cw button
int ccwpin = 4; //digital pin number for the ccw button
int spdpin = 7; //digital pin to select speed fast / slow

void setup() {

stepper2.setMaxSpeed(1000);
stepper2.setAcceleration(10);
stepper2.setMinPulseWidth(100);
stepper2.setSpeed(500);

pinMode(cwpin, INPUT_PULLUP);
pinMode(ccwpin, INPUT_PULLUP);
pinMode(spdpin, INPUT_PULLUP); //added for speed selection

}

void loop() {

if (digitalRead(cwpin) == LOW) { // if the input is LOW (button pushed)
// need to add line to ignore ccw button while cw pressed and same below ??
CW(); } //rotates cw direction

if (digitalRead(ccwpin) == LOW) { // if the input is LOW (button pushed)

CCW(); } } //rotates ccw direction

void CW(){

while (digitalRead(cwpin) == LOW) {
stepper2.setSpeed(100);
stepper2.runSpeed();} }

void CCW(){

while (digitalRead(ccwpin) == LOW) {
stepper2.setSpeed(-100);
stepper2.runSpeed();} }

many thanks

Try this mod, UNTESTED:

#include <AccelStepper.h>
#define HALFSTEP 8

//declare variables for the motor pins
int motorPin1 = 8;      // Blue   - 28BYJ48 pin 1
int motorPin2 = 9;      // Pink   - 28BYJ48 pin 2
int motorPin3 = 10;     // Yellow - 28BYJ48 pin 3
int motorPin4 = 11;     // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)
 
// The sequence 1-3-2-4 required for proper sequencing of 28BYJ48
//AccelStepper stepper2(FULLSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

AccelStepper stepper2(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

int cwpin = 3;    //digital pin number for the cw button
int ccwpin = 4;  //digital pin number for the ccw button
int spdpin = 7;   //digital pin to select speed fast / slow
unsigned int spEEd; //NEW

void setup() {
 
  stepper2.setMaxSpeed(1000);
  stepper2.setAcceleration(10);
  stepper2.setMinPulseWidth(100);
  stepper2.setSpeed(500);
       
  pinMode(cwpin, INPUT_PULLUP);
  pinMode(ccwpin, INPUT_PULLUP);
  pinMode(spdpin, INPUT_PULLUP); //added for speed selection
   
}

void loop() {
 if(digitalRead(spdpin)) //NEW
   spEEd = 100;          //NEW
 else                    //NEW
   spEEd = 50;           //NEW   
 
 if (digitalRead(cwpin) == LOW) { //  if the input is LOW (button pushed)
         // need to add line to ignore ccw button while cw pressed and same below ??
 CW(); } //rotates cw direction
                           
  if (digitalRead(ccwpin) == LOW) { //  if the input is LOW (button pushed)
 
 CCW(); } } //rotates ccw direction

 
void CW(){

    while (digitalRead(cwpin) == LOW) {
    stepper2.setSpeed(spEEd); //NEW
    stepper2.runSpeed();}   }
 

void CCW(){

    while (digitalRead(ccwpin) == LOW) {
    stepper2.setSpeed(-spEEd); //NEW
    stepper2.runSpeed();}  }

Pls leave double posting and read the forum rules about how to post code using code tags (</>).

You started with https://forum.arduino.cc/index.php?topic=417368.0

Ask a moderator (click on the “Report to Moderator” link at the lower left of each post) to join the two threads to keep everything in one logical order and location. Thanks.

see my comments here to avoid double posting.

Threads merged.

first thanks outsider for the help needed to rework a little Hooray my first attempt at coding is working at last practical application helps me understand how things work especially coding never done it before never needed to learn so here’s to many more headaches thanks guys

sorted two speed stepper control



#include <AccelStepper.h>
#define HALFSTEP 8

//declare variables for the motor pins
int motorPin1 = 8;      // Blue   - 28BYJ48 pin 1
int motorPin2 = 9;      // Pink   - 28BYJ48 pin 2
int motorPin3 = 10;     // Yellow - 28BYJ48 pin 3
int motorPin4 = 11;     // Orange - 28BYJ48 pin 4
// Red    - 28BYJ48 pin 5 (VCC)

// The sequence 1-3-2-4 required for proper sequencing of 28BYJ48
//AccelStepper stepper2(FULLSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

AccelStepper stepper2(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);



int cwpin = 3;    //digital pin number for the cw button
int ccwpin = 4;  //digital pin number for the ccw button
int spdpin = 7;   //digital pin to select speed fast / slow
float  spEEd; //changed to float for fwd rev selection

void setup() {

  stepper2.setMaxSpeed(1000);
  stepper2.setAcceleration(10);
  stepper2.setMinPulseWidth(100);
 
  pinMode(cwpin, INPUT_PULLUP);
  pinMode(ccwpin, INPUT_PULLUP);
  pinMode(spdpin, INPUT_PULLUP); //added for speed selection

}

void loop() {
  if (digitalRead(spdpin) == LOW) spEEd = (200); 
  
  if (digitalRead(spdpin) == HIGH) spEEd = (30); 



  
    if (digitalRead(cwpin) == LOW)  //  if the input is LOW (button pushed)

    { CW();//rotates cw direction
    }

    if (digitalRead(ccwpin) == LOW){ //  if the input is LOW (button pushed)

      CCW  ();
      }  //rotates ccw direction


}

  void CW() {

    while (digitalRead(cwpin) == LOW)  
   
  {    stepper2.setSpeed(spEEd); 
      stepper2.runSpeed();
    
  }
  }

  void CCW() {

    while (digitalRead(ccwpin) == LOW)
   
     { 
        stepper2.setSpeed(-spEEd); //note - for negative 
        stepper2.runSpeed();
      }
  }

and what does your last post tell us do you need more help or is everything fine and wouldn't it be easier for all of us if you could remember of what you apparently forgot what you learnt at school using sentences with clarity and readability and not like so:
perhaps you dont always need to use commas periods colons etc to make sentences clear when i am in a hurry tired cold lazy or angry i sometimes leave out punctuation marks grammar is stupid i can write without it and dont need it my uncle Harry once said he was not very clever and i never understood a word he wrote to me i think ill learn some punctuation not too much enough to write to Uncle Harry he needs some help

Hooray my first attempt at coding is working

Good for you, now improve it.

For instance, any variables that will never change in the program could be declared as const and any variable whose value will never exceed 255 could be declared as byte. The former will prevent accidental changes being made and the latter will save memory. Neither are likely in your current program but it is good practice to use both techniques and it could pay off in larger, more complicated programs.

thanks for the comment UKHelibob
i first needed to get the code working at two speeds by selecting the correct buttons the project is to control a small magnetic transmitting loop antenna which needs very fine tuning this can be achieved in several ways mechanically by reduction gear slow motion drive ,but thought that a stepper motor might be more practical hence my fun and games
the next part is to include a count function ,for positional reference home position and lcd display with some information
so for the moment thanks to all for the help the last above code has been tested and is ok