Control a dual DC motor powered buggy using the BBC MicroBit

Dependencies:   microbit

Files at this revision

API Documentation at this revision

Comitter:
elijah_ubit
Date:
Wed Jun 22 15:07:04 2016 +0000
Parent:
0:370b7f440dcf
Commit message:
first

Changed in this revision

buggy_functions.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 370b7f440dcf -r e2fc7f9cbdde buggy_functions.cpp
--- a/buggy_functions.cpp	Thu Jun 09 10:27:13 2016 +0000
+++ b/buggy_functions.cpp	Wed Jun 22 15:07:04 2016 +0000
@@ -14,8 +14,8 @@
 #include "buggy_functions.h"
 
 /*set up pins that will be used to control motors */
-MicroBitPin LeftMotor(MICROBIT_ID_IO_P1, MICROBIT_PIN_P1, PIN_CAPABILITY_ALL);
-MicroBitPin RightMotor(MICROBIT_ID_IO_P2, MICROBIT_PIN_P2, PIN_CAPABILITY_ALL);
+PwmOut Lmotor(MICROBIT_PIN_P1);
+PwmOut Rmotor(MICROBIT_PIN_P2);
 
 //set up LED matrix display
 MicroBitDisplay display;
@@ -23,7 +23,7 @@
 //Trim is an offset that you can adjust to help the buggy drive straight
 //Trim = -0.3 is a left trim
 //Trim =  0.3 is a right trim
-float trim = 0.0;
+float trim = 0.3;
 
 /* Functions (listed below) contain the code that runs the buggy.
 These functions can be used in the main.cpp file */
@@ -43,35 +43,31 @@
 
 extern void forward(float time) //moves forward for (time) seconds
 {
-    trim = trim*20000;
-    LeftMotor.setServoPulseUs(20000 + trim);
-    RightMotor.setServoPulseUs(20000 + trim);   //set the left and right motor to 20000 (full speed) - the trim offset
+    Lmotor = 1.0 + trim;
+    Rmotor = 1.0 - trim; //set the left and right motor to 1.0 (full speed) - the trim offset
     hold(time); //wait for (time) seconds while the motors are on
     stop(); //stops the motors
 }
 
 extern void left(float time) //moves left for (time) seconds
 {
-    trim = trim*20000;
-    LeftMotor.setServoPulseUs(0);    //set left motor to off
-    RightMotor.setServoPulseUs(20000 - trim);   //set right motor to full speed - trim
+    Rmotor = 1.0 - trim; //set the right motor to full speed
+    Lmotor = 0.0; //set the left motor to off
     hold(time); //waits for (time) seconds
     stop(); //stops the motors
 }
 
 extern void right(float time) //moves right for (time) seconds
 {
-    trim = trim*20000;
-    RightMotor.setServoPulseUs(0);    //set left motor to off
-    LeftMotor.setServoPulseUs(20000 + trim);   //set right motor to full speed + trim
+    Lmotor = 1.0 + trim; //set the left motor to full speed
+    Rmotor = 0.0; //set the right motor to off
     hold(time); //waits for (time) seconds
     stop(); //stops the motors
 }
 
 extern void stop() //stops the motors
 {
-    RightMotor.setServoPulseUs(0);  //turn motors off
-    LeftMotor.setServoPulseUs(0);
+    Lmotor = Rmotor = 0; //set the speed of each motor to 0
 }
 
 #endif // BUGGY_FUNCTIONS_C
\ No newline at end of file
diff -r 370b7f440dcf -r e2fc7f9cbdde main.cpp
--- a/main.cpp	Thu Jun 09 10:27:13 2016 +0000
+++ b/main.cpp	Wed Jun 22 15:07:04 2016 +0000
@@ -41,9 +41,10 @@
 int main() //int main is run automatically. Place your program here
 {
     stop();
-
-    forward(3); //move the buggy forward for 10 seconds
-    right(2);
-    forward(3);
-    left(4);
+    while(1){
+        forward(6); //move the buggy forward for 10 seconds
+        right(8);
+        forward(5);
+        left(2);
+    }
 } 
\ No newline at end of file