First test programme for the RenBuggy

Dependencies:   SevenSegmentDisplay mbed

Fork of Renbed_Buggy_Basics by Renishaw

Committer:
MiskinPrj
Date:
Thu Apr 21 08:59:08 2016 +0000
Revision:
0:23373ebd6d3a
Child:
1:9f6d495cda75
First save and publish

Who changed what in which revision?

UserRevisionLine numberNew 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 0:23373ebd6d3a 21 //Lmotor = Rmotor = 0;
MiskinPrj 0:23373ebd6d3a 22
MiskinPrj 0:23373ebd6d3a 23 //DigitalIn is used as a button
MiskinPrj 0:23373ebd6d3a 24 DigitalIn CurrentButtonState(p7);
MiskinPrj 0:23373ebd6d3a 25 //This bool is used to store the buttons state (pressed/not pressed)
MiskinPrj 0:23373ebd6d3a 26 bool LastButtonState = 0;
MiskinPrj 0:23373ebd6d3a 27
MiskinPrj 0:23373ebd6d3a 28 //This creates an object called "seg" that controls the seven segment display
MiskinPrj 0:23373ebd6d3a 29 SevenSegmentDisplay seg(0);
MiskinPrj 0:23373ebd6d3a 30
MiskinPrj 0:23373ebd6d3a 31 /* Functions (listed below) contain the code that runs the buggy.
MiskinPrj 0:23373ebd6d3a 32 These functions can be used in the main.cpp file */
MiskinPrj 0:23373ebd6d3a 33
MiskinPrj 0:23373ebd6d3a 34
MiskinPrj 0:23373ebd6d3a 35 extern void hold(float time) //waits for (time) seconds
MiskinPrj 0:23373ebd6d3a 36 {
MiskinPrj 0:23373ebd6d3a 37 seg.DisplayDigits(0,0); //Displays the digits 0 and 0
MiskinPrj 0:23373ebd6d3a 38 for (float i = time;i>0;i-=0.01){ //For every hundreth of a second, display the time remaining
MiskinPrj 0:23373ebd6d3a 39 seg.DisplayDigits((int)i/10,(int)i%10);
MiskinPrj 0:23373ebd6d3a 40 wait(0.01);
MiskinPrj 0:23373ebd6d3a 41 }
MiskinPrj 0:23373ebd6d3a 42 }
MiskinPrj 0:23373ebd6d3a 43
MiskinPrj 0:23373ebd6d3a 44 extern void forward(float time) //moves forward for (time) seconds
MiskinPrj 0:23373ebd6d3a 45 {
MiskinPrj 0:23373ebd6d3a 46 Lmotor = Rmotor = 1.0; //set the left and right motor to 1.0 (full speed)
MiskinPrj 0:23373ebd6d3a 47 hold(time); //wait for (time) seconds while the motors are on
MiskinPrj 0:23373ebd6d3a 48 stop(); //stops the motors
MiskinPrj 0:23373ebd6d3a 49 }
MiskinPrj 0:23373ebd6d3a 50
MiskinPrj 0:23373ebd6d3a 51 extern void left(float time) //moves left for (time) seconds
MiskinPrj 0:23373ebd6d3a 52 {
MiskinPrj 0:23373ebd6d3a 53 Rmotor = 1.0; //set the right motor to full speed
MiskinPrj 0:23373ebd6d3a 54 Lmotor = 0.0; //set the left motor to off
MiskinPrj 0:23373ebd6d3a 55 hold(time); //waits for (time) seconds
MiskinPrj 0:23373ebd6d3a 56 stop(); //stops the motors
MiskinPrj 0:23373ebd6d3a 57 }
MiskinPrj 0:23373ebd6d3a 58
MiskinPrj 0:23373ebd6d3a 59 extern void right(float time) //moves right for (time) seconds
MiskinPrj 0:23373ebd6d3a 60 {
MiskinPrj 0:23373ebd6d3a 61 Lmotor = 1.0; //set the left motor to full speed
MiskinPrj 0:23373ebd6d3a 62 Rmotor = 0.0; //set the right motor to off
MiskinPrj 0:23373ebd6d3a 63 hold(time); //waits for (time) seconds
MiskinPrj 0:23373ebd6d3a 64 stop(); //stops the motors
MiskinPrj 0:23373ebd6d3a 65 }
MiskinPrj 0:23373ebd6d3a 66
MiskinPrj 0:23373ebd6d3a 67 extern void stop() //stops the motors
MiskinPrj 0:23373ebd6d3a 68 {
MiskinPrj 0:23373ebd6d3a 69 Lmotor = Rmotor = 0; //set the speed of each motor to 0
MiskinPrj 0:23373ebd6d3a 70 }
MiskinPrj 0:23373ebd6d3a 71
MiskinPrj 0:23373ebd6d3a 72 extern void readButton(float time) //checks if the button is pressed for (time) seconds
MiskinPrj 0:23373ebd6d3a 73 {
MiskinPrj 0:23373ebd6d3a 74 seg.DisplayDigits(0,0); //displays 0 0 on the seven segment display
MiskinPrj 0:23373ebd6d3a 75 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 76 {
MiskinPrj 0:23373ebd6d3a 77 if (CurrentButtonState == 1 and LastButtonState == 0){
MiskinPrj 0:23373ebd6d3a 78 rollDice(); //rolls the dice if the button is pressed
MiskinPrj 0:23373ebd6d3a 79 }
MiskinPrj 0:23373ebd6d3a 80 LastButtonState = CurrentButtonState;
MiskinPrj 0:23373ebd6d3a 81 wait(0.01);
MiskinPrj 0:23373ebd6d3a 82 }
MiskinPrj 0:23373ebd6d3a 83 }
MiskinPrj 0:23373ebd6d3a 84
MiskinPrj 0:23373ebd6d3a 85 extern int rollDice() //a function that randomly generates a number and displays it on the seven segment display
MiskinPrj 0:23373ebd6d3a 86 {
MiskinPrj 0:23373ebd6d3a 87 int tens; //declare two variables, tens and units to store the number to be displayed
MiskinPrj 0:23373ebd6d3a 88 int units;
MiskinPrj 0:23373ebd6d3a 89 for (int i = 20;i>0;i--){
MiskinPrj 0:23373ebd6d3a 90 tens = rand()%9; //generate a random number from 0-9 and store it in "tens"
MiskinPrj 0:23373ebd6d3a 91 units = rand()%9;
MiskinPrj 0:23373ebd6d3a 92 seg.DisplayDigits(tens,units); //display the numbers stored in tens and units
MiskinPrj 0:23373ebd6d3a 93 wait(0.04);
MiskinPrj 0:23373ebd6d3a 94 }
MiskinPrj 0:23373ebd6d3a 95 return tens*10+units;
MiskinPrj 0:23373ebd6d3a 96 }
MiskinPrj 0:23373ebd6d3a 97 #endif // BUGGY_FUNCTIONS_C