RobotKitMenu-7

From ArduinoInfo
Jump to navigation Jump to search
BACK to MENU

ROBOT COMMANDS AND EDITING:

Now that you have built the Robot and have it running, you can learn to edit the Commands and create a new Software Sketch to make the Robot follow your own commands. Some of the different Commands and what they do are listed below:

  • Forward: Runs both motors in the forward direction
  • Backward: Runs both motor in a backward direction
  • TurnRight: Makes the left motor go forward with the right motor stopped
  • TurnLeft: Makes the right motor go forward with the left motor stopped
  • SpinRight: Runs the right motor in reverse and the left motor in forward
  • SpinLeft: Runs the left motor in reverse and the right motor in forward
  • Wait: Stops the robot for a certain length of time that you decide
  • Stop: Stops both motors


You can change the Robot Commands in these ways:

Speed:

  • Forward:
    • ForwardSlow, ForwardMedium, ForwardFast, ForwardMax
  • Backward:
    • BackwardSlow, BackwardMedium, BackwardFast, BackwardMax

Length of Time a Command will Run:

  • Time is measured in milliseconds (Ms) so 1000 Ms = 1 second, 500 Ms = 1/2 second, etc.

Order in which the Commands Occur:

  • You can mix and match commands to make the Robot run in many different patterns


How to use this code

It's interesting to look at the format of the code in the Software Sketch below. There are basically 4 parts to the Software Sketch after the Title and introduction:
  1. Definition Area: This is where the Motor Control Library, motor speeds and adjustments are defined (created by the original programmer - don't change this!)
  2. Setup Area: T his is code that runs only once at the beginning to set things up (defined by the original programmer - don't change this!)
  3. Editable Area: C ontains the code that You can change to make a new robot control program!
  4. Motor Control Area: Code which specifies what the motors will do in each of the different Commands. (defined by the original programmer - don't change this!)



Now you can copy and paste the software Sketch below into the Arduino IDE and try it out as-is on your own Robot. Observe your Robot carefully. What did the Robot do? How do the commands in the Editable Area control what the robot does?

Now you're ready to make some changes. Try changing the timing and order of the different commands in the Editable Area of the Software Sketch. Can you make the robot "dance"? Use "SAVE AS" to save different versions; give each a name.

/* YourDuino Basic Robot Kit V2 User EDitable Software Sketch
How To Page that goes with this Software Sketch:
https://arduinoinfo.mywikis.net/wiki/Robot_Commands
 WHAT IT DOES:
 - Runs the robot motors
 - Program the robot to make different moves such as Forward, SpinRight, Back etc.
 - SEE the comments after "//" on each line below

 - V2.20 3/27/2015 maryalice@yourduino.com
 Questions: terry@yourduino.com */

/***************************************************************/
/*---------------------( DEFINITION AREA )---------------------*/
/***************************************************************/

/*-----( Import needed libraries )-----*/
#include <YD_MotorDriver1.h>  // For control of the two DC Motors

/*-----( Declare Constants and Pin Numbers )-----*/

// NOTE: Pins 9 (Motor1) and 10 (Motor2) are predefined, unchangeable
#define  Motor1A  8
#define  Motor1B  7
#define  Motor2C  6
#define  Motor2D  5
/* Motor speed can be in the range of 200 - 400 only */
#define  StartMoveSpeed    200  // Lowest Motor Speed value for start of motion
/* The section sets up the motor speeds for slow, medium, fast 
 The adjust numbers help make the robot run straight.
 Positive numbers turn more right, negative numbers turn more left.*/
#define  SlowMoveSpeed     280
#define  SlowMoveAdjust     0   // Adjust to go straight: - Left + Right??

#define  MediumMoveSpeed   300
#define  MediumMoveAdjust   0  // Adjust to go straight: - Left + Right

#define  FastMoveSpeed     350
#define  FastMoveAdjust     0  // Adjust to go straight: - Left + Right

#define  MaxMoveSpeed     385
#define  MaxMoveAdjust     0  // Adjust to go straight: - Left + Right
/*-----( Declare objects )-----*/
YD_MotorDriver1 RobotDriver(Motor1A,Motor1B,Motor2C,Motor2D); // Set pins

/***************************************************************/
/*-----------------( SETUP AREA RUNS ONCE )--------------------*/
/***************************************************************/

void setup()
{
  Serial.begin(115200);  //Start the "Serial Monitor"
  delay(1000);
  Serial.println("YourDuino Robot Kit V2 User Editable Robot Control");
  //--NOTE:  Motor Pins set to OUTPUT by their libraries  
  RobotDriver.init();  //Start up the Robot Driver Library
}//--(end setup )---

/******************( LOOP: RUNS CONSTANTLY )********************/
void loop()
{
/***************************************************************/
/*--------------------( EDITABLE AREA )------------------------*/
/***************************************************************/
  ForwardSlow(2000);
  Wait(2000);

  ForwardMedium(2000);
  Wait(2000);

  BackwardMedium(4000);
  Wait(2000);

  SpinRight(5000);
  Wait(2000);

  SpinLeft(5000);
  Wait(5000);

//Now this will go back to the beginning of code in the Editable Area

/***************************************************************/
/*-----------------( END OF EDITABLE AREA )--------------------*/
/***************************************************************/
}//--(end main loop )---

/***************************************************************/
/*-------------------( MOTOR CONTROL AREA )--------------------*/
/***************************************************************/

void ForwardSlow(int howLong)
{
  RobotDriver.Motor1Speed(SlowMoveSpeed + SlowMoveAdjust);
  RobotDriver.Motor2Speed(SlowMoveSpeed - SlowMoveAdjust);
  delay(howLong);
  Stop();
}
/*---------------------------*/

void ForwardMedium(int howLong)
{
  RobotDriver.Motor1Speed(MediumMoveSpeed + MediumMoveAdjust);
  RobotDriver.Motor2Speed(MediumMoveSpeed - MediumMoveAdjust);
  delay(howLong);
  Stop();
}
/*---------------------------*/
void ForwardFast(int howLong)
{
  RobotDriver.Motor1Speed(FastMoveSpeed + FastMoveAdjust);
  RobotDriver.Motor2Speed(FastMoveSpeed - FastMoveAdjust);
  delay(howLong);
  Stop();
}

/*---------------------------*/
void ForwardMax(int howLong)
{
  RobotDriver.Motor1Speed(MaxMoveSpeed + MaxMoveAdjust);
  RobotDriver.Motor2Speed(MaxMoveSpeed - MaxMoveAdjust);
  delay(howLong);
  Stop();
}
/*---------------------------*/
void BackwardSlow(int howLong)
{
  RobotDriver.Motor1Speed(- SlowMoveSpeed );
  RobotDriver.Motor2Speed(- SlowMoveSpeed );
  delay(howLong);
  Stop();
}
/*---------------------------*/
void BackwardMedium(int howLong)
{
  RobotDriver.Motor1Speed(- MediumMoveSpeed);
  RobotDriver.Motor2Speed(- MediumMoveSpeed);
  delay(howLong);
  Stop();
}
/*---------------------------*/
void BackwardFast(int howLong)
{
  RobotDriver.Motor1Speed(- FastMoveSpeed);
  RobotDriver.Motor2Speed(- FastMoveSpeed);
  delay(howLong);
  Stop();
}

/*---------------------------*/
void BackwardMax(int howLong)
{
  RobotDriver.Motor1Speed(- MaxMoveSpeed);
  RobotDriver.Motor2Speed(- MaxMoveSpeed);
  delay(howLong);
  Stop();
}
/*---------------------------*/
void Stop()
{
  RobotDriver.Motor1Speed(0);
  RobotDriver.Motor2Speed(0);
}
/*---------------------------*/
void TurnLeft(int howLong)
{
  RobotDriver.Motor1Speed(0);
  RobotDriver.Motor2Speed(MediumMoveSpeed);
  delay(howLong);
  Stop();
}
/*---------------------------*/
void TurnRight(int howLong)
{
  RobotDriver.Motor1Speed(MediumMoveSpeed);
  RobotDriver.Motor2Speed(0);
  delay(howLong);
  Stop();
}
/*---------------------------*/
void SpinLeft(int howLong)
{
  RobotDriver.Motor1Speed(-MediumMoveSpeed);
  RobotDriver.Motor2Speed( MediumMoveSpeed);
  delay(howLong);
  Stop();
}
/*---------------------------*/
void SpinRight(int howLong)
{
  RobotDriver.Motor1Speed(MediumMoveSpeed);
  RobotDriver.Motor2Speed(- MediumMoveSpeed);
  delay(howLong);
  Stop();
}
/*---------------------------*/
void Wait(int howLong)
{
  delay(howLong);
}
/*---------------------------*/

//*********( THE END )***********









Go To the Next Section: [/RobotKitMenu-8 Collision Avoidance]
OR back to the Main Menu :


Please email comments, critiques, suggestions and questions to: terry@yourduino.com