Skip to main content
Mess is deleted, code which is on right is working.
Source Link

I have connected motor shield to arduino uno. Connection between the two arduinos are A5 to A5, A4 to A4, 5V to 5V and GND to GND. I have a stepper connected to UNO, and a button connected to nano. However, I cant get to work the communication somehow. Any advice is appreciated.

Master

//master uno, reader
#include <Wire.h>
#include <AFMotor.h>

AF_Stepper motor(48, 2);
int x = 0;
void setup()
{
    Serial.begin(9600);
    Wire.begin(5);
    Wire.onReceive(receiveEvent);
    motor.setSpeed(300);  // 10 rpm   
    motor.step(500, FORWARD, SINGLE); 
    motor.release();
    delay(1000);
}
void receiveEvent( int bytes )
{
    x = Wire.read();
    Wire.endTransmission();
}
void loop()
{
    if (x == 1) {
        Serial.println("its actually 1, buttons working");
        motor.step(100, FORWARD, SINGLE);
        delay(1000);
    }
    if (x == 0) {
        Serial.println("button aint pressed");
        delay(1000);
    }
}

Slave

//slave nano, sender
#include <Wire.h> // <-- remove spaces
int x =0;
int slaveAddress = 5;
const int button1 = 2; // the number of the pushbutton pin
//const int button2 = 3;
int buttonState1 = 0;
//int buttonState2 = 0; // variable for reading the pushbutton status
void setup()
{
    pinMode(button1,INPUT); // initialize the pushbutton pin as an input
    //pinMode(button2,INPUT);
    Wire.begin(); 
    Serial.begin( 9600 );
}
void loop()
{
    // read the state of the pushbutton value:
    buttonState1 = digitalRead(button1);
    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if ( buttonState1 == HIGH ){
        x = 1;
    }
    else { 
        x = 0;
    }
    Wire.beginTransmission(5);
    Wire.write(x); // sends x
    Wire.endTransmission(); // stop transmitting
    delay(100);
}

EDIT: SoEDIT5: I deleted the messy previous codes, the question was answered. However, I managed to runcant get the tutorial, its workingsteppers to rotate(they only vibrate). My questionAny advice is how can I make it to work with buttons, because in this case I'm requesting bytesappreciated. I don't understand how does this whole thing work.The code right now is:

#include <Wire.h>
#include <AFMotor.h>
//define steppers
AF_Stepper motor1(48, 1); 
AF_Stepper motor2(48, 2);

//define variables
int x = 0;
int y = 0;

void setup()  
{
  Wire.begin(5); //begin i2c communication
  Wire.onReceive(receiveEvent);
  motor1.setSpeed(10);  // join10 i2crpm bus  
  motor2.setSpeed(address10); optional for// master10 rpm 
  
  motor1.release();
  Serialmotor2.beginrelease(9600); 
  delay(1000);
}

void loop()
{
  delay(500); // startwait serial0.5 forsecond output
}

void loopreceiveEvent()int howMany)
{
    //read x,y from slave
    x = Wire.requestFromread(8,);
 6   y = Wire.read();
    
    if (x == 1) //stepper1 requestrotate 6forward
 bytes from slave device{ #8
 
  while (Wire   motor1.availablestep()100, FORWARD, SINGLE); {
    }
    else if (x == 2) //stepper1 slaverotate maybackward
 send less than requested{
    char c = Wiremotor1.readstep(100, BACKWARD, SINGLE); 
    }
     else if (y == 1) //stepper2 receiverotate aforward
 byte as character {
    Serial  motor2.printstep(c100, BACKWARD, SINGLE);
    }
     else if (y == 2) // printstepper2 therotate characterbackward
  }
  {
  delay    motor2.step(500100, BACKWARD, SINGLE);
    }
} 
void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

void loop() {
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  Wire.write("hello "); // respond with message of 6 bytes
  // as expected by master
}

EDIT2: If the button is pushed, Master reads 1 and prints it to serial monitor. If the button isnt pushed, prints 0.

Master:

#include <Wire.h>

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

void loop() {
  Wire.requestFrom(8,1);    // request 6 bytes from slave device #8

  while (Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}

Slave:

#include <Wire.h>

const int button1 = 2;

int buttonState1 = 0;
void setup() {
  pinMode(button1,INPUT);
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

    void loop() {
      delay(100);
    }
    
    
    void requestEvent() {
      buttonState1 = digitalRead(button1);
      if(buttonState1 == HIGH){
      Wire.write("1");
      }
      else Wire.write("0");
    }

Now I'm lost. How to make to work with the stepper, button and stuff. I think I need time for this one.

EDIT3: I did some search, made some adjustments and got this: Master

#include <Wire.h>
#include <AFMotor.h>
#include <AccelStepper.h>

AF_Stepper motorx(48, 1);
AF_Stepper motory(48, 2);
int x = 0;
int y = 0;

void forwardstep1() {  
  motorx.onestep(FORWARD, SINGLE);
}
void backwardstep1() {  
  motorx.onestep(BACKWARD, SINGLE);
}

void forwardstep2() {  
  motory.onestep(FORWARD, SINGLE);
}
void backwardstep2() {  
  motory.onestep(BACKWARD, SINGLE);
}

AccelStepper stepperx(forwardstep1, backwardstep1);
AccelStepper steppery(forwardstep2, backwardstep2);

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

  stepperx.setMaxSpeed(200.0);
  stepperx.setAcceleration(100.0);
//  stepperx.moveTo(10000);

  steppery.setMaxSpeed(300.0);
  steppery.setAcceleration(100.0);
 // stepperx.moveTo(10000);
}

void loop() {
  Wire.requestFrom(8,2);    // request 2 bytes from slave device #8
  //Wire.endTransmission(); 
  while (Wire.available()) { 
    char c = Wire.read(); 
    Serial.print(c); // print the character
  } 
    x = Wire.read();
    y = Wire.read();
    if(x == 1){
      forwardstep1();
    } else if(y == 1){
      forwardstep2();
  } else delay(500);
  }

Slave:

#include <Wire.h>

//define buttons
const int button1 = 2; 
const int button2 = 3;
const int button3 = 4;
const int button4 = 5;

//define state of buttons
int buttonState1 = 0;
int buttonState2 = 0;
 
int ybuttonState3 = 0;
int buttonState4 = 0;

// verticaldefine movementvariables
int x = 0;
int //y horizontal= movement0;

void setup() {
  pinMode(button1,INPUT);
  pinMode(button2,INPUT);{
  Wire.begin(8);                // joinstart i2c bus with address #8communication
   Wire.onRequestbegin(requestEvent); // register event
}

void loop()  
{
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, seeread setup()
voidstate requestEvent()of {buttons
  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);
  if(buttonState1 == HIGH){
    ybuttonState3 = 1; 
    Wire.write(y);  
 // Wire.writedigitalRead("y1"button3); 
  }
  else if(buttonState2 == HIGH){
    xbuttonState4 = 1;
    Wire.writedigitalRead(xbutton4);
    } else{
    y = 0;
    Wire.write(y);
//if button1 is pressed,send x =to 0;master
    Wire.writeif(x);
 // buttonState1 == Wire.write("00"HIGH);
  }}{
  

The steppers are not working. I guess my code is incorrect. Could someone help me fix this? The thing I still want is to move the stepper if I push a button.

EDIT4: Alrighty. I just managed to do it, it works(not really), but the steppers are just vibrating not rotating. The power might be low. How can I make it actually rotate? Anyways, Master:

#include <Wire.h>
#include <AFMotor.h>
AF_Stepper motor(48, 2);

int x = 0;1;
int y = 0;

void setup()
{
  Wire.beginbeginTransmission(5);
  Wire.onReceive(receiveEvent);
  motor.setSpeed(10);  // 10 rpm   
 
 // motor.step(100, FORWARD, SINGLE); 
  motorWire.release();
  delay(1000);
}

void loop()
{
  delaywrite(1000x);
}

void receiveEvent(int howMany)
{
    x = Wire.readendTransmission();
    if (x == 1)
    {  }
    //if button2 motor.step(100,is FORWARDpressed, SINGLE); 
 send x to }master
    else if (xbuttonState2 == 2HIGH)
    {
      motor.step(100,x BACKWARD,= SINGLE);2;
      motorWire.stepbeginTransmission(100, BACKWARD, SINGLE5);
      motorWire.stepwrite(100, BACKWARD, SINGLEx);
      motorWire.stependTransmission(100, BACKWARD, SINGLE); 
    }
} 

Slave:

#include <Wire.h>

const int button1 = 2;
const int button2 = 3;

int buttonState1 = 0;
int buttonState2 = 0;

int x = 0;

void setup()
{
  Serial.begin(9600);
  
  Wire.begin();
}

void loop()
{
  buttonState1//if =button3 digitalRead(button1);
is pressed,send buttonState2y =to digitalRead(button2);master
    if(buttonState1buttonState3 == HIGH){
        xy = 1;
        Wire.beginTransmission(5);
        Wire.write(xy);
        Wire.endTransmission();
    }
   
  //if button4 is }pressed,send y to master
    else if(buttonState2buttonState4 == HIGH)
    {
      xy = 2;
      Wire.beginTransmission(5);
      Wire.write(xy);
      Wire.endTransmission();
    }
} 

I have connected motor shield to arduino uno. Connection between the two arduinos are A5 to A5, A4 to A4, 5V to 5V and GND to GND. I have a stepper connected to UNO, and a button connected to nano. However, I cant get to work the communication somehow. Any advice is appreciated.

Master

//master uno, reader
#include <Wire.h>
#include <AFMotor.h>

AF_Stepper motor(48, 2);
int x = 0;
void setup()
{
    Serial.begin(9600);
    Wire.begin(5);
    Wire.onReceive(receiveEvent);
    motor.setSpeed(300);  // 10 rpm   
    motor.step(500, FORWARD, SINGLE); 
    motor.release();
    delay(1000);
}
void receiveEvent( int bytes )
{
    x = Wire.read();
    Wire.endTransmission();
}
void loop()
{
    if (x == 1) {
        Serial.println("its actually 1, buttons working");
        motor.step(100, FORWARD, SINGLE);
        delay(1000);
    }
    if (x == 0) {
        Serial.println("button aint pressed");
        delay(1000);
    }
}

Slave

//slave nano, sender
#include <Wire.h> // <-- remove spaces
int x =0;
int slaveAddress = 5;
const int button1 = 2; // the number of the pushbutton pin
//const int button2 = 3;
int buttonState1 = 0;
//int buttonState2 = 0; // variable for reading the pushbutton status
void setup()
{
    pinMode(button1,INPUT); // initialize the pushbutton pin as an input
    //pinMode(button2,INPUT);
    Wire.begin(); 
    Serial.begin( 9600 );
}
void loop()
{
    // read the state of the pushbutton value:
    buttonState1 = digitalRead(button1);
    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if ( buttonState1 == HIGH ){
        x = 1;
    }
    else { 
        x = 0;
    }
    Wire.beginTransmission(5);
    Wire.write(x); // sends x
    Wire.endTransmission(); // stop transmitting
    delay(100);
}

EDIT: So I managed to run the tutorial, its working. My question is how can I make it to work with buttons, because in this case I'm requesting bytes. I don't understand how does this whole thing work.

#include <Wire.h>

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

void loop() {
  Wire.requestFrom(8, 6);    // request 6 bytes from slave device #8

  while (Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}
void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

void loop() {
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  Wire.write("hello "); // respond with message of 6 bytes
  // as expected by master
}

EDIT2: If the button is pushed, Master reads 1 and prints it to serial monitor. If the button isnt pushed, prints 0.

Master:

#include <Wire.h>

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

void loop() {
  Wire.requestFrom(8,1);    // request 6 bytes from slave device #8

  while (Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}

Slave:

#include <Wire.h>

const int button1 = 2;

int buttonState1 = 0;
void setup() {
  pinMode(button1,INPUT);
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

    void loop() {
      delay(100);
    }
    
    
    void requestEvent() {
      buttonState1 = digitalRead(button1);
      if(buttonState1 == HIGH){
      Wire.write("1");
      }
      else Wire.write("0");
    }

Now I'm lost. How to make to work with the stepper, button and stuff. I think I need time for this one.

EDIT3: I did some search, made some adjustments and got this: Master

#include <Wire.h>
#include <AFMotor.h>
#include <AccelStepper.h>

AF_Stepper motorx(48, 1);
AF_Stepper motory(48, 2);
int x = 0;
int y = 0;

void forwardstep1() {  
  motorx.onestep(FORWARD, SINGLE);
}
void backwardstep1() {  
  motorx.onestep(BACKWARD, SINGLE);
}

void forwardstep2() {  
  motory.onestep(FORWARD, SINGLE);
}
void backwardstep2() {  
  motory.onestep(BACKWARD, SINGLE);
}

AccelStepper stepperx(forwardstep1, backwardstep1);
AccelStepper steppery(forwardstep2, backwardstep2);

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

  stepperx.setMaxSpeed(200.0);
  stepperx.setAcceleration(100.0);
//  stepperx.moveTo(10000);

  steppery.setMaxSpeed(300.0);
  steppery.setAcceleration(100.0);
 // stepperx.moveTo(10000);
}

void loop() {
  Wire.requestFrom(8,2);    // request 2 bytes from slave device #8
  //Wire.endTransmission(); 
  while (Wire.available()) { 
    char c = Wire.read(); 
    Serial.print(c); // print the character
  } 
    x = Wire.read();
    y = Wire.read();
    if(x == 1){
      forwardstep1();
    } else if(y == 1){
      forwardstep2();
  } else delay(500);
  }

Slave:

#include <Wire.h>

const int button1 = 2;
const int button2 = 3;

int buttonState1 = 0;
int buttonState2 = 0;
 
int y = 0; // vertical movement
int x = 0; // horizontal movement

void setup() {
  pinMode(button1,INPUT);
  pinMode(button2,INPUT);
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

void loop() {
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);
  if(buttonState1 == HIGH){
    y = 1; 
    Wire.write(y);  
 // Wire.write("y1"); 
  }
  else if(buttonState2 == HIGH){
    x = 1;
    Wire.write(x);
    } else{
    y = 0;
    Wire.write(y);
    x = 0;
    Wire.write(x);
 //   Wire.write("00");
  }}
  

The steppers are not working. I guess my code is incorrect. Could someone help me fix this? The thing I still want is to move the stepper if I push a button.

EDIT4: Alrighty. I just managed to do it, it works(not really), but the steppers are just vibrating not rotating. The power might be low. How can I make it actually rotate? Anyways, Master:

#include <Wire.h>
#include <AFMotor.h>
AF_Stepper motor(48, 2);

int x = 0;
int y = 0;

void setup()
{
  Wire.begin(5);
  Wire.onReceive(receiveEvent);
  motor.setSpeed(10);  // 10 rpm   
 
 // motor.step(100, FORWARD, SINGLE); 
  motor.release();
  delay(1000);
}

void loop()
{
  delay(1000);
}

void receiveEvent(int howMany)
{
    x = Wire.read();
    if (x == 1)
    {  
      motor.step(100, FORWARD, SINGLE); 
    }
    else if (x == 2)
    {
      motor.step(100, BACKWARD, SINGLE);
      motor.step(100, BACKWARD, SINGLE);
      motor.step(100, BACKWARD, SINGLE);
      motor.step(100, BACKWARD, SINGLE); 
    }
} 

Slave:

#include <Wire.h>

const int button1 = 2;
const int button2 = 3;

int buttonState1 = 0;
int buttonState2 = 0;

int x = 0;

void setup()
{
  Serial.begin(9600);
  
  Wire.begin();
}

void loop()
{
  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);
    if(buttonState1 == HIGH){
        x = 1;
        Wire.beginTransmission(5);
        Wire.write(x);
        Wire.endTransmission();
       
     }
    else if(buttonState2 == HIGH)
    {
      x = 2;
      Wire.beginTransmission(5);
      Wire.write(x);
      Wire.endTransmission();
    }
} 

I have connected motor shield to arduino uno. Connection between the two arduinos are A5 to A5, A4 to A4, 5V to 5V and GND to GND. I have a stepper connected to UNO, and a button connected to nano.

EDIT5: I deleted the messy previous codes, the question was answered. However, I cant get the steppers to rotate(they only vibrate). Any advice is appreciated. The code right now is:

#include <Wire.h>
#include <AFMotor.h>
//define steppers
AF_Stepper motor1(48, 1); 
AF_Stepper motor2(48, 2);

//define variables
int x = 0;
int y = 0;

void setup() 
{
  Wire.begin(5); //begin i2c communication
  Wire.onReceive(receiveEvent);
  motor1.setSpeed(10);  // 10 rpm   
  motor2.setSpeed(10);  // 10 rpm 
  
  motor1.release();
  motor2.release(); 
  delay(1000);
}

void loop()
{
  delay(500); // wait 0.5 second 
}

void receiveEvent(int howMany)
{
    //read x,y from slave
    x = Wire.read();
    y = Wire.read();
    
    if (x == 1) //stepper1 rotate forward
    {  
      motor1.step(100, FORWARD, SINGLE); 
    }
    else if (x == 2) //stepper1 rotate backward
    {
      motor1.step(100, BACKWARD, SINGLE); 
    }
     else if (y == 1) //stepper2 rotate forward
    {
      motor2.step(100, BACKWARD, SINGLE);
    }
     else if (y == 2) //stepper2 rotate backward
    {
      motor2.step(100, BACKWARD, SINGLE);
    }
} 
#include <Wire.h>

//define buttons
const int button1 = 2; 
const int button2 = 3;
const int button3 = 4;
const int button4 = 5;

//define state of buttons
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;

//define variables
int x = 0;
int y = 0;

void setup()
{
   //start i2c communication
   Wire.begin();
}

void loop() 
{
  //read state of buttons
  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);
  buttonState3 = digitalRead(button3);
  buttonState4 = digitalRead(button4);

    //if button1 is pressed,send x to master
    if(buttonState1 == HIGH){
      x = 1;
      Wire.beginTransmission(5);
      Wire.write(x);
      Wire.endTransmission();
    }
    //if button2 is pressed,send x to master
    else if(buttonState2 == HIGH)
    {
      x = 2;
      Wire.beginTransmission(5);
      Wire.write(x);
      Wire.endTransmission();
    }
    //if button3 is pressed,send y to master
    if(buttonState3 == HIGH){
      y = 1;
      Wire.beginTransmission(5);
      Wire.write(y);
      Wire.endTransmission();
    }
    //if button4 is pressed,send y to master
    else if(buttonState4 == HIGH)
    {
      y = 2;
      Wire.beginTransmission(5);
      Wire.write(y);
      Wire.endTransmission();
    }
} 
code done.
Source Link

EDIT4: Alrighty. I just managed to do it, it works(not really), but the steppers are just vibrating not rotating. The power might be low. How can I make it actually rotate? Anyways, Master:

#include <Wire.h>
#include <AFMotor.h>
AF_Stepper motor(48, 2);

int x = 0;
int y = 0;

void setup()
{
  Wire.begin(5);
  Wire.onReceive(receiveEvent);
  motor.setSpeed(10);  // 10 rpm   
 
 // motor.step(100, FORWARD, SINGLE); 
  motor.release();
  delay(1000);
}

void loop()
{
  delay(1000);
}

void receiveEvent(int howMany)
{
    x = Wire.read();
    if (x == 1)
    {  
      motor.step(100, FORWARD, SINGLE); 
    }
    else if (x == 2)
    {
      motor.step(100, BACKWARD, SINGLE);
      motor.step(100, BACKWARD, SINGLE);
      motor.step(100, BACKWARD, SINGLE);
      motor.step(100, BACKWARD, SINGLE); 
    }
} 

Slave:

#include <Wire.h>

const int button1 = 2;
const int button2 = 3;

int buttonState1 = 0;
int buttonState2 = 0;

int x = 0;

void setup()
{
  Serial.begin(9600);
  
  Wire.begin();
}

void loop()
{
  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);
    if(buttonState1 == HIGH){
        x = 1;
        Wire.beginTransmission(5);
        Wire.write(x);
        Wire.endTransmission();
       
    }
    else if(buttonState2 == HIGH)
    {
      x = 2;
      Wire.beginTransmission(5);
      Wire.write(x);
      Wire.endTransmission();
    }
} 

EDIT4: Alrighty. I just managed to do it, it works(not really), but the steppers are just vibrating not rotating. The power might be low. How can I make it actually rotate? Anyways, Master:

#include <Wire.h>
#include <AFMotor.h>
AF_Stepper motor(48, 2);

int x = 0;
int y = 0;

void setup()
{
  Wire.begin(5);
  Wire.onReceive(receiveEvent);
  motor.setSpeed(10);  // 10 rpm   
 
 // motor.step(100, FORWARD, SINGLE); 
  motor.release();
  delay(1000);
}

void loop()
{
  delay(1000);
}

void receiveEvent(int howMany)
{
    x = Wire.read();
    if (x == 1)
    {  
      motor.step(100, FORWARD, SINGLE); 
    }
    else if (x == 2)
    {
      motor.step(100, BACKWARD, SINGLE);
      motor.step(100, BACKWARD, SINGLE);
      motor.step(100, BACKWARD, SINGLE);
      motor.step(100, BACKWARD, SINGLE); 
    }
} 

Slave:

#include <Wire.h>

const int button1 = 2;
const int button2 = 3;

int buttonState1 = 0;
int buttonState2 = 0;

int x = 0;

void setup()
{
  Serial.begin(9600);
  
  Wire.begin();
}

void loop()
{
  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);
    if(buttonState1 == HIGH){
        x = 1;
        Wire.beginTransmission(5);
        Wire.write(x);
        Wire.endTransmission();
       
    }
    else if(buttonState2 == HIGH)
    {
      x = 2;
      Wire.beginTransmission(5);
      Wire.write(x);
      Wire.endTransmission();
    }
} 
steppers not working.
Source Link

EDIT3: I did some search, made some adjustments and got this: Master

#include <Wire.h>
#include <AFMotor.h>
#include <AccelStepper.h>

AF_Stepper motorx(48, 1);
AF_Stepper motory(48, 2);
int x = 0;
int y = 0;

void forwardstep1() {  
  motorx.onestep(FORWARD, SINGLE);
}
void backwardstep1() {  
  motorx.onestep(BACKWARD, SINGLE);
}

void forwardstep2() {  
  motory.onestep(FORWARD, SINGLE);
}
void backwardstep2() {  
  motory.onestep(BACKWARD, SINGLE);
}

AccelStepper stepperx(forwardstep1, backwardstep1);
AccelStepper steppery(forwardstep2, backwardstep2);

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

  stepperx.setMaxSpeed(200.0);
  stepperx.setAcceleration(100.0);
//  stepperx.moveTo(10000);

  steppery.setMaxSpeed(300.0);
  steppery.setAcceleration(100.0);
 // stepperx.moveTo(10000);
}

void loop() {
  Wire.requestFrom(8,2);    // request 2 bytes from slave device #8
  //Wire.endTransmission(); 
  while (Wire.available()) { 
    char c = Wire.read(); 
    Serial.print(c); // print the character
  } 
    x = Wire.read();
    y = Wire.read();
    if(x == 1){
      forwardstep1();
    } else if(y == 1){
      forwardstep2();
  } else delay(500);
  }

Slave:

#include <Wire.h>

const int button1 = 2;
const int button2 = 3;

int buttonState1 = 0;
int buttonState2 = 0;

int y = 0; // vertical movement
int x = 0; // horizontal movement

void setup() {
  pinMode(button1,INPUT);
  pinMode(button2,INPUT);
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

void loop() {
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);
  if(buttonState1 == HIGH){
    y = 1; 
    Wire.write(y);  
 // Wire.write("y1"); 
  }
  else if(buttonState2 == HIGH){
    x = 1;
    Wire.write(x);
    } else{
    y = 0;
    Wire.write(y);
    x = 0;
    Wire.write(x);
 //   Wire.write("00");
  }}
  

The steppers are not working. I guess my code is incorrect. Could someone help me fix this? The thing I still want is to move the stepper if I push a button.

EDIT3: I did some search, made some adjustments and got this: Master

#include <Wire.h>
#include <AFMotor.h>
#include <AccelStepper.h>

AF_Stepper motorx(48, 1);
AF_Stepper motory(48, 2);
int x = 0;
int y = 0;

void forwardstep1() {  
  motorx.onestep(FORWARD, SINGLE);
}
void backwardstep1() {  
  motorx.onestep(BACKWARD, SINGLE);
}

void forwardstep2() {  
  motory.onestep(FORWARD, SINGLE);
}
void backwardstep2() {  
  motory.onestep(BACKWARD, SINGLE);
}

AccelStepper stepperx(forwardstep1, backwardstep1);
AccelStepper steppery(forwardstep2, backwardstep2);

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

  stepperx.setMaxSpeed(200.0);
  stepperx.setAcceleration(100.0);
//  stepperx.moveTo(10000);

  steppery.setMaxSpeed(300.0);
  steppery.setAcceleration(100.0);
 // stepperx.moveTo(10000);
}

void loop() {
  Wire.requestFrom(8,2);    // request 2 bytes from slave device #8
  //Wire.endTransmission(); 
  while (Wire.available()) { 
    char c = Wire.read(); 
    Serial.print(c); // print the character
  } 
    x = Wire.read();
    y = Wire.read();
    if(x == 1){
      forwardstep1();
    } else if(y == 1){
      forwardstep2();
  } else delay(500);
  }

Slave:

#include <Wire.h>

const int button1 = 2;
const int button2 = 3;

int buttonState1 = 0;
int buttonState2 = 0;

int y = 0; // vertical movement
int x = 0; // horizontal movement

void setup() {
  pinMode(button1,INPUT);
  pinMode(button2,INPUT);
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

void loop() {
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  buttonState1 = digitalRead(button1);
  buttonState2 = digitalRead(button2);
  if(buttonState1 == HIGH){
    y = 1; 
    Wire.write(y);  
 // Wire.write("y1"); 
  }
  else if(buttonState2 == HIGH){
    x = 1;
    Wire.write(x);
    } else{
    y = 0;
    Wire.write(y);
    x = 0;
    Wire.write(x);
 //   Wire.write("00");
  }}
  

The steppers are not working. I guess my code is incorrect. Could someone help me fix this? The thing I still want is to move the stepper if I push a button.

pressed=1,not=0. next:control the stepper with the button???
Source Link
Loading
Please avoid removing the previous versions of the code, since it usually invalidates a lot of discussion later and, in this case, also prevents the other to see what you really wanted
Source Link
frarugi87
  • 2.7k
  • 12
  • 19
Loading
edited tags
Source Link
Loading
edited tags
Link
Loading
Source Link
Loading