Arduino – Week 4 – Robot

The final week task was to program a robot which would move by itself and avoid objects

Unfortunately this wasn’t completed due to time constraints, although most of the code for the movements was completed and fully working

The board was attached to the chassis with metal standoff – which did short the board out – however it was sorted out by replacing them with plastic ones

Two stepper motors were used to control the robot, one on each side.

There is a breakout board on top of the arduino, which allows for easy plugging in and out of the stepper motors

Coding

Below is the coding used for the control

Where the breaks are for the left, right, forward, and reverse, the position of the output pins have been changed, so that the stepper motor activated the different magnets in a different order, which changes the direciton.

The left and right have one wheel going forward and one going backwards, therefore turning on the spot, similar to how tanks rotate using their tracks

Here the robot is programmed to run the command for about 16 seconds, after which it will stop moving

/*
  Stepper Motor lakelly 02/05/2019
*/

// the setup function runs once when you press reset or power the board
void setup() 
{
  // initialize digital coil A as an output.
  pinMode(10, OUTPUT);
    // initialize digital coil B as an output.
  pinMode(11, OUTPUT);
    // initialize digital coil C as an output.
  pinMode(12, OUTPUT);
    // initialize digital coil D as an output.
  pinMode(13, OUTPUT);
  //motor 2
  pinMode(6, OUTPUT);
    // initialize digital coil B as an output.
  pinMode(7, OUTPUT);
    // initialize digital coil C as an output.
  pinMode(8, OUTPUT);
    // initialize digital coil D as an output.
  pinMode(9, OUTPUT);

}

 int i=0;
  
void loop() {

   while(i < 1000){
  right();
  i++;

   }
}

void forward()    //+++++++++++++++++++++++++++++++++++++++++++++++FOWARDS++++++++++++++++++++++++++++++++++++++++++++++
{
      //STEP 1
  digitalWrite(10, HIGH);   
  digitalWrite(11, LOW);   
  digitalWrite(12, LOW);
  digitalWrite(13, HIGH);
  delay(2);                      // delay
      //STEP 2
  digitalWrite(10, HIGH);   
  digitalWrite(11, HIGH);   
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
    delay(2);                      // delay
      //STEP 3
  digitalWrite(10, LOW);   
  digitalWrite(11, HIGH);   
  digitalWrite(12, HIGH);
  digitalWrite(13, LOW);
    delay(2);                      // delay
      //STEP 4
  digitalWrite(10, LOW);   
  digitalWrite(11, LOW);   
  digitalWrite(12, HIGH);
  digitalWrite(13, HIGH);
    delay(2);                      // delay

//motor2
  //STEP 1
  digitalWrite(6, HIGH);   
  digitalWrite(7, LOW);   
  digitalWrite(8, LOW);
  digitalWrite(9, HIGH);
  delay(2);                      // delay
      //STEP 2
  digitalWrite(6, HIGH);   
  digitalWrite(7, HIGH);   
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
    delay(2);                      // delay
      //STEP 3
  digitalWrite(6, LOW);   
  digitalWrite(7, HIGH);   
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
    delay(2);                      // delay
      //STEP 4
  digitalWrite(6, LOW);   
  digitalWrite(7, LOW);   
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
    delay(2);  // delay
}

void backward()    //+++++++++++++++++++++++++++++++++++++++++++++++BACKWARDS++++++++++++++++++++++++++++++++++++++++++++++
{
  //STEP 1
  digitalWrite(10, HIGH);   
  digitalWrite(11, LOW);   
  digitalWrite(12, LOW);
  digitalWrite(13, HIGH);
  delay(2);                      // delay
      //STEP 2
  digitalWrite(10, HIGH);   
  digitalWrite(11, HIGH);   
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
    delay(2);                      // delay
      //STEP 3
  digitalWrite(10, LOW);   
  digitalWrite(11, HIGH);   
  digitalWrite(12, HIGH);
  digitalWrite(13, LOW);
    delay(2);                      // delay
      //STEP 4
  digitalWrite(10, LOW);   
  digitalWrite(11, LOW);   
  digitalWrite(12, HIGH);
  digitalWrite(13, HIGH);
    delay(2);                      // delay


//motor2
  //STEP 1
  digitalWrite(9, HIGH);   
  digitalWrite(8, LOW);   
  digitalWrite(7, LOW);
  digitalWrite(6, HIGH);
  delay(2);                      // delay
      //STEP 2
  digitalWrite(9, HIGH);   
  digitalWrite(8, HIGH);   
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
    delay(2);                      // delay
      //STEP 3
  digitalWrite(9, LOW);   
  digitalWrite(8, HIGH);   
  digitalWrite(7, HIGH);
  digitalWrite(6, LOW);
    delay(2);                      // delay
      //STEP 4
  digitalWrite(9, LOW);   
  digitalWrite(8, LOW);   
  digitalWrite(7, HIGH);
  digitalWrite(6, HIGH);
    delay(2);                      // delay
}


void right()  //+++++++++++++++++++++++++++++++++++++++++++++++RIGHT++++++++++++++++++++++++++++++++++++++++++++++
{
   //STEP 1
  digitalWrite(13, HIGH);   
  digitalWrite(12, LOW);   
  digitalWrite(11, LOW);
  digitalWrite(10, HIGH);
  delay(2);                      // delay
      //STEP 2
  digitalWrite(13, HIGH);   
  digitalWrite(12, HIGH);   
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
    delay(2);                      // delay
      //STEP 3
  digitalWrite(13, LOW);   
  digitalWrite(12, HIGH);   
  digitalWrite(11, HIGH);
  digitalWrite(10, LOW);
    delay(2);                      // delay
      //STEP 4
  digitalWrite(13, LOW);   
  digitalWrite(12, LOW);   
  digitalWrite(11, HIGH);
  digitalWrite(10, HIGH);
    delay(2);                      // delay


//motor2
  //STEP 1
  digitalWrite(9, HIGH);   
  digitalWrite(8, LOW);   
  digitalWrite(7, LOW);
  digitalWrite(6, HIGH);
  delay(2);                      // delay
      //STEP 2
  digitalWrite(9, HIGH);   
  digitalWrite(8, HIGH);   
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
    delay(2);                      // delay
      //STEP 3
  digitalWrite(9, LOW);   
  digitalWrite(8, HIGH);   
  digitalWrite(7, HIGH);
  digitalWrite(6, LOW);
    delay(2);                      // delay
      //STEP 4
  digitalWrite(9, LOW);   
  digitalWrite(8, LOW);   
  digitalWrite(7, HIGH);
  digitalWrite(6, HIGH);
    delay(2);                      // delay
}


void left()   //+++++++++++++++++++++++++++++++++++++++++++++++left++++++++++++++++++++++++++++++++++++++++++++++
{
 //STEP 1
  digitalWrite(10, HIGH);   
  digitalWrite(11, LOW);   
  digitalWrite(12, LOW);
  digitalWrite(13, HIGH);
  delay(2);                      // delay
      //STEP 2
  digitalWrite(10, HIGH);   
  digitalWrite(11, HIGH);   
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
    delay(2);                      // delay
      //STEP 3
  digitalWrite(10, LOW);   
  digitalWrite(11, HIGH);   
  digitalWrite(12, HIGH);
  digitalWrite(13, LOW);
    delay(2);                      // delay
      //STEP 4
  digitalWrite(10, LOW);   
  digitalWrite(11, LOW);   
  digitalWrite(12, HIGH);
  digitalWrite(13, HIGH);
    delay(2);                      // delay


//motor2
  //STEP 1
  digitalWrite(6, HIGH);   
  digitalWrite(7, LOW);   
  digitalWrite(8, LOW);
  digitalWrite(9, HIGH);
  delay(2);                      // delay
      //STEP 2
  digitalWrite(6, HIGH);   
  digitalWrite(7, HIGH);   
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
    delay(2);                      // delay
      //STEP 3
  digitalWrite(6, LOW);   
  digitalWrite(7, HIGH);   
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
    delay(2);                      // delay
      //STEP 4
  digitalWrite(6, LOW);   
  digitalWrite(7, LOW);   
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
    delay(2);                      // delay
}
Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*