Code stops running when using stepper motor via IC2 (Two Arduino's)

Hi,

I have an Arduino Mega, and an Arduino Nano.

They have the SDA and SCL pins connected, and the Mega (master) is currently set to send a signal using the Wire library to the Nano to turn Pin 13 on / off every second. This works fine.

Now, I have added the Stepper library to the nano and wired up a stepper. The stepper works fine when using it in the loop etc. However, when I try to make the stepper turn one revolution instead of turning on the LED pin, the stepper steps once, and then both arduino’s freeze.

I can’t understand why this is happening. Can someone shed some light as to why this doesn’t work?

#include <Wire.h>

const byte slaveID = 3;

#include <Stepper.h>
const int stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);



void setup() {
  // put your setup code here, to run once:
  Wire.begin(slaveID);
  Wire.onReceive(recieveEvent);
  Serial.begin(9600);
  myStepper.setSpeed(60);
 
  pinMode(13,OUTPUT);
  digitalWrite(13, LOW);
  Serial.println("Starting first turn");
    for(int x = 0; x <= 1; x++){
      // step one revolution  in one direction:
    myStepper.step(stepsPerRevolution);
  }
}

void loop() {

}


void recieveEvent(int howMany){
  char inChar;

  while(Wire.available() > 0){
    inChar = Wire.read();
  }

  if(inChar == 'H'){
    digitalWrite(13,HIGH);
    myStepper.step(stepsPerRevolution); //This will send 1 step to the motor and then everything freezes. 
  
  }else if (inChar == 'L'){
    digitalWrite(13,LOW);
  }
  
}

(deleted)

This spelling error won't help much either but you did say it worked with a led ?:

void recieveEvent(int howMany){ 
. . .
}

Yes worked with an LED.

No the stepper is powered from a driver and with 12V 20A external supply. The stepper works fine as mentioned before, just it can't be called when using I2C.

I have an Arduino Mega, and an Arduino Nano.

They have the SDA and SCL pins connected, and the Mega (master) is currently set to send a signal using the Wire library to the Nano to turn Pin 13 on / off every second. This works fine.

Are the grounds common? They must be. Is the wiring between the two Arduinos short, neat and well away from the motor and its supply?

MarkT: Are the grounds common? They must be. Is the wiring between the two Arduinos short, neat and well away from the motor and its supply?

Yes everything is grounded. As mentioned the stepper works fine, even with I2C blinking an LED. But if I try to move the stepper by putting it in the I2C receive function, all the arduino's lock up untill the one running the stepper is disconnected.

It's a bug in the stepper library. I haven't pin pointed it exactly, but if anyone else is reading this from a Google search, use AccelStepper instead. It works better and had more features.