Basic buggy functions with seven segment readout
Dependencies: SevenSegmentDisplay mbed
buggy_functions.cpp
00001 /********************************************************* 00002 *buggy_functions.cpp * 00003 *Author: Elijah Orr & Dan Argust * 00004 * * 00005 *A library of functions that can be used to control the * 00006 *RenBuggy. * 00007 *********************************************************/ 00008 00009 #ifndef BUGGY_FUNCTIONS_C 00010 #define BUGGY_FUNCTIONS_C 00011 00012 /* necessary includes */ 00013 #include "mbed.h" 00014 #include "buggy_functions.h" 00015 #include "SevenSegmentDisplay.h" 00016 00017 // PwmOut is used to control the motors 00018 PwmOut Lmotor(LeftMotorPin); 00019 PwmOut Rmotor(RightMotorPin); 00020 00021 //Trim is an offset that you can adjust to help the buggy drive straight 00022 //Trim = -0.3 is a left trim 00023 //Trim = 0.3 is a right trim 00024 float trim = 0.0; 00025 00026 //DigitalIn is used as a button 00027 DigitalIn CurrentButtonState(p7); 00028 //This bool is used to store the buttons state (pressed/not pressed) 00029 bool LastButtonState = 0; 00030 00031 //This creates an object called "seg" that controls the seven segment display 00032 SevenSegmentDisplay seg(0); 00033 00034 /* Functions (listed below) contain the code that runs the buggy. 00035 These functions can be used in the main.cpp file */ 00036 00037 00038 extern void hold(float time) //waits for (time) seconds 00039 { 00040 seg.DisplayDigits(0,0); //Displays the digits 0 and 0 00041 for (float i = time;i>0;i-=0.01){ //For every hundreth of a second, display the time remaining 00042 seg.DisplayDigits((int)i/10,(int)i%10); 00043 wait(0.01); 00044 } 00045 } 00046 00047 extern void forward(float time) //moves forward for (time) seconds 00048 { 00049 Lmotor = 1.0 + trim; 00050 Rmotor = 1.0 - trim; //set the left and right motor to 1.0 (full speed) - the trim offset 00051 hold(time); //wait for (time) seconds while the motors are on 00052 stop(); //stops the motors 00053 } 00054 00055 extern void left(float time) //moves left for (time) seconds 00056 { 00057 Rmotor = 1.0 - trim; //set the right motor to full speed 00058 Lmotor = 0.0; //set the left motor to off 00059 hold(time); //waits for (time) seconds 00060 stop(); //stops the motors 00061 } 00062 00063 extern void right(float time) //moves right for (time) seconds 00064 { 00065 Lmotor = 1.0 + trim; //set the left motor to full speed 00066 Rmotor = 0.0; //set the right motor to off 00067 hold(time); //waits for (time) seconds 00068 stop(); //stops the motors 00069 } 00070 00071 extern void stop() //stops the motors 00072 { 00073 Lmotor = Rmotor = 0; //set the speed of each motor to 0 00074 } 00075 00076 extern void readButton(float time) //checks if the button is pressed for (time) seconds 00077 { 00078 seg.DisplayDigits(0,0); //displays 0 0 on the seven segment display 00079 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) 00080 { 00081 if (CurrentButtonState == 1 and LastButtonState == 0){ 00082 rollDice(); //rolls the dice if the button is pressed 00083 } 00084 LastButtonState = CurrentButtonState; 00085 wait(0.01); 00086 } 00087 } 00088 00089 extern int rollDice() //a function that randomly generates a number and displays it on the seven segment display 00090 { 00091 int tens; //declare two variables, tens and units to store the number to be displayed 00092 int units; 00093 for (int i = 20;i>0;i--){ 00094 tens = rand()%9; //generate a random number from 0-9 and store it in "tens" 00095 units = rand()%9; 00096 seg.DisplayDigits(tens,units); //display the numbers stored in tens and units 00097 wait(0.04); 00098 } 00099 return tens*10+units; 00100 } 00101 #endif // BUGGY_FUNCTIONS_C
Generated on Mon Jul 18 2022 19:57:55 by
1.7.2