First test programme for the RenBuggy
Dependencies: SevenSegmentDisplay mbed
Fork of Renbed_Buggy_Basics by
buggy_functions.cpp@3:152595d33544, 2016-04-21 (annotated)
- Committer:
- MiskinPrj
- Date:
- Thu Apr 21 14:42:35 2016 +0000
- Revision:
- 3:152595d33544
- Parent:
- 2:10c115fb71e2
stop and hold at start of main
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MiskinPrj | 0:23373ebd6d3a | 1 | /********************************************************* |
MiskinPrj | 0:23373ebd6d3a | 2 | *buggy_functions.cpp * |
MiskinPrj | 0:23373ebd6d3a | 3 | *Author: Elijah Orr & Dan Argust * |
MiskinPrj | 0:23373ebd6d3a | 4 | * * |
MiskinPrj | 0:23373ebd6d3a | 5 | *A library of functions that can be used to control the * |
MiskinPrj | 0:23373ebd6d3a | 6 | *RenBuggy. * |
MiskinPrj | 0:23373ebd6d3a | 7 | *********************************************************/ |
MiskinPrj | 0:23373ebd6d3a | 8 | |
MiskinPrj | 0:23373ebd6d3a | 9 | #ifndef BUGGY_FUNCTIONS_C |
MiskinPrj | 0:23373ebd6d3a | 10 | #define BUGGY_FUNCTIONS_C |
MiskinPrj | 0:23373ebd6d3a | 11 | |
MiskinPrj | 0:23373ebd6d3a | 12 | /* necessary includes */ |
MiskinPrj | 0:23373ebd6d3a | 13 | #include "mbed.h" |
MiskinPrj | 0:23373ebd6d3a | 14 | #include "buggy_functions.h" |
MiskinPrj | 0:23373ebd6d3a | 15 | #include "SevenSegmentDisplay.h" |
MiskinPrj | 0:23373ebd6d3a | 16 | |
MiskinPrj | 0:23373ebd6d3a | 17 | // PwmOut is used to control the motors |
MiskinPrj | 0:23373ebd6d3a | 18 | PwmOut Lmotor(LeftMotorPin); |
MiskinPrj | 0:23373ebd6d3a | 19 | PwmOut Rmotor(RightMotorPin); |
MiskinPrj | 0:23373ebd6d3a | 20 | |
MiskinPrj | 1:9f6d495cda75 | 21 | //Trim is an offset that you can adjust to help the buggy drive straight |
MiskinPrj | 1:9f6d495cda75 | 22 | //Trim = -0.3 is a left trim |
MiskinPrj | 1:9f6d495cda75 | 23 | //Trim = 0.3 is a right trim |
MiskinPrj | 2:10c115fb71e2 | 24 | float trim = 0.0; |
MiskinPrj | 0:23373ebd6d3a | 25 | |
MiskinPrj | 0:23373ebd6d3a | 26 | //DigitalIn is used as a button |
MiskinPrj | 0:23373ebd6d3a | 27 | DigitalIn CurrentButtonState(p7); |
MiskinPrj | 0:23373ebd6d3a | 28 | //This bool is used to store the buttons state (pressed/not pressed) |
MiskinPrj | 0:23373ebd6d3a | 29 | bool LastButtonState = 0; |
MiskinPrj | 0:23373ebd6d3a | 30 | |
MiskinPrj | 0:23373ebd6d3a | 31 | //This creates an object called "seg" that controls the seven segment display |
MiskinPrj | 0:23373ebd6d3a | 32 | SevenSegmentDisplay seg(0); |
MiskinPrj | 0:23373ebd6d3a | 33 | |
MiskinPrj | 0:23373ebd6d3a | 34 | /* Functions (listed below) contain the code that runs the buggy. |
MiskinPrj | 0:23373ebd6d3a | 35 | These functions can be used in the main.cpp file */ |
MiskinPrj | 0:23373ebd6d3a | 36 | |
MiskinPrj | 0:23373ebd6d3a | 37 | |
MiskinPrj | 0:23373ebd6d3a | 38 | extern void hold(float time) //waits for (time) seconds |
MiskinPrj | 0:23373ebd6d3a | 39 | { |
MiskinPrj | 0:23373ebd6d3a | 40 | seg.DisplayDigits(0,0); //Displays the digits 0 and 0 |
MiskinPrj | 0:23373ebd6d3a | 41 | for (float i = time;i>0;i-=0.01){ //For every hundreth of a second, display the time remaining |
MiskinPrj | 0:23373ebd6d3a | 42 | seg.DisplayDigits((int)i/10,(int)i%10); |
MiskinPrj | 0:23373ebd6d3a | 43 | wait(0.01); |
MiskinPrj | 0:23373ebd6d3a | 44 | } |
MiskinPrj | 0:23373ebd6d3a | 45 | } |
MiskinPrj | 0:23373ebd6d3a | 46 | |
MiskinPrj | 0:23373ebd6d3a | 47 | extern void forward(float time) //moves forward for (time) seconds |
MiskinPrj | 0:23373ebd6d3a | 48 | { |
MiskinPrj | 1:9f6d495cda75 | 49 | Lmotor = 1.0 + trim; |
MiskinPrj | 1:9f6d495cda75 | 50 | Rmotor = 1.0 - trim; //set the left and right motor to 1.0 (full speed) - the trim offset |
MiskinPrj | 0:23373ebd6d3a | 51 | hold(time); //wait for (time) seconds while the motors are on |
MiskinPrj | 0:23373ebd6d3a | 52 | stop(); //stops the motors |
MiskinPrj | 0:23373ebd6d3a | 53 | } |
MiskinPrj | 0:23373ebd6d3a | 54 | |
MiskinPrj | 0:23373ebd6d3a | 55 | extern void left(float time) //moves left for (time) seconds |
MiskinPrj | 0:23373ebd6d3a | 56 | { |
MiskinPrj | 2:10c115fb71e2 | 57 | Rmotor = 1.0 - trim; //set the right motor to full speed |
MiskinPrj | 0:23373ebd6d3a | 58 | Lmotor = 0.0; //set the left motor to off |
MiskinPrj | 0:23373ebd6d3a | 59 | hold(time); //waits for (time) seconds |
MiskinPrj | 0:23373ebd6d3a | 60 | stop(); //stops the motors |
MiskinPrj | 0:23373ebd6d3a | 61 | } |
MiskinPrj | 0:23373ebd6d3a | 62 | |
MiskinPrj | 0:23373ebd6d3a | 63 | extern void right(float time) //moves right for (time) seconds |
MiskinPrj | 0:23373ebd6d3a | 64 | { |
MiskinPrj | 2:10c115fb71e2 | 65 | Lmotor = 1.0 + trim; //set the left motor to full speed |
MiskinPrj | 0:23373ebd6d3a | 66 | Rmotor = 0.0; //set the right motor to off |
MiskinPrj | 0:23373ebd6d3a | 67 | hold(time); //waits for (time) seconds |
MiskinPrj | 0:23373ebd6d3a | 68 | stop(); //stops the motors |
MiskinPrj | 0:23373ebd6d3a | 69 | } |
MiskinPrj | 0:23373ebd6d3a | 70 | |
MiskinPrj | 0:23373ebd6d3a | 71 | extern void stop() //stops the motors |
MiskinPrj | 0:23373ebd6d3a | 72 | { |
MiskinPrj | 0:23373ebd6d3a | 73 | Lmotor = Rmotor = 0; //set the speed of each motor to 0 |
MiskinPrj | 0:23373ebd6d3a | 74 | } |
MiskinPrj | 0:23373ebd6d3a | 75 | |
MiskinPrj | 0:23373ebd6d3a | 76 | extern void readButton(float time) //checks if the button is pressed for (time) seconds |
MiskinPrj | 0:23373ebd6d3a | 77 | { |
MiskinPrj | 0:23373ebd6d3a | 78 | seg.DisplayDigits(0,0); //displays 0 0 on the seven segment display |
MiskinPrj | 0:23373ebd6d3a | 79 | for (float i = time;i>0;i-=0.01) //for each hundreth of a seconds check if the button state goes from low to high (eg. pressed) |
MiskinPrj | 0:23373ebd6d3a | 80 | { |
MiskinPrj | 0:23373ebd6d3a | 81 | if (CurrentButtonState == 1 and LastButtonState == 0){ |
MiskinPrj | 0:23373ebd6d3a | 82 | rollDice(); //rolls the dice if the button is pressed |
MiskinPrj | 0:23373ebd6d3a | 83 | } |
MiskinPrj | 0:23373ebd6d3a | 84 | LastButtonState = CurrentButtonState; |
MiskinPrj | 0:23373ebd6d3a | 85 | wait(0.01); |
MiskinPrj | 0:23373ebd6d3a | 86 | } |
MiskinPrj | 0:23373ebd6d3a | 87 | } |
MiskinPrj | 0:23373ebd6d3a | 88 | |
MiskinPrj | 0:23373ebd6d3a | 89 | extern int rollDice() //a function that randomly generates a number and displays it on the seven segment display |
MiskinPrj | 0:23373ebd6d3a | 90 | { |
MiskinPrj | 0:23373ebd6d3a | 91 | int tens; //declare two variables, tens and units to store the number to be displayed |
MiskinPrj | 0:23373ebd6d3a | 92 | int units; |
MiskinPrj | 0:23373ebd6d3a | 93 | for (int i = 20;i>0;i--){ |
MiskinPrj | 0:23373ebd6d3a | 94 | tens = rand()%9; //generate a random number from 0-9 and store it in "tens" |
MiskinPrj | 0:23373ebd6d3a | 95 | units = rand()%9; |
MiskinPrj | 0:23373ebd6d3a | 96 | seg.DisplayDigits(tens,units); //display the numbers stored in tens and units |
MiskinPrj | 0:23373ebd6d3a | 97 | wait(0.04); |
MiskinPrj | 0:23373ebd6d3a | 98 | } |
MiskinPrj | 0:23373ebd6d3a | 99 | return tens*10+units; |
MiskinPrj | 0:23373ebd6d3a | 100 | } |
MiskinPrj | 0:23373ebd6d3a | 101 | #endif // BUGGY_FUNCTIONS_C |