2 arduino communication with interrupt method on slave.

I have 1 arduino sending bytes to a second every 500 milliseconds using the i2c communications. My second arduino (slave) receives these bytes converts to an integer and then uses it to drive a blocking function.
my slave has a blocking function that involves flashing an led on pin 13 using delay.
My receive event takes sets this delay time period.
What i want is for the delay function to run, but if the master arduino sends a byte the led flash loop breaks and goes straight to reading a new value and then goes back to the led flash with the new value for delay period.
I want to replace this led with a motor step signal eventually.
The problem i have is that the motor step uses delay and wants to finish the amount of steps it was told to do before it goes to checking for incoming bytes.
The code for the master arduino

void setup()
{
  // Start I²C bus as a slave
  Wire.begin(SlaveDeviceId);
  // Set the callback to call when data is received.
  Wire.onReceive(receiveCallback);
 
  // For demonstration purposes.
  Serial.begin(9600);
}
 
void loop()
{
}
 
// aCount is the number of bytes received.
void receiveCallback(int aCount)
{
  if(aCount == 2)
  {
    int receivedValue  = Wire.read() << 8;
    receivedValue |= Wire.read();
    Serial.println(receivedValue);
  }
  else
  {
    Serial.print("Unexpected number of bytes received: ");
    Serial.println(aCount);
  }
}

Code for the slave arduino

int value;
// motor value
int sleepPin = 7;
int stepPin = 2;
int dirPin = 3;

void setup()
{
  // Start I²C bus as a slave
  Wire.begin(4);
  // Set the callback to call when data is received.
  Wire.onReceive(receiveCallback);
 
  // For demonstration purposes.
  Serial.begin(9600);
 //setup for motor
 pinMode(dirPin, OUTPUT);
 pinMode(stepPin, OUTPUT);
 pinMode(sleepPin, OUTPUT);
}
// blocking motor function
void step(boolean dir,int steps, int value)
{
 digitalWrite(dirPin,dir);
 for(int i=0;i<steps;i++)
  {
   digitalWrite(stepPin, LOW);
   delayMicroseconds(value);
   digitalWrite(stepPin, HIGH);
   delayMicroseconds(value);
  }
}
 
void loop()
{
  step(1,1024,value);
}
 
// aCount is the number of bytes received.
void receiveCallback(int aCount)
{
  if(aCount == 2)
  {
    int receivedValue  = Wire.read() << 8;
    receivedValue |= Wire.read();
    value = receivedValue;
    Serial.println(receivedValue);
  }
  else
  {
    Serial.print("Unexpected number of bytes received: ");
    Serial.println(aCount);
  }
}

Instead of just describing what your code does, you should also post it (using CODE tags).

Almost certainly you will need to replace delay() with the technique in the Blink Without Delay example sketch.

You will probably also need to rearrange your code so that each iteration of loop() causes one movement of the motor as well as checking for other stuff.

As @Arrch said, post your code if you want effective advice.

...R

Thank you for input so for, I have posted some code that should give an idea of what i want to do.

zer044: Thank you for input so for, I have posted some code that should give an idea of what i want to do.

Where?

I hope you have not overwritten code in an earlier post. That just makes the whole Thread confusing.

...R

The code for the master arduino

void setup()
{
  // Start I²C bus as a slave
  Wire.begin(SlaveDeviceId);
  // Set the callback to call when data is received.
  Wire.onReceive(receiveCallback);
 
  // For demonstration purposes.
  Serial.begin(9600);
}
 
void loop()
{
}
 
// aCount is the number of bytes received.
void receiveCallback(int aCount)
{
  if(aCount == 2)
  {
    int receivedValue  = Wire.read() << 8;
    receivedValue |= Wire.read();
    Serial.println(receivedValue);
  }
  else
  {
    Serial.print("Unexpected number of bytes received: ");
    Serial.println(aCount);
  }
}

Code for the slave arduino

int value;
// motor value
int sleepPin = 7;
int stepPin = 2;
int dirPin = 3;

void setup()
{
  // Start I²C bus as a slave
  Wire.begin(4);
  // Set the callback to call when data is received.
  Wire.onReceive(receiveCallback);
 
  // For demonstration purposes.
  Serial.begin(9600);
 //setup for motor
 pinMode(dirPin, OUTPUT);
 pinMode(stepPin, OUTPUT);
 pinMode(sleepPin, OUTPUT);
}
// blocking motor function
void step(boolean dir,int steps, int value)
{
 digitalWrite(dirPin,dir);
 for(int i=0;i<steps;i++)
  {
   digitalWrite(stepPin, LOW);
   delayMicroseconds(value);
   digitalWrite(stepPin, HIGH);
   delayMicroseconds(value);
  }
}
 
void loop()
{
  step(1,1024,value);
}
 
// aCount is the number of bytes received.
void receiveCallback(int aCount)
{
  if(aCount == 2)
  {
    int receivedValue  = Wire.read() << 8;
    receivedValue |= Wire.read();
    value = receivedValue;
    Serial.println(receivedValue);
  }
  else
  {
    Serial.print("Unexpected number of bytes received: ");
    Serial.println(aCount);
  }
}

You should treat the wire.onReceive() callback as an interrupt routine - it should be as short as possible (probably just store a variable and return) and should not have Serial.print() commands.

I would drop the wire.onReceive() in favour of if wire.available() (assuming wire has that function). That will put your code in control of when things happen. You can put as much Serial.print() as you want inside an if (wire.available() ) section.

…R