I need help just simple code

Help me do this
Move both wheels forward(5 seconds), stop (3 seconds) then both wheels backward (3 seconds), stop (3 seconds) Turn robot to the left (3 seconds) and Turn robot to the right(3seconds).

Sure, we'll help. Show us what you've done so far.

I've made a robot for school and the material lacks so I need help with the code

const int lMotorA = 11;
const int lMotorB = 6;
const int rMotorA = 10;
const int rMotorB = 9;
const int led = 15;
const int speaker = 3;

void setup() {
pinMode (led, OUTPUT);
pinMode (speaker, OUTPUT);
pinMode(lMotorA, OUTPUT);
pinMode(lMotorB, OUTPUT);
pinMode(rMotorA, OUTPUT);
pinMode(rMotorB, OUTPUT);
}

void loop() {

digitalWrite(led, HIGH); //Turns on the LED
delay(1000); //Wait 1 second
digitalWrite(led, LOW); //Turns off the LED
delay(1000); //Wait 1 second

digitalWrite(rMotorA, HIGH); //Turns right motor clockwise or forward
delay(5000); //Wait 5 second
digitalWrite(rMotorA, LOW); //Stops right motor
delay(3000); //Wait 3 second

digitalWrite(lMotorB, HIGH); //Turns left motor counter clockwise or backward
delay(3000); //Wait 3 second
digitalWrite(lMotorB, LOW); // Stops left motor
delay(3000); //Wait 1 second

}

I just need help combining the motors

void setup() {
pinMode (led, OUTPUT);
pinMode (speaker, OUTPUT);
pinMode(lMotorA, OUTPUT);
pinMode(lMotorB, OUTPUT);
pinMode(rMotorA, OUTPUT);
pinMode(rMotorB, OUTPUT);
}

void loop() {

digitalWrite(led, HIGH); //Turns on the LED
delay(1000); //Wait 1 second
digitalWrite(led, LOW); //Turns off the LED
delay(1000); //Wait 1 second

digitalWrite(rMotorA, HIGH);
delay(5000);
digitalWrite(lMotorA, HIGH);
delay(5000);
digitalWrite(rMotorA, LOW);
delay(3000);
digitalWrite(lMotorA, LOW);
delay(3000);

digitalWrite(rMotorB, HIGH);
delay(3000);
digitalWrite(lMotorB, HIGH); //Turns left motor counter clockwise or backward
delay(3000); //Wait 3 second
digitalWrite(rMotorB, LOW); // Stops left motor
delay(3000); //Wait 3 second
digitalWrite(lMotorB, LOW);
delay(3000);

digitalWrite(rMotorC, HIGH);
delay(3000);
digitalWrite(lMotorC, HIGH);
delay(3000);

digitalWrite(rMotorD, HIGH);
delay(3000);
digitalWrite(lMotorD, HIGH);
delay(3000);
}

Is C and D correct for the left and right

You might get more satisfaction if you read up on millis() timing, and state machines... then you could play with concepts like the following -
It looks complicated but will teach you a lot.

PSEUDO CODE - just the idea, not tested or compiled...

enum StepNames{STOP, MOVE_FWD, STOP, MOVE_LEFT, MOVE_RIGHT };
// Commands stored as a direection, and a duraiton
const uint8_t stepCommand[] = {MOVE_FWD, 5, STOP, 3, MOVE_BACK, 3, STOP, 3, 
MOVE_LEFT, 3, MOVE_RIGHT, 3 };
const num_steps = sizeof(stepCommand / sizeof(stepCommand[0] );
uint32_t stepTimeMS
prevStepMS = 0L
uint8_t step_num = 0;

// use millis to step through the individual stepCommand[]s 
if (step_num < numSteps) {
  if ((millis() - prevStepMS) > stepTimeMS)  {
    prevStepMS  = millis(); 
    step_num++;
    // read the *direction* stepCommand[odds] and *duration* stepCommand [evens] from the stepCommand[] array
    // do it

// you might create a function to take a command e.g.
// void Move(unit8_t direction) {
    // set the motors in motion
// }
  }
}

Not a complete program so I have no idea what C and D might mean. Or what you are using to drive whatever motors you're talking about or how things are connected or powered.

But I'm pretty sure you will not make anything go straight by switching the right side motor on, waiting 5 seconds then switching the left motor on.

Steve

Check out my two tutorials
How to write Timers and Delays in Arduino and
Multi-tasking in Arduino

Change your code to the multi-tasking format and then use the millisDelay timers to start/stop the motors in each task.

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