-1

I am a beginner in the Arduino world.
I want to control motors via master and slave 2 wires.
But I have the code that works on a single Arduino. Now I don't know which code I need for master and slave.

int x_pin = A0; //potmeter X axis

int in1 = 2;
int in2 = 3;
int en1 = 5; //pwm pin

int y_pin = A1; //potmeter y axis

int in3 = 7;
int in4 = 8;
int en2 = 6; //pwm pin

void setup()
{
    Serial.begin(9600);
    pinMode(A0, INPUT);
    pinMode(2,OUTPUT);
    pinMode(3,OUTPUT);
    pinMode(5,OUTPUT);
    pinMode(A1, INPUT);
    pinMode(7,OUTPUT);
    pinMode(8,OUTPUT);
    pinMode(6,OUTPUT);  
}

void loop()
{
    int x_data = analogRead(A0);

    Serial.print("x_data:");
    Serial.print(x_data);
    Serial.print("\n");
    delay(100);

    //Forward
    if(x_data <= 500)
    {
        int speed_F = map(x_data, 500, 0, 0, 250);
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
        analogWrite(en1, speed_F);
    }
    //Backward
    else if(x_data >= 550)
    {
        int speed_B = map(x_data, 550, 1020, 0, 250);
        digitalWrite(in1, LOW);
        digitalWrite(in2, HIGH);
        analogWrite(en1, speed_B);
    }
    else
    {
        digitalWrite(in1, LOW);
        digitalWrite(in2, LOW);
        analogWrite(en1, 0);
    }
    
    int y_data = analogRead(A1);

    Serial.print("y_data:");
    Serial.print(y_data);
    Serial.print("\n");
    delay(100);

    //Forward
    if(y_data <= 500)
    {
        int speed_F = map(y_data, 500, 0, 0, 250);
        digitalWrite(in3, HIGH);
        digitalWrite(in4, LOW);
        analogWrite(en2, speed_F);
    }
    //Backward
    else if(y_data >= 550)
    {
        int speed_B = map(y_data, 550, 1020, 0, 250);
        digitalWrite(in3, LOW);
        digitalWrite(in4, HIGH);
        analogWrite(en2, speed_B);
    }
    else
    {
        digitalWrite(in3, LOW);
        digitalWrite(in4, LOW);
        analogWrite(en2, 0);
    }
}
5
  • 1
    what is your question? ... please add a focused, answerable question to your post
    – jsotola
    Commented Jul 10 at 6:17
  • With master and slave you mean two Arduinos, communicating via TwoWire/I2C? The slave, which controls the motor, needs the motor control code and I2C receive code. The master needs the I2C send/master code and the logic, how the motor should behave. Though this is a trivial statement. It is unclear, what your actual problem is in building this.
    – chrisl
    Commented Jul 10 at 8:37
  • They are two arduinos with two wires/I2C connection. Master sends the commands from potentiometers to the slave. The slave controls the motor driver.
    – lu me
    Commented Jul 11 at 2:42
  • I found it. Thanks everyone anyway
    – lu me
    Commented Jul 12 at 3:56
  • Welcome to SE/Arduino! Please take the tour to learn how this site works. This is not a forum. -- Now you have two options, 1) if you don't think your question is valuable for others (including your future self), delete it; 2) if you think your question and its solution are valuable, please write an actual answer. Just don't leave the question open. Commented Jul 12 at 6:41

0

Browse other questions tagged or ask your own question.