Bluetooth Controlled Direction and Gripper Sketch

This Arduino sketch allows the robot to be controlled by a smartphone via bluetooth communication. In order to complete this challenge, the robot needed to be maneuvered to the end of an L shaped track in order to retrieve a rubber ball. The ball then needed to be returned to the starting area in order to drop the ball into a receptacle. This process needed to be completed three times in a row without touching the robot in order to complete this challenge.


Arduino Sketch

//********************************************SUMMARY************************************************
//  This sketch is the program designed to control the robot to navigate the ball portion of the
//  obsticle course.  It will communicate with a bluetooth cell phone from which it will recieve input
//  to control the speed, and direction of the robot as well as to minipulate the gripper at the front
//  of the robot.
//******************************LIBRARIES THAT ARE TO BE INCLUDED************************************ #include <Adafruit_MotorShield.h> #include <Gripper.h> #include <MegaServo.h> #include <motor.h> #include <SoftwareSerial.h>
//*******************CREATE THE MOTOR SHIELD OBJECT WITH THE DEFAULT I2C ADDRESS********************* Adafruit_MotorShield AFMS = Adafruit_MotorShield();
//*****************************GLOBAL CONSTANTS ARE DECLARED HERE************************************ const int DELAY_TIME = 1000; const float UNIT_CONVERSION = 0.003937; //Converts whatever unit pops out for the range finder to in const byte rxPin = 18; const byte txPin = 19;
//*****************************GLOBAL VARIABLES ARE DECLARED HERE************************************ char message[256]; int index = 0;
//**********************************POINTERS ARE DECLARED HERE*************************************** Gripper *openGrip;// = new Gripper(36);
MegaServo myGripper;
motor *RFMotor = new motor(&AFMS, 1, 0); //right front motor *LFMotor = new motor(&AFMS, 2, 1); //left front motor *LRMotor = new motor(&AFMS, 3, 1); //left rear motor *RRMotor = new motor(&AFMS, 4, 0); //right rear
SoftwareSerial BTSerial(rxPin, txPin);
//=================================================================================================== //=========================================SETUP STARTS HERE========================================= //=================================================================================================== void setup() {
pinMode(rxPin, INPUT); pinMode(txPin, OUTPUT);
Serial.begin(38400); // Usually 9600 for BT mode, although it is sometimes 38400: Serial1.begin(115200); AFMS.begin(); Serial.println("test");
openGrip = new Gripper(36); Gripper(22); delay(1000); // delay for 1 second before moving on openGrip -> close(); // call the close() function from the .cpp file to close the gripper. }
//=================================================================================================== //==========================================LOOP STARTS HERE========================================= //=================================================================================================== void loop() { if (Serial1.available()) { message[index] = (char)Serial1.read(); if (message[index] == 0x3F) //Checks for a ? character { switch (message[0]) { // was previously message[0] case '1': Serial.print("Button 1 Pressed"); LFMotor->setDrive(60.0, FORWARD); LRMotor->setDrive(60.0, FORWARD); RFMotor->setDrive(150.0, FORWARD); RRMotor->setDrive(150.0, FORWARD); break; case '2': Serial.print("Button 2 Pressed"); LFMotor->setDrive(125.0, FORWARD); LRMotor->setDrive(125.0, FORWARD); RFMotor->setDrive(132.0, FORWARD); RRMotor->setDrive(132.0, FORWARD); break; case '3': Serial.print("Button 3 Pressed"); LFMotor->setDrive(130.0, FORWARD); LRMotor->setDrive(130.0, FORWARD); RFMotor->setDrive(65.0, FORWARD); RRMotor->setDrive(65.0, FORWARD); break; case '4': Serial.print("Button 4 Pressed"); LFMotor->setDrive(60.0, BACKWARD); LRMotor->setDrive(60.0, BACKWARD); RFMotor->setDrive(60.0, FORWARD); RRMotor->setDrive(60.0, FORWARD); break; case '5': Serial.print("Button 5 Pressed"); if (openGrip->isOpen()) { openGrip->close(); } else { openGrip->open(); } break; case '6': Serial.print("Button 6 Pressed"); LFMotor->setDrive(60.0, FORWARD); LRMotor->setDrive(60.0, FORWARD); RFMotor->setDrive(60.0, BACKWARD); RRMotor->setDrive(60.0, BACKWARD); break; case '7': Serial.print("Button 7 Pressed"); LFMotor->setDrive(60.0, BACKWARD); LRMotor->setDrive(60.0, BACKWARD); RFMotor->setDrive(120.0, BACKWARD); RRMotor->setDrive(120.0, BACKWARD); break; case '8': Serial.print("Button 8 Pressed"); LFMotor->setDrive(130.0, BACKWARD); LRMotor->setDrive(130.0, BACKWARD); RFMotor->setDrive(120.0, BACKWARD); RRMotor->setDrive(120.0, BACKWARD); break; case '9': Serial.print("Button 9 Pressed"); LFMotor->setDrive(150.0, BACKWARD); LRMotor->setDrive(150.0, BACKWARD); RFMotor->setDrive(60.0, BACKWARD); RRMotor->setDrive(60.0, BACKWARD); break; default : Serial.print("BREAK"); break; }
index = 0; Serial.println(); }
else if (message[index] == 0x24) { //Checks for a $ character switch (message[0]) { case '1': Serial.print("Button 1 Released"); LFMotor->stop(); LRMotor->stop(); RFMotor->stop(); RRMotor->stop(); break; case '2': Serial.print("Button 2 Released"); LFMotor->stop(); LRMotor->stop(); RFMotor->stop(); RRMotor->stop(); break; case '3': Serial.print("Button 3 Released"); LFMotor->stop(); LRMotor->stop(); RFMotor->stop(); RRMotor->stop(); break; case '4': Serial.print("Button 4 Released"); LFMotor->stop(); LRMotor->stop(); RFMotor->stop(); RRMotor->stop(); break; case '5': Serial.print("Button 5 Released"); break; case '6': Serial.print("Button 6 Released"); LFMotor->stop(); LRMotor->stop(); RFMotor->stop(); RRMotor->stop(); break; case '7': Serial.print("Button 7 Released"); LFMotor->stop(); LRMotor->stop(); RFMotor->stop(); RRMotor->stop(); break; case '8': Serial.print("Button 8 Released"); LFMotor->stop(); LRMotor->stop(); RFMotor->stop(); RRMotor->stop(); break; case '9': Serial.print("Button 9 Released"); LFMotor->stop(); LRMotor->stop(); RFMotor->stop(); RRMotor->stop(); break; default: Serial.print("BREAK"); break; }
index = 0; Serial.println(); }
else { index++; //Increases the index counter by one if neither a ? or a $ was encountered. } } }
//=================================================================================================== //=======================================COMMANDS CHEAT SHEET======================================== //===================================================================================================
//**********************************************ENCODER********************************************** // encoder(unsigned int slots, unsigned int diameter, unsigned int pin) // void updateTime(unsigned int time) //function to update the current time from the internal timer // double getSpeed() //calculate the speed // void zeroSpeed(); //set the speed to zero
//**********************************************GRIPPER********************************************** // gripper(unsigned int pin); //Constructor, pin attached // void open(); //no return // void close(); //no return // bool isOpen(); //returns what state the gripper is in
//********************************************LINECONTROL******************************************** // lineControl(linesensor *leftPTR, linesensor *rightPTR, speedcontrol *leftSpeed, speedcontrol *rightSpeed); // void detectNone(); // void updateSpeed(); // void detectLeft(); // void detectRight();
//*********************************************LINESENSOR******************************************** // linesensor(unsigned in pin, byt sensornumber); // bool linestatus();
//***********************************************MOTOR*********************************************** // motor(Adafruit_MotorShield*shieldPtr,byte motorNumber, boolean polarity); // void setDrive(unsigned int dutyCycle, unsigned int command) // set the motor's direction // void stop(); // emergency stop - Set speed to zero // unsigned int getDrive(); // returns the speed
//**********************************************RANGE_ECHO******************************************* // Range_Echo(int TP, int EP); // long Timing(); // calculates the distance for which the object is from the sensor // long Ranging(int sys); // sets the units, SYS for centameters, INC for inches
//*********************************************SPEEDCONTROL****************************************** // speedcontrol(HardwareSerial *port, encoder *encoderPtr, motor *motorPtr1, motor *motorPtr2,double sampleTime); // void setDesiredSpeed(double speed); // sets the desired speed // double getDesiredSpeed(); // returns the value of the desired speed // void enable(); // sets the internal enable flag to 1 // void disable(); // sets the internal enable flag to 0 // void update(); // calculates the current speed // void setPGain(double gain); // void setIGain(double gain); // void setDGain(double gain); // void setILimit(double limit);