AccelStepper set multiple objects with setMaxSpeed

Hello,
How would I set multiple drivers to a setMaxSpeed variable because right now, I have this:

w2.setMaxSpeed(3000);
w3.setMaxSpeed(3000);
w4.setMaxSpeed(3000);

but I want to optimize it so the sketch is smaller. I was thinking of something like this:

w2, w3, w4.setMaxSpeed(3000);

but im not sure if that will work or not.

Thanks

324hz
win21H2
he/him

Didn't the compiler already tell you?

Write a function that sets all 3 to the max speed as an argument to the function.

void setMaxSpeedAll(int speed)
{
   w2.setMaxSpeed(speed);
   w3.setMaxSpeed(speed);
   w4.setMaxSpeed(speed);
}

Won't make it smaller, but it makes it calling one function instead of 3.

hello,
so if the compiler does not give any errors that means it works?

Thanks

324hz
win21H2
he/him

It depends how you define "work" (in all such arguments, the language standard wins)

thanks I will try to implement that into my code. So if it is only 1 function, will it optimize the code? like will it make it run a bit faster?

Thanks

324hz
win21H2
he/him

No, of course not

Do you have warnings enabled (File, Preferences, Compiler warnings, All) Does the compiler issue any warnings?

Just because it compiles does not mean that it will work.

nope it does not have any errors or warnings and I have everything selected

If you want your code to run faster, we may be able to help with that if you post the code.

ok. here it is:

#include <SoftwareSerial.h>
#include <AccelStepper.h>

SoftwareSerial Bluetooth(A8, 38); 
AccelStepper LeftBackWheel(1, 42, 43);  
AccelStepper LeftFrontWheel(1, 40, 41); 
AccelStepper RightBackWheel(1, 44, 45); 
AccelStepper RightFrontWheel(1, 46, 47); 

int wheelSpeed = 1500;
int dataIn, m;
int lbw[50], lfw[50], rbw[50], rfw[50]; 
int index = 0;

void setup() {
  
  LeftFrontWheel.setMaxSpeed(3000);
  LeftBackWheel.setMaxSpeed(3000);
  RightFrontWheel.setMaxSpeed(3000);
  RightBackWheel.setMaxSpeed(3000);
  
  Serial.begin(38400);
  Bluetooth.begin(9600); 
}

void loop() {
  if (Bluetooth.available() > 0) {
    dataIn = Bluetooth.read();  
    if (dataIn == 0) {
      m = 0;
    }
    if (dataIn == 2) {
      m = 2;
    }
    if (dataIn == 7) {
      m = 7;
    }
    if (dataIn == 9) {
      m = 9;
    }
    if (dataIn == 10) {
      m = 10;
    }
    if (dataIn == 5) {
      m = 5;
    }
    if (dataIn == 4) {
      m = 4;
    }
    
    if (dataIn >= 16) {
      wheelSpeed = dataIn * 10;
      Serial.println(wheelSpeed);
    }
  }

  if (m == 2) {
    moveForward();
  }
  if (m == 7) {
    moveBackward();
  }
  if (m == 9) {
    rotateLeft();
  }
  if (m == 10) {
    rotateRight();
  }
  if (m == 0) {
    stopMoving();
  }
  if (m == 5) {
    moveSidewaysRight();
  }
  if (m == 4) {
    moveSidewaysLeft();
  }
 
  if (m == 12) {
    if (index == 0) {
      LeftBackWheel.setCurrentPosition(0);
      LeftFrontWheel.setCurrentPosition(0);
      RightBackWheel.setCurrentPosition(0);
      RightFrontWheel.setCurrentPosition(0);
    }
    lbw[index] = LeftBackWheel.currentPosition();  
    lfw[index] = LeftFrontWheel.currentPosition();
    rbw[index] = RightBackWheel.currentPosition();
    rfw[index] = RightFrontWheel.currentPosition();
    index++;                     
    m = 0;
  }
  if (m == 14) {
    runSteps();
    if (dataIn != 14) {
      stopMoving();
      memset(lbw, 0, sizeof(lbw));
      memset(lfw, 0, sizeof(lfw));
      memset(rbw, 0, sizeof(rbw));
      memset(rfw, 0, sizeof(rfw));
      index = 0;
    }
  }
  LeftFrontWheel.runSpeed();
  LeftBackWheel.runSpeed();
  RightFrontWheel.runSpeed();
  RightBackWheel.runSpeed();


void moveForward() {
  LeftFrontWheel.setSpeed(-wheelSpeed);
  LeftBackWheel.setSpeed(-wheelSpeed);
  RightFrontWheel.setSpeed(wheelSpeed);
  RightBackWheel.setSpeed(wheelSpeed);
}
void moveBackward() {
  LeftFrontWheel.setSpeed(wheelSpeed);
  LeftBackWheel.setSpeed(wheelSpeed);
  RightFrontWheel.setSpeed(-wheelSpeed);
  RightBackWheel.setSpeed(-wheelSpeed);
}
void moveSidewaysRight() {
  LeftFrontWheel.setSpeed(-wheelSpeed);
  LeftBackWheel.setSpeed(wheelSpeed);
  RightFrontWheel.setSpeed(-wheelSpeed);
  RightBackWheel.setSpeed(wheelSpeed);
}
void moveSidewaysLeft() {
  LeftFrontWheel.setSpeed(wheelSpeed);
  LeftBackWheel.setSpeed(-wheelSpeed);
  RightFrontWheel.setSpeed(wheelSpeed);
  RightBackWheel.setSpeed(-wheelSpeed);
}
void rotateLeft() {
  LeftFrontWheel.setSpeed(wheelSpeed);
  LeftBackWheel.setSpeed(wheelSpeed);
  RightFrontWheel.setSpeed(wheelSpeed);
  RightBackWheel.setSpeed(wheelSpeed);
}
void rotateRight() {
  LeftFrontWheel.setSpeed(-wheelSpeed);
  LeftBackWheel.setSpeed(-wheelSpeed);
  RightFrontWheel.setSpeed(-wheelSpeed);
  RightBackWheel.setSpeed(-wheelSpeed);
}
void stopMoving() {
  LeftFrontWheel.setSpeed(0);
  LeftBackWheel.setSpeed(0);
  RightFrontWheel.setSpeed(0);
  RightBackWheel.setSpeed(0);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.