// Code added so far. Sorry is so messy For Motor Functions which as far as I know is ok. .h * BbotmfFour.h * * Created on: Dec 25, 2015 * Author: Shannon */ #ifndef BBOTMFFOUR_H_ #define BBOTMFFOUR_H_ // Include below defines #include "Arduino.h" // Name space underneath includes // Constants beneath namespaces /* * Using constants instead of defines because defines * can cause hard to find errors. */ // connect ENA1 to 2 //const int ENA1 = 9; // Not needed is hooked up to 5 volts const int ENA2 = 3; // this is needed to control speed of back wheels const int IN1 = 2; // Front turn const int IN2 = 4; // Front turn const int IN3 = 6; // Back turn const int IN4 = 7; // Back turn class BbotmfFour { public: // cstr's and dstr's BbotmfFour(); BbotmfFour(int, int); virtual ~BbotmfFour(); /** * mode and percent values change so * an overloaded constructor as shown above * is needed for them. * They are also passed in the private * functions below. * Public vars used for passing in constructor. */ int mode, percent; void motor1(); void motor2(); void testRunMotorFunction(); private: // Private vars used for passing in below functions int _mode, _percent; //****************** Motor A control ******************* // void motorA(int mode, int percent) void motorOne(int, int); //****************** Motor B control ******************* // void motorB(int mode, int percent) // void motorB(int mode, int percent) void motorTwo(int, int); // int retPercent(int); int retPerMode(); void TestRunMotorFunction(); }; #endif /* BBOTMFFOUR_H_ */ .cpp /* * BbotmfFour.cpp * * Created on: Dec 25, 2015 * Author: Shannon */ #include "BbotmfFour.h" BbotmfFour::BbotmfFour() { // leave empty will do what it is supposed to do } BbotmfFour::BbotmfFour(int mode, int percent) { // leave empty will do what it is supposed to do _mode = 0; _percent = 0; //set all of the outputs pinMode(ENA2, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT); _mode = mode; _percent = percent; } BbotmfFour::~BbotmfFour() { // leave empty will do what it is supposed to do } // Behaviors encaps void BbotmfFour::motor1() { // leave empty will do what it is supposed to do motorOne(_mode, _percent); } void BbotmfFour::motor2() { // leave empty will do what it is supposed to do motorTwo(_mode, _percent); } void BbotmfFour::testRunMotorFunction() { TestRunMotorFunction(); } /****************** Motor A control ******************* // void motorA(int mode, int percent) ****************** Motor A control *******************/ // Private Behavior void BbotmfFour::motorOne(int _mode, int _percent) { // leave empty will do what it is supposed to do //change the percentage range of 0 -> 100 into the PWM //range of 0 -> 255 using the map function int duty = map(_percent, 0, 100, 0, 255); switch(_mode) { case 1: //turn clockwise Right //setting IN1 high connects motor lead 1 to +voltage digitalWrite(IN1, HIGH); //setting IN2 low connects motor lead 2 to ground digitalWrite(IN2, LOW); /** //use pwm to control motor speed through enable pin analogWrite(ENA1, duty); **/ break; case 2: //turn counter-clockwise Left //setting IN1 low connects motor lead 1 to ground digitalWrite(IN1, LOW); //setting IN2 high connects motor lead 2 to +voltage digitalWrite(IN2, HIGH); break; case 3: //brake motor //setting IN1 low connects motor lead 1 to ground digitalWrite(IN1, LOW); //setting IN2 high connects motor lead 2 to ground digitalWrite(IN2, LOW); break; } //********************************************************** } void BbotmfFour::motorTwo(int _mode, int _percent) { // leave empty will do what it is supposed to do //change the percentage range of 0 -> 100 into the PWM //range of 0 -> 255 using the map function int duty = map(_percent, 0, 100, 0, 255); switch(_mode) { case 0: //disable/coast digitalWrite(ENA2, LOW); //set enable low to disable B break; case 1: //turn clockwise //setting IN3 high connects motor lead 1 to +voltage digitalWrite(IN3, HIGH); //setting IN4 low connects motor lead 2 to ground digitalWrite(IN4, LOW); //use pwm to control motor speed through enable pin analogWrite(ENA2, duty); break; case 2: //turn counter-clockwise //setting IN3 low connects motor lead 1 to ground digitalWrite(IN3, LOW); //setting IN4 high connects motor lead 2 to +voltage digitalWrite(IN4, HIGH); //use pwm to control motor speed through enable pin analogWrite(ENA2, duty); break; case 3: //brake motor //setting IN3 low connects motor lead 1 to ground digitalWrite(IN3, LOW); //setting IN4 high connects motor lead 2 to ground digitalWrite(IN4, LOW); //use pwm to control motor braking power //through enable pin analogWrite(ENA2, duty); break; } //********************************************************** } // Private attribute, set it up as much as possibe // to be added to above behaviors. int BbotmfFour::retPerMode() { // Attribute to configure and return percent // pay special attention how this is set up with no // asterick in front of firs var but one in front of the second // one but no comma to seperate. Also the fact that // there is more than one being returned. return _mode *_percent ; } void BbotmfFour::TestRunMotorFunction() { /** // motorA(1, 15); //have motor A turn clockwise at 15% speed motorA(1, 50); //have motor A turn clockwise at 50% speed delay(5000); //let motor A run for 5 seconds motorA(3, 100); //brake motor A with 100% braking power */ /** * This is added to this class as test run code to make sure bot is working */ Serial.print("\n\n+-------------------------------------------------------------+\n\n"); Serial.print(" Entering main\n\n"); // bbmotoright->motor1(); motor1(); Serial.print(" First call to function - Call for fight turn\n\n"); delay(5000); Serial.print(" Delay for 5 seconds\n\n"); motor1(); Serial.print(" Second call to function - Call for left turn \n\n"); delay(5000); Serial.print(" Delay for 5 seconds\n\n"); motor1(); Serial.print(" 3rd call to Function - Call for Break \n\n"); delay(5000); Serial.print(" Delay for 5 seconds\n\n"); Serial.print("\n\n+-------------------------------------------------------------+\n\n"); motor2(); Serial.print(" Fourth call to function - Call for forward \n\n"); delay(5000); Serial.print(" Delay for 5 seconds\n\n"); motor2(); Serial.print(" Fifth call to function - Call for Reverse \n\n"); delay(5000); Serial.print(" Delay for 5 seconds\n\n"); motor2(); Serial.print(" Sixth call to function - Call to coast \n\n"); delay(5000); Serial.print(" Delay for 5 seconds\n\n"); motor2(); Serial.print(" Seventh call to Function - Call for FullBreak\n\n"); delay(5000); Serial.print(" Delay for 5 seconds\n\n"); Serial.print("\n\n+-------------------------------------------------------------+\n\n"); } Ping code is very basic and I am still trying to figure out how to compare variable directions for the best path . All I have so far is below. .h /* * BBotPingFour.h * * Created on: Dec 25, 2015 * Author: Shannon */ #ifndef BBOTPINGFOUR_H_ #define BBOTPINGFOUR_H_ #include "Arduino.h" // #include "NewPing.h" // Libraries are picky Make sure you don't have // something like ie NewPing inside of a NewPing folder there should only be one. /**************** notes for 3 pin ping and 4 pin ping // Testing both 3 pin and 4 pin sensor * The 3 pin only have one pin for trigger and echo, * while the 4 pin has a pin for Trigger and a pin for echo * // since we are learning the basics we will * code for both the two will be tested. * ******************** */ /********************* Testing 3 things on 2nd small breadboard servo pin 8 3 pin ping pin 9 4 pin Trig_pin 10 4 pin Echo_pin 11 ***********************/ // constant vars don't change and you'd just use them from here ////////////////// defining the ping pins const int PINGER = 9; // examp 1 pin sens Trigger and receives Echo // const int TRIG_PIN = 10; // examp 2 pin sends Trigger only // const int ECHO_PIN = 11; // examp 2 pin receives Echo only /***************** Adding 4 pin sensor constants declare and define const int PINGER = 9; // examp 1 pin sends Trigger and receives Echo const int TRIG_PIN = 10; // examp 2 pin sends Trigger only const int ECHO_PIN = 11; // examp 2 pin receives Echo only ********************/ // For the ping you want to use the value in main You may want to put the function in a variable in main. class BBotPingFour { public: // cstr's and dstr's BBotPingFour(); virtual ~BBotPingFour(); // Public variables // int distance, duration; void theDistance(); // Private variables. private: void TheDistance(); long microsecondsToInches(long microseconds); long microsecondsToCentimeters(long microseconds); }; #endif /* BBOTPINGFOUR_H_ */ .cpp * BBotPingFour.cpp * * Created on: Dec 25, 2015 * Author: Shannon */ #include "Arduino.h" #include "NewPing.h" #include "BBotPingFour.h" BBotPingFour::BBotPingFour() { } BBotPingFour::~BBotPingFour() { } void BBotPingFour::theDistance() { TheDistance(); } // Private variables. void BBotPingFour::TheDistance() { // establish variables for duration of the ping, // and the distance result in inches and centimeters: // local declaration, should be only function that needs these varsa // Otherwise declare them globally (the same area the constants are declared long duration, inches, cm; // Initializing duration = 0; inches = 0; cm = 0; // The PING))) is triggered by a HIGH pulse of 2 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: pinMode(PINGER, OUTPUT); digitalWrite(PINGER, LOW); delayMicroseconds(2); digitalWrite(PINGER, HIGH); delayMicroseconds(5); digitalWrite(PINGER, LOW); // The same pin is used to read the signal from the PING))): a HIGH // pulse whose duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. pinMode(PINGER, INPUT); duration = pulseIn(PINGER, HIGH); // convert the time into a distance inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); Serial.print("*******************For Pinger***************"); Serial.print("in, "); Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println(); } long BBotPingFour::microsecondsToInches(long microseconds) { return microseconds / 74 / 2; } long BBotPingFour::microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; } Ok I figure that is a good start. Stuck on ping because I run the code in the arduino IDE and it run fine but when I try running that same code (kind of ) in the Eclipse Environment my servo comes to a grinding halt and I mean you can hear the gears grinding. I don't see why it is not working. Here is what I got. .h /* * BBotServoFive.h * * Created on: Dec 27, 2015 * Author: Shannon */ #ifndef BBOTSERVOFIVE_H_ #define BBOTSERVOFIVE_H_ #include "Arduino.h" #include "Servo.h" const int SONE = 6; // putting myservo object creation here causes errors do in private. // Servo myservo; /********************* Testing 3 things on 2nd small breadboard servo pin 8 3 pin ping pin 9 4 pin Trig_pin 10 4 pin Echo_pin 11 ***********************/ class BBotServoFive { public: BBotServoFive(); virtual ~BBotServoFive(); void toRight(); private: // declaring Servo Servo myServo; int pos = 5; // declaring Servo to Right void ToRight(); }; #endif /* BBOTSERVOFIVE_H_ */ .cpp * BBotServoFive.cpp * * Created on: Dec 27, 2015 * Author: Shannon */ #include "BBotServoFive.h" #include "Arduino.h" #include "Servo.h" BBotServoFive::BBotServoFive() { // TODO Auto-generated constructor stub myServo.attach(SONE); // attaches the servo on pin 9 to the servo object } BBotServoFive::~BBotServoFive() { // TODO Auto-generated destructor stub } // Globals // add global variable declaration for all functions // Public encaps void BBotServoFive::toRight() { // TODO Auto-generated destructor stub ToRight(); } // private: void BBotServoFive::ToRight() { /** The for loop is special in that it need the variable in it's * Params So it don't have to be declared in the header. * Each one in that position is specifically for that for loop. */ for(pos = 5; pos <= 90; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myServo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } delay(5000); for(pos = 90; pos <= 175; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myServo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } delay(5000); for(pos = 175; pos>= 90; pos-=1) // goes from 180 degrees to 0 degrees { myServo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } delay(5000); // center to right for(pos = 90; pos>=5; pos -=1) // goes from 180 degrees to 0 degrees { myServo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } delay(3000); }