Control a dual DC motor powered buggy using the BBC MicroBit

Dependencies:   microbit

Committer:
elijah_ubit
Date:
Wed Jun 22 15:07:04 2016 +0000
Revision:
1:e2fc7f9cbdde
Parent:
0:370b7f440dcf
first

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elijah_ubit 0:370b7f440dcf 1 /*********************************************************
elijah_ubit 0:370b7f440dcf 2 *buggy_functions.h *
elijah_ubit 0:370b7f440dcf 3 *Author: Elijah Orr & Dan Argust *
elijah_ubit 0:370b7f440dcf 4 * *
elijah_ubit 0:370b7f440dcf 5 *A library of functions that can be used to control a *
elijah_ubit 0:370b7f440dcf 6 *buggy using the BBC MicroBit *
elijah_ubit 0:370b7f440dcf 7 *********************************************************/
elijah_ubit 0:370b7f440dcf 8
elijah_ubit 0:370b7f440dcf 9 #ifndef BUGGY_FUNCTIONS_C
elijah_ubit 0:370b7f440dcf 10 #define BUGGY_FUNCTIONS_C
elijah_ubit 0:370b7f440dcf 11
elijah_ubit 0:370b7f440dcf 12 /* necessary includes */
elijah_ubit 0:370b7f440dcf 13 #include "MicroBit.h"
elijah_ubit 0:370b7f440dcf 14 #include "buggy_functions.h"
elijah_ubit 0:370b7f440dcf 15
elijah_ubit 0:370b7f440dcf 16 /*set up pins that will be used to control motors */
elijah_ubit 1:e2fc7f9cbdde 17 PwmOut Lmotor(MICROBIT_PIN_P1);
elijah_ubit 1:e2fc7f9cbdde 18 PwmOut Rmotor(MICROBIT_PIN_P2);
elijah_ubit 0:370b7f440dcf 19
elijah_ubit 0:370b7f440dcf 20 //set up LED matrix display
elijah_ubit 0:370b7f440dcf 21 MicroBitDisplay display;
elijah_ubit 0:370b7f440dcf 22
elijah_ubit 0:370b7f440dcf 23 //Trim is an offset that you can adjust to help the buggy drive straight
elijah_ubit 0:370b7f440dcf 24 //Trim = -0.3 is a left trim
elijah_ubit 0:370b7f440dcf 25 //Trim = 0.3 is a right trim
elijah_ubit 1:e2fc7f9cbdde 26 float trim = 0.3;
elijah_ubit 0:370b7f440dcf 27
elijah_ubit 0:370b7f440dcf 28 /* Functions (listed below) contain the code that runs the buggy.
elijah_ubit 0:370b7f440dcf 29 These functions can be used in the main.cpp file */
elijah_ubit 0:370b7f440dcf 30
elijah_ubit 0:370b7f440dcf 31 extern void hold(float time) //waits for (time) seconds
elijah_ubit 0:370b7f440dcf 32 {
elijah_ubit 0:370b7f440dcf 33 for (float i = time;i>0;i-=0.01){ //code in the loop will execute every 0.01s during the delay
elijah_ubit 0:370b7f440dcf 34 char display_buffer [1]; //buffer to store display string
elijah_ubit 0:370b7f440dcf 35 int time_int = i;
elijah_ubit 0:370b7f440dcf 36 sprintf(display_buffer, "%d", time_int); //format the time as a string
elijah_ubit 0:370b7f440dcf 37 display.enable(); //enable display
elijah_ubit 0:370b7f440dcf 38 display.printAsync(display_buffer); //diplay string
elijah_ubit 0:370b7f440dcf 39 wait(0.01);
elijah_ubit 0:370b7f440dcf 40 display.disable(); //disable display
elijah_ubit 0:370b7f440dcf 41 }
elijah_ubit 0:370b7f440dcf 42 }
elijah_ubit 0:370b7f440dcf 43
elijah_ubit 0:370b7f440dcf 44 extern void forward(float time) //moves forward for (time) seconds
elijah_ubit 0:370b7f440dcf 45 {
elijah_ubit 1:e2fc7f9cbdde 46 Lmotor = 1.0 + trim;
elijah_ubit 1:e2fc7f9cbdde 47 Rmotor = 1.0 - trim; //set the left and right motor to 1.0 (full speed) - the trim offset
elijah_ubit 0:370b7f440dcf 48 hold(time); //wait for (time) seconds while the motors are on
elijah_ubit 0:370b7f440dcf 49 stop(); //stops the motors
elijah_ubit 0:370b7f440dcf 50 }
elijah_ubit 0:370b7f440dcf 51
elijah_ubit 0:370b7f440dcf 52 extern void left(float time) //moves left for (time) seconds
elijah_ubit 0:370b7f440dcf 53 {
elijah_ubit 1:e2fc7f9cbdde 54 Rmotor = 1.0 - trim; //set the right motor to full speed
elijah_ubit 1:e2fc7f9cbdde 55 Lmotor = 0.0; //set the left motor to off
elijah_ubit 0:370b7f440dcf 56 hold(time); //waits for (time) seconds
elijah_ubit 0:370b7f440dcf 57 stop(); //stops the motors
elijah_ubit 0:370b7f440dcf 58 }
elijah_ubit 0:370b7f440dcf 59
elijah_ubit 0:370b7f440dcf 60 extern void right(float time) //moves right for (time) seconds
elijah_ubit 0:370b7f440dcf 61 {
elijah_ubit 1:e2fc7f9cbdde 62 Lmotor = 1.0 + trim; //set the left motor to full speed
elijah_ubit 1:e2fc7f9cbdde 63 Rmotor = 0.0; //set the right motor to off
elijah_ubit 0:370b7f440dcf 64 hold(time); //waits for (time) seconds
elijah_ubit 0:370b7f440dcf 65 stop(); //stops the motors
elijah_ubit 0:370b7f440dcf 66 }
elijah_ubit 0:370b7f440dcf 67
elijah_ubit 0:370b7f440dcf 68 extern void stop() //stops the motors
elijah_ubit 0:370b7f440dcf 69 {
elijah_ubit 1:e2fc7f9cbdde 70 Lmotor = Rmotor = 0; //set the speed of each motor to 0
elijah_ubit 0:370b7f440dcf 71 }
elijah_ubit 0:370b7f440dcf 72
elijah_ubit 0:370b7f440dcf 73 #endif // BUGGY_FUNCTIONS_C