Tuesday, October 15, 2013

Simplified Serial

Tasked to build a robot, using a Mega 2560 Arduino board, that went forwards, backwards, turned left, turned right, and stopped utilizing dc motors. I came up with the specimen in the videos below. A total of about 1 hour was spent wiring, coding, and debugging.





In this video we can see the movements derived from the code.




In this video we can see the LCD Display as it reads the values associated with the motors.


Wiring:


In this photo the connection to S1 and Ground of the motor controller (4 input side) can be seen.


In this photo the connections to the motors, switch, and battery can be seen.


In this photo the connections to the arduino and breadboard can be seen.


Code:

/* In this program the robot will demonstrate basic movement utilizing DC motors. This program is designed
   to make to robot move forwards, backwards, turn left, turn right, veer left, veer right, and stop.
   
   Program written by Korbin Barker on 10/13/13
   
   Robotic Platform: Undetermined
   Microcontroller:  Arduino MEGA 2560
   Motors:           4 DC Motors
                     1 Sabertooth 2x12 Motor Controller
   Inputs:           1 Flip Switch
   Power Supply:     12 Volts DC
*/

/* ================ INCLUDE LIBRARY STEP =================*/

#include <SoftwareSerial.h>

/* ================= INITIALIZATION ===================== */

const int LCDDisplay = 14; //LCD Display is in TX 3 (port 14)

const int stopValue = 0; //neutral value
const int leftStop = 64;  //stops left motor
const int rightStop = 191;  //stops right motor
const int forwardLeft = 127; //full forward (left)
const int forwardRight = 255; //full forward (right)
const int forwardLeftSlow = 102; //slow forward (left)
const int forwardRightSlow = 230; //slow forward (right)
const int backwardLeft = 1;  //full backward (left)
const int backwardRight = 128;  //full backward (right)
const int backwardLeftSlow = 26; //full backward
const int backwardRightSlow =  153; //slow backward
const int time = 2000; //sets a time for delays

int leftValue = 0; //intializes left value for LCD Display
int rightValue = 0; //initializes right value for LCD Display

SoftwareSerial mySerial = SoftwareSerial(255, LCDDisplay);

/* =================== FUNCTION DECLARATION ==================== */

/* clears LCD Display */
void clearScreen()
{
  mySerial.write(12);  //clears screen
  mySerial.write(17);  //turns backlight on
  delay(5);
}

/* writes values to LCD */
void writeDisplay()
{
  clearScreen();
  mySerial.print("Left Value:");
  mySerial.print(leftValue);
  mySerial.write(13);
  mySerial.write(17);
  mySerial.print("Right Value:");
  mySerial.print(rightValue);
  delay(5);
}

/* stops robot's movement */
void stopRobot()
{
  rightValue = leftValue = stopValue; //sets values to be printed on LCD Display
  Serial1.write(stopValue); //writes pulse neutral (stopped) at 0
  delay(time);
}

/* moves robot forward */
void forward()
{
  leftValue = forwardLeft;  // sets value to be printed on LCD Display
  rightValue = forwardRight;   // sets value to be printed on LCD Display 
  Serial1.write(forwardLeft);  
  Serial1.write(forwardRight);
  writeDisplay();
  delay(time);
  stopRobot();
}

/* moves robot forward slowly */
void forwardSlow()
{
  leftValue = forwardLeftSlow;
  rightValue = forwardRightSlow;
  Serial1.write(forwardLeftSlow);
  Serial1.write(forwardRightSlow);
  writeDisplay();
  delay(time);
  stopRobot();
}

/* moves robot backward */
void backward()
{
  leftValue = backwardLeft;
  rightValue = backwardRight;
  Serial1.write(backwardLeft);
  Serial1.write(backwardRight);
  writeDisplay();
  delay(time);
  stopRobot();
}

/* moves robot backward slowly */
void backwardSlow()
{
  leftValue = backwardLeftSlow;
  rightValue = backwardRightSlow;
  Serial1.write(backwardLeftSlow);
  Serial1.write(backwardRightSlow);
  writeDisplay();
  delay(time);
  stopRobot();
}

/* turns robot to the left */
void leftTurn()
{
  leftValue = backwardLeft;
  rightValue = forwardRight;
  Serial1.write(backwardLeft);
  Serial1.write(forwardRight);
  writeDisplay();
  delay(time/2);
  stopRobot();
}

/* turns robot to the right */
void rightTurn()
{
  leftValue = forwardLeft;
  rightValue = backwardRight;
  Serial1.write(forwardLeft);
  Serial1.write(backwardRight);
  writeDisplay();
  delay(time/2);
  stopRobot();
}

/* veers robot to the left */
void leftVeer()
{
  leftValue = forwardLeftSlow;
  rightValue = forwardRight;
  Serial1.write(forwardLeftSlow);
  Serial1.write(forwardRight);
  writeDisplay();
  delay(time);
  stopRobot();
}

/* veers robot to the right */
void rightVeer()
{
  leftValue = forwardLeft;
  rightValue = forwardRightSlow;
  Serial1.write(forwardLeft);
  Serial1.write(forwardRightSlow);
  writeDisplay();
  delay(time);
  stopRobot();
}

/* ================= SETUP STEP ======================*/

void setup()
{
  pinMode(LCDDisplay, OUTPUT);
  digitalWrite(LCDDisplay, HIGH);
  mySerial.begin(9600);
  delay(100);
  Serial1.begin(9600);
  Serial.begin(9600);
}

/* ================ LOOP STEP =======================*/

void loop()
{
  /* see comments under the FUNCTION DECLARATION to see what each of the functions does */
  
  clearScreen();
  stopRobot();
  forward();
  forwardSlow();
  backward();
  backwardSlow();
  leftTurn();
  rightTurn();
  leftVeer();
  rightVeer();  
}


Issues:

No significant issues arose from this assignment that couldn't easily be remedied.




No comments:

Post a Comment