How do I get my Arduino master to send motor steps data to my slave for it to move

Good day. I'm new to Arduino but I'm working on a project where I need to get my Arduino master to send step data to my slave for it to move in said direction, but even though the data sort of comes through serially, I can't get my motor to move. I have 2 pieces of code but I don't know where I'm going wrong or might have gone wrong. I was hoping to share an image too but it's not coming through. please forgive any mistakes.

// Wire Master Writer

#include <Wire.h>

void setup() {
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600);
}

byte x = 0;
char c;

void loop() {
  while(Serial.available()>0){
  c=Serial.read();
  Wire.beginTransmission(8); // transmit to device #8
  Wire.write(c);               
  Wire.endTransmission();    // stop transmitting
  }
  x;
  delay(500);
}


----------------------------------------------------------------------------------------

// Wire Slave Receiver

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

int stepsPerRevolution=360;
int motSpeed=10;
int StepsPerMillimeterX = 5.0;
int Loc_int;
Stepper stepper_X_axis (stepsPerRevolution,8,9,10,11);


void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
  stepper_X_axis.setSpeed(motSpeed);
}

void loop() {
  stepper_X_axis.step(stepsPerRevolution);
  delay(100);
  
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
   while (Wire.available()){
    char x = Wire.read();    // receive byte as an integer
    Serial.println(x);         // print the integer
    int Loc_Int =x ;
    Serial.println(Loc_Int);
    //stepper_X_axis.step(stepsPerRevolution);
  }
  //delay(500);
}

A simulated or a real motor? Have you tried a simple sketch on SIM2 that only tests the motor?

thanks for the reply. its a simulated motor. I'm using proteus. i have simulated other sketched and tried to incorporate as much as i can as well.

1. Upload the simple sketches into Master and Slave to see that the Master can detect the presence of Slave and the Slave receives 7 (for example) from Master and shows on its Serial monitor. Do not forget to connect 2x4.7k pull-up resisters with signal wires of the I2C bus. Also, connect the GND-wire between the UNOs. The connection should be like Fig-1.

i2cunounox
Figure-1:

2. Master Sketch:

#include <Wire.h>
char y;

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  //-----------------
  Wire.beginTransmission(8);
  byte busStatus = Wire.endTransmission();
  if (busStatus != 0)
  {
    Serial.print("Slave is not found.");
    while (1); //wait for ever
  }
  Serial.println("Slave is found.");
}

void loop()
{
  while (Serial.available() == 0)
  {
    ;   //wait until data comes from SM1
  }
  y = Serial.read();
  Wire.beginTransmission(8);
  Wire.write(y);
  Wire.endTransmission();
}

3. Slave Sketch:

#include <Wire.h>
char y;
volatile bool flag = false;

void setup() 
{
  Wire.begin(8);               
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           
}

void loop() 
{
  if(flag == true)
  {
    Serial.println(y);
    flag = false; 
  }
}

void receiveEvent(int howMany) 
{
    y = Wire.read();    // receive byte as an integer
    flag = true;
}

4. Open Serial Monitor at the Master side with No line ending option.
5. Check that the message "Slave is found." has appeared on the OutputBox of the Serial Monitor (Fig-2).


Figure-2:

6. Open Serial Monitor for the Slave.
7. Enter 7 in the InputBox of the Serial Monitor of Master and then click on the Send button.
8. Check that 7 has appeared on SM2.
9. Now add your motor related codes with the above functional sketches.

This section is to TELL, not to ASK ,

Thread moved as requested.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.