I have 2 motors and I need it to go forward stop then backwards stop then repeat I made one motor do this but I can get the other to do it

//Romeo v1.1 M1; Direction = 3; speed = 5; M2; speed = 7; Direction = 9;
byte M1d = 3;
byte M1s = 5;
byte M2d = 7;
byte M2s = 9;

void setup() {
pinMode(M1d, OUTPUT);
pinMode(M1s, OUTPUT);
pinMode(M2d, OUTPUT);
pinMode(M2s, OUTPUT);
}
void loop() {
digitalWrite(M1d, LOW);
//digitalWrite(M2d, LOW);
analogWrite(M1s, 255);
//analogWrite(M2s, 90);
delay(1000);

digitalWrite(M1d, LOW);
analogWrite(M1s, 0);
delay(1000);

digitalWrite(M1d, HIGH);
analogWrite(M1s, 0);
delay(1000);

digitalWrite(M1d, LOW);
analogWrite(M1s, 0);
delay(1000);

Well, from what you've written, maybe you mean:

//Romeo v1.1 M1; Direction = 3; speed = 5; M2; speed = 7; Direction = 9;
byte M1d = 3;
byte M1s = 5;
byte M2d = 7;
byte M2s = 9;

void setup() {
  pinMode(M1d, OUTPUT);
  pinMode(M1s, OUTPUT);
  pinMode(M2d, OUTPUT);
  pinMode(M2s, OUTPUT);
}
void loop() {
  digitalWrite(M1d, LOW);
  //digitalWrite(M2d, LOW);
  analogWrite(M1s, 255);
  //analogWrite(M2s, 90);
  delay(1000);

  digitalWrite(M1d, LOW);
  analogWrite(M1s, 0);
  delay(1000);

  digitalWrite(M1d, HIGH);
  analogWrite(M1s, 0);
  delay(1000);

  digitalWrite(M1d, LOW);
  analogWrite(M1s, 0);
  delay(1000);

// M2
  digitalWrite(M2d, LOW);
  //digitalWrite(M2d, LOW);
  analogWrite(M2s, 255);
  //analogWrite(M2s, 90);
  delay(1000);

  digitalWrite(M2d, LOW);
  analogWrite(M2s, 0);
  delay(1000);

  digitalWrite(M2d, HIGH);
  analogWrite(M2s, 0);
  delay(1000);

  digitalWrite(M2d, LOW);
  analogWrite(M2s, 0);
  delay(1000);
}

Good luck!

(Notice how the indenting and forum code formatting tag makes the code readable and copy-paste-able.)

ok thank you so much

my robot will be in a rectangular box

You should move your post to a more suitable category as it does not relate to IDE 2.x.

I guess it will be staying inside the rectangular box until you can provide more details. Please read about how to get the best from the forum here:

1 Like

So it will always sense a wall and turn, what is the problem?

That sounds like an interesting project that could be a lot of fun! However, I am not clairvoyant. Please keep in mind that we are not a free design or code-writing service. We’re more than happy to help with your design or code, but we need you to make an initial attempt. Please design and write your code, then post it following forum guidelines along with an explanation of what’s not working properly.

  1. Show Your Work First: Before asking for assistance, make an attempt to design or write the code yourself. Share your work along with details about what isn’t working.
  2. Provide Clear Documentation: Since we can’t see your project, share an annotated schematic (best) or a clear drawing of your setup. Pictures are welcome, but avoid using Fritzing diagrams as they are wiring diagrams, not schematics, and are not ideal for troubleshooting.
  3. Include Technical Details: If there is specific hardware involved, include links to technical information. There are often many versions of similar components, so precise details are essential.
  4. Reference Resources: For additional help, check out useful links and tutorials: Useful Links on Arduino Forum.

I moved your topic to a more appropriate forum category @r3bhjkwdnwkhn .

In the future, please take the time to pick the forum category that best suits the subject of your question. There is an "About the _____ category" topic at the top of each category that explains its purpose.

Thanks in advance for your cooperation.

//Romeo v1.1 M1; Direction = 3; speed = 5; M2; speed = 7; Direction = 9;
byte M1d = 3;
byte M1s = 5;
byte M2d = 7;
byte M2s = 9;

void setup() {
  pinMode(M1d, OUTPUT);
  pinMode(M1s, OUTPUT);
  pinMode(M2d, OUTPUT);
  pinMode(M2s, OUTPUT);
}
void loop() {
  digitalWrite(M1d, LOW);
  analogWrite(M1s, 255);
  digitalWrite(M2d, HIGH);
  analogWrite(M2s, 255);
  delay(1000);


  digitalWrite(M1d, LOW);
  analogWrite(M1s, 0);
  analogWrite(M2s, 0);
  delay(1000);


  digitalWrite(M1d, HIGH);
  analogWrite(M1s, 0);
  digitalWrite(M2d, HIGH);
  analogWrite(M2s, 255);
  delay(1000);


  digitalWrite(M1d, LOW);
  analogWrite(M1s, 0);
  analogWrite(M2s, 0);
  delay(1000);
}

Most people would use the serial monitor to display messages that they add to their code to show the logic steps as they are taken or what is being attempted by that particular bit of code. Why don't you try doing that and see if that helps you discover the problem.

Duplicate topic of I have 2 motors and I need it to go forward stop then backwards stop then repeat I made one motor do this but I can get the other to do it. Flagged.

Well, to start, your pins definition doesn´t fit the description:

//Romeo v1.1 M1; Direction = 3; speed = 5; M2; speed = 7; Direction = 9;
byte M1d = 3;
byte M1s = 5;
byte M2d = 7; //(should be M2s)
byte M2s = 9; //(should be M2d)

but as we don't know how everything is wired, it's hard to say even that it's wrong...

Think about it, forward on one front wheel is backward on the other front wheel. Simple to solve (actually 2 ways but one is preferred)

  • Schematic diagrams are the universal language of electronics.
  • We use schematics to communicate hardware design. All components and connections are illustrated.
  • As always, show us a good schematic of your proposed circuit. Hand drawn schematics are acceptable.
  • Show us several good image views of your actual wiring.

@r3bhjkwdnwkhn, do not cross-post. Threads merged.

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