@ Constantin,
I have been looking to get things working with your suggested Easytransfer library, do you have experience with it? This may be basic coding error on my part with using flags but I cant seem to find the problem: I'm hoping to run two steppers off the slave, depending on the flag state in the master I want to send info on the parameters of the stepper and which one to activate. Through the serial printing the flagslave state I get a bunch of zeros coming through very fast with stepper3 activated until some point where it changes to 1 and carries on operating stepper 3? Sometimes stepper4 will come on once with no change of flag state then return to stepper3? I have read that the function runToPosition() 'blocks' and is suggested to not be run in event loops? Could there be a problem here?
My master code:
/*THIS IS THE MASTER*/
#include <Wire.h>
#include <EasyTransferI2C.h>
int flagmaster = 1;
//create object
EasyTransferI2C ET;
struct SEND_DATA_STRUCTURE{
//put your variable definitions here for the data you want to send
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
int flagslave;
int a;
int b;
int c;
int d;
int e;
int f;
};
//give a name to the group of data
SEND_DATA_STRUCTURE mydata;
//define slave i2c address
#define I2C_SLAVE_ADDRESS 9
void setup(){
Wire.begin();
//start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
ET.begin(details(mydata), &Wire);
}
void loop()
{
if (flagmaster == 1)
{
mydata.a = 100;
mydata.b = 500;
mydata.c = 75;
mydata.flagslave = 1;
flagmaster = 0;
ET.sendData(I2C_SLAVE_ADDRESS);
}
if (flagmaster == 0)
{
mydata.d = 50;
mydata.e = 400;
mydata.f = 20;
mydata.flagslave = 0;
flagmaster = 1;
ET.sendData(I2C_SLAVE_ADDRESS);
}
delay (5000);
}
and slave code:
/*THIS IS THE SLAVE*/
#include <AccelStepper.h>
#include <Wire.h>
#include <EasyTransferI2C.h>
AccelStepper stepper3(4, 2, 3, 4, 6);
AccelStepper stepper4(4, 7, 8, 9, 10);
//int flagslave = 1;
//create object
EasyTransferI2C ET;
struct RECEIVE_DATA_STRUCTURE{
//put your variable definitions here for the data you want to receive
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
int flagslave;
int a;
int b;
int c;
int d;
int e;
int f;
};
//give a name to the group of data
RECEIVE_DATA_STRUCTURE mydata;
//define slave i2c address
#define I2C_SLAVE_ADDRESS 9
void setup(){
Wire.begin(I2C_SLAVE_ADDRESS);
//start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
ET.begin(details(mydata), &Wire);
//define handler function on receiving data
Wire.onReceive(receive);
Serial.begin(9600);
}
void loop() {
//check and see if a data packet has come in.
if(ET.receiveData() && (mydata.flagslave == 1)){
//this is how you access the variables. [name of the group].[variable name]
//since we have data, we will blink it out.
stepper3.setCurrentPosition(0);
stepper3.setMaxSpeed(mydata.a); //Set stepper speed in steps per second
stepper3.moveTo(mydata.b);
stepper3.setAcceleration(mydata.c);
}
if (ET.receiveData() && (mydata.flagslave == 0))
{
stepper4.setCurrentPosition(0);
stepper4.setMaxSpeed(mydata.d); //Set stepper speed in steps per second
stepper4.moveTo(mydata.e);
stepper4.setAcceleration(mydata.f);
}
stepper3.runToPosition();
stepper4.runToPosition();
Serial.println(mydata.flagslave);
}
void receive(int numBytes) {}
Any help would be great