Hi Paul
Please check if this slave code is what you are looking for :
Basically ,
for delay of 100 - the current position of motor (when it stopped) is 7 and the request is called when the motor is at position 7 .
for delay of 4000 - the current position of motor is 2547 and the request is called when the motor is at position 2547 .
So is the request being called after the motor stops ? or is the delay interfering in some way ?
// I2C communication 1 Master- 1 Slave
// program for slave 4
#include <Wire.h>
#include <AccelStepper.h>
int incomingByte = 0;
// timer variables
long millisec = 0;
long microsec = 0;
// stepper motor pins: STEP, DIRECTION, STEPPER ON/OFF
AccelStepper Stepper1(1,2,3);
void setup() {
Wire.begin(4);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
//Start Serial for debuging purposes
Serial.begin(115200); // USB comm with PC
for(int i = 0;i <54;i++) {
pinMode(i,OUTPUT);
digitalWrite(i,LOW);
}
// switch stepper chips off
digitalWrite(4,HIGH);
Stepper1.setMaxSpeed(1000);
}
void receiveEvent(int howMany)
{
if (Wire.available() > 0) {
incomingByte = Wire.receive();
}
}
void requestEvent()
{
Serial.println(" Request is called when the motor is at position");
Serial.println(Stepper1.currentPosition());
while(Stepper1.currentPosition() != Stepper1.targetPosition())
{ //do nothing
}
digitalWrite(4,HIGH);
Wire.send("Action complete");
Serial.println("Request send when motor is at position");
Serial.println(Stepper1.currentPosition());
}
void loop()
{
Stepper1.run();
Serial.println(Stepper1.currentPosition());
//Serial.print(" Selection:");Serial.println(incomingByte);
switch(incomingByte) {
case 1: digitalWrite(4,LOW); digitalWrite(3,HIGH); digitalWrite(2,HIGH); //Serial.println("Stepper1: forward");
Serial.println("forward");
Stepper1.setCurrentPosition(0);
Stepper1.setAcceleration(500.0);
Stepper1.moveTo(4000);
break;
}
// switch loop
incomingByte = 0;
} // main loop