-4

Today I found a problem in my project, here is the video of problem

https://streamable.com/wamm9

code MASTER : https://www.dropbox.com/s/l4x3zwzv6szrs50/MASTER.ino?dl=0

#include <Wire.h>

const int BUTTON1 = 5;
const int BUTTON2 = 6;
int buttonchoose;

void setup() {
  // put your setup code here, to run once:
  pinMode(BUTTON1, INPUT);
  pinMode(BUTTON2, INPUT);
  Wire.begin();
  Serial.begin(9600);
}


void loop() {
  Serial.println("You want turn X motor");
  Serial.println("1-YES  2-NO");
  buttonchoose = fonctioninput();
  delay(500);
  if (buttonchoose == 1) {
    //Turn motor x for onerevolution
    Wire.beginTransmission(8); // transmit to device #8
    Wire.write('X'); // send M (milk)
    Wire.endTransmission();    // stop transmitting
    Serial.println("send X");
  }
  Serial.println("You want turn Y motor");
  Serial.println("1-YES  2-NO");
  buttonchoose = fonctioninput();
  delay(500);
  if (buttonchoose == 1) {
    //Turn motor Y for onerevolution
    Wire.beginTransmission(8); // transmit to device #8
    Wire.write('Y'); // send M (milk)
    Wire.endTransmission();    // stop transmitting
    Serial.println("send Y");
  }

  Serial.println("You want turn Z motor");
  Serial.println("1-YES  2-NO");
  buttonchoose = fonctioninput();
  delay(500);
  if (buttonchoose == 1) {
    //Turn motor Z for onerevolution
    Wire.beginTransmission(8); // transmit to device #8
    Wire.write('Z'); // send M (milk)
    Wire.endTransmission();    // stop transmitting
    Serial.println("send Z");
  }
}


int fonctioninput() {
  int val1 = 0;
  int val2 = 0;
  int buttonreturn = 0;
  while ( (val1 - val2) == 0) {
    val1 = digitalRead(BUTTON1);
    val2 = digitalRead(BUTTON2);
  }

  if (val1 == 1) {
    buttonreturn = 1;
  }
  else {
    buttonreturn = 2;
  }
  return buttonreturn;
}

code SLAVE : https://www.dropbox.com/s/2lg8ogjep80gtsa/SLAVE.ino?dl=0

#include <Wire.h>
#include <Stepper.h>

const int stepsPerRevolution = 64;
int led = A0;
Stepper motorX(stepsPerRevolution, 2, 3, 4, 5);
Stepper motorY(stepsPerRevolution, 6, 7, 8, 9);
Stepper motorZ(stepsPerRevolution, 10, 11, 12, 13);

void setup() {
  Wire.begin(8);
  // Wire.onRequest(requestEvent);
  Wire.onReceive(receiveEvent);

  pinMode(led, OUTPUT);
  motorX.setSpeed(60);
  motorY.setSpeed(60);
  motorZ.setSpeed(60);
  Serial.begin(9600);
}


void loop() {}


void receiveEvent(int howMany) {

  char choose;

  while (Wire.available() > 0) {
    //initialization
    choose = 'A';
    choose = Wire.read();

    if (choose == 'X')    {
      Serial.println("TURN MOTOR X");

      turnX();
    }
    else if (choose == 'Y') {
      Serial.println("TURN MOTOR Y");
      turnY();
    }
    else if (choose == 'Z') {
      Serial.println("TURN MOTOR Z");
      turnZ();
    }
  }
  analogWrite(led, 1020);
  delay(300);
  analogWrite(led, 0);
}


void turnX() {
  motorX.step(stepsPerRevolution);
}


void turnY() {
  motorY.step(stepsPerRevolution);
}


void turnZ() {
  motorZ.step(stepsPerRevolution);
}

When I choose yes to turn a motor it works well but the other motors that is after does not work. I want the other motors to turn if I choose yes even if the previous motor is running. Please, I need your URGENT help because my project is blocked :(

3

The step function from the Stepper library is blocking. So it can only run one motor at a time. You need a different stepper library that is non-blocking. Try AccelStepper.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.