0% found this document useful (0 votes)
13 views5 pages

Codes of Arduino (XCTU)

This document is a C++ program that controls a dual motor shield for robotic movement. It includes functions for moving the robot forward, backward, turning left, and turning right, as well as handling user input for controlling these actions. The program also includes fault detection to stop the motors in case of an error.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Codes of Arduino (XCTU)

This document is a C++ program that controls a dual motor shield for robotic movement. It includes functions for moving the robot forward, backward, turning left, and turning right, as well as handling user input for controlling these actions. The program also includes fault detection to stop the motors in case of an error.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include "DualVNH5019MotorShield.

h" // Include Motor Shield Library

char input; //Input from User


int velo=150;
int lastOp = 0;
DualVNH5019MotorShield md;

void performLastOp()
{
if ( lastOp == 1 )
turnleftaround(velo);
else if ( lastOp == 2 )
turnrightaround(velo);
else if ( lastOp == 3 )
moveforward(velo);
else if ( lastOp == 4 )
moveback(velo);
else if ( lastOp == 5 )
turnright(velo);
else if ( lastOp == 6 )
turnleft(velo);

void stopIfFault() //If There is a fault stop all motors


{
if (md.getM1Fault())
{
[Link]("M1 fault");
while(1);
}
if (md.getM2Fault())
{
[Link]("M2 fault");
while(1);
}
}
void moveforward( int vel) //Moveforward Function
{
md.setM1Speed(-vel);
md.setM2Speed(vel);
stopIfFault();

}
void moveback(int vel) //Moveback Function
{
md.setM1Speed(vel);
md.setM2Speed(-vel);
stopIfFault();

}
void turnrightaround( int vel) //Turn right around Function
{
md.setM1Speed(vel);
md.setM2Speed(vel);
stopIfFault();

}
void turnleftaround( int vel) //Turn left around Function
{
md.setM1Speed(-vel);
md.setM2Speed(-vel);
stopIfFault();

}
void turnright( int vel) //Turn right Function
{
md.setM1Speed(-vel/4);
md.setM2Speed(vel);
stopIfFault();

}
void turnleft( int vel) //Turn left Function
{
md.setM1Speed(-vel);
md.setM2Speed(vel/4);
stopIfFault();

void stoop() // Stop Function


{
md.setM1Speed(0);
md.setM2Speed(0);
stopIfFault();

void setup()
{

[Link](9600); //[Link](9600);
[Link]();
}

void loop()
{
if ([Link]())
{

input = [Link]();
switch(input){
case 'q':// turn left around auto
turnleftaround(velo);
lastOp = 1;
break;
case 'e': // turn right around for auto
turnrightaround(velo);
lastOp = 2;
break;
case 'w': // forward
moveforward(velo);
lastOp = 3;
break;
case 's':// backward
moveback(velo);
lastOp = 4;
break;

case 'd':// right


turnright(velo);
lastOp = 5;
break;
case 'a':// left
turnleft(velo);
lastOp = 6;
break;
case 'x':// stop
stoop();
lastOp = 7;
break;
case 'r':// increase speed
velo=velo+25;
performLastOp();
break;
case 'f':// decrease speed
velo=velo-25;
performLastOp();
break;
case 'o': // forward
moveforward(75);
break;
case 'z':// turn left around auto
turnleftaround(100);
break;
case 'c': // turn right around for auto
turnrightaround(100);
break;
}

}
}

You might also like