RenBuggyTimed with edited function and access to seven segment display

Dependencies:   SevenSegmentDisplay mbed

Fork of 1-RenBuggyTimed by Ren Buggy

Committer:
DanArgust
Date:
Wed Apr 13 07:04:28 2016 +0000
Revision:
1:91ca3ea0f578
Parent:
0:9870f526ddd1
Release 0.0.1 1-RenBuggyTimed with changes all functions and including access to Seven Segment Display

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RenBuggy 0:9870f526ddd1 1 /*********************************************************
RenBuggy 0:9870f526ddd1 2 *TimedMovement.cpp *
DanArgust 1:91ca3ea0f578 3 *Author: Dan Argust *
RenBuggy 0:9870f526ddd1 4 * *
RenBuggy 0:9870f526ddd1 5 *A library of functions that can be used to control the *
RenBuggy 0:9870f526ddd1 6 *RenBuggy. *
RenBuggy 0:9870f526ddd1 7 *********************************************************/
RenBuggy 0:9870f526ddd1 8
RenBuggy 0:9870f526ddd1 9 #ifndef TIMEDMOVEMENT_C
RenBuggy 0:9870f526ddd1 10 #define TIMEDMOVEMENT_C
RenBuggy 0:9870f526ddd1 11
RenBuggy 0:9870f526ddd1 12 /* necessary includes */
RenBuggy 0:9870f526ddd1 13 #include "mbed.h"
RenBuggy 0:9870f526ddd1 14 #include "TimedMovement.h"
DanArgust 1:91ca3ea0f578 15 #include "SevenSegmentDisplay.h"
RenBuggy 0:9870f526ddd1 16
RenBuggy 0:9870f526ddd1 17 /* PwmOut is a class included in the mbed.h library that allows
RenBuggy 0:9870f526ddd1 18 a pin to be configured as a PWM output. This is used to control
RenBuggy 0:9870f526ddd1 19 the speed of the motors. Lmotor and Rmotor are chosen names for
RenBuggy 0:9870f526ddd1 20 the pins, LeftMotorPin and RightMotorPin (see TimedMovement.h)
RenBuggy 0:9870f526ddd1 21 specify the physical pins to be used. */
RenBuggy 0:9870f526ddd1 22 PwmOut Lmotor(LeftMotorPin);
RenBuggy 0:9870f526ddd1 23 PwmOut Rmotor(RightMotorPin);
RenBuggy 0:9870f526ddd1 24
DanArgust 1:91ca3ea0f578 25 DigitalIn CurrentButtonState(p7);
DanArgust 1:91ca3ea0f578 26 bool LastButtonState = 0;
DanArgust 1:91ca3ea0f578 27
DanArgust 1:91ca3ea0f578 28 SevenSegmentDisplay seg(0);
DanArgust 1:91ca3ea0f578 29
RenBuggy 0:9870f526ddd1 30 /* Function definitions contain the code that will execute when
RenBuggy 0:9870f526ddd1 31 the function is called. These must have the same return type
RenBuggy 0:9870f526ddd1 32 and parameters as the function declarations. */
RenBuggy 0:9870f526ddd1 33
RenBuggy 0:9870f526ddd1 34 /****************************************************************
RenBuggy 0:9870f526ddd1 35 * Function: forward() *
RenBuggy 0:9870f526ddd1 36 * *
RenBuggy 0:9870f526ddd1 37 * Moves the RenBuggy directly forwards *
RenBuggy 0:9870f526ddd1 38 * *
RenBuggy 0:9870f526ddd1 39 * Inputs: A floating point value representing the length of *
RenBuggy 0:9870f526ddd1 40 * time the buggy will move for *
RenBuggy 0:9870f526ddd1 41 * *
RenBuggy 0:9870f526ddd1 42 * Returns: none *
RenBuggy 0:9870f526ddd1 43 ****************************************************************/
DanArgust 1:91ca3ea0f578 44 extern void hold(float time)
DanArgust 1:91ca3ea0f578 45 {
DanArgust 1:91ca3ea0f578 46 seg.DisplayDigits(0,0);
DanArgust 1:91ca3ea0f578 47 for (float i = time;i>0;i-=0.01){
DanArgust 1:91ca3ea0f578 48 seg.DisplayDigits((int)i/10,(int)i%10);
DanArgust 1:91ca3ea0f578 49 //if (CurrentButtonState == 1 and LastButtonState == 0){
DanArgust 1:91ca3ea0f578 50 // rollDice();
DanArgust 1:91ca3ea0f578 51 //}
DanArgust 1:91ca3ea0f578 52 //LastButtonState = CurrentButtonState;
DanArgust 1:91ca3ea0f578 53 wait(0.01);
DanArgust 1:91ca3ea0f578 54 }
DanArgust 1:91ca3ea0f578 55 }
DanArgust 1:91ca3ea0f578 56
RenBuggy 0:9870f526ddd1 57 extern void forward(float time)
RenBuggy 0:9870f526ddd1 58 {
RenBuggy 0:9870f526ddd1 59 /* Lmotor and Rmotor are set to 1.0 (i.e. the motors will
RenBuggy 0:9870f526ddd1 60 operate at full speed). As both motors will move at the
RenBuggy 0:9870f526ddd1 61 same speed, the RenBuggy will go directly forward. */
DanArgust 1:91ca3ea0f578 62 //Lmotor = Rmotor = 1.0;
RenBuggy 0:9870f526ddd1 63 Lmotor = Rmotor = 1.0;
RenBuggy 0:9870f526ddd1 64 /* the program will wait here for the length of time passed
DanArgust 1:91ca3ea0f578 65 to the function before continuing. hold() is used to wait a
DanArgust 1:91ca3ea0f578 66 number of seconds and display them on the seven seg display */
DanArgust 1:91ca3ea0f578 67 hold(time);
DanArgust 1:91ca3ea0f578 68 stop();
RenBuggy 0:9870f526ddd1 69 }
RenBuggy 0:9870f526ddd1 70
RenBuggy 0:9870f526ddd1 71 /****************************************************************
RenBuggy 0:9870f526ddd1 72 * Function: left() *
RenBuggy 0:9870f526ddd1 73 * *
RenBuggy 0:9870f526ddd1 74 * Turns the RenBuggy to the left *
RenBuggy 0:9870f526ddd1 75 * *
RenBuggy 0:9870f526ddd1 76 * Inputs: A floating point value representing the length of *
RenBuggy 0:9870f526ddd1 77 * time the buggy will turn for *
RenBuggy 0:9870f526ddd1 78 * *
RenBuggy 0:9870f526ddd1 79 * Returns: none *
RenBuggy 0:9870f526ddd1 80 ****************************************************************/
RenBuggy 0:9870f526ddd1 81 extern void left(float time)
RenBuggy 0:9870f526ddd1 82 {
RenBuggy 0:9870f526ddd1 83 Rmotor = 1.0;
DanArgust 1:91ca3ea0f578 84 Lmotor = 0.0;
DanArgust 1:91ca3ea0f578 85 hold(time);
DanArgust 1:91ca3ea0f578 86 //readButton(time);
DanArgust 1:91ca3ea0f578 87 stop();
RenBuggy 0:9870f526ddd1 88 }
RenBuggy 0:9870f526ddd1 89
RenBuggy 0:9870f526ddd1 90 /****************************************************************
RenBuggy 0:9870f526ddd1 91 * Function: right() *
RenBuggy 0:9870f526ddd1 92 * *
RenBuggy 0:9870f526ddd1 93 * Turns the RenBuggy to the right *
RenBuggy 0:9870f526ddd1 94 * *
RenBuggy 0:9870f526ddd1 95 * Inputs: A floating point value representing the length of *
RenBuggy 0:9870f526ddd1 96 * time the buggy will turn for *
RenBuggy 0:9870f526ddd1 97 * *
RenBuggy 0:9870f526ddd1 98 * Returns: none *
RenBuggy 0:9870f526ddd1 99 ****************************************************************/
RenBuggy 0:9870f526ddd1 100 extern void right(float time)
RenBuggy 0:9870f526ddd1 101 {
RenBuggy 0:9870f526ddd1 102 Lmotor = 1.0;
DanArgust 1:91ca3ea0f578 103 Rmotor = 0.0;
DanArgust 1:91ca3ea0f578 104 hold(time);
DanArgust 1:91ca3ea0f578 105 stop();
RenBuggy 0:9870f526ddd1 106 }
RenBuggy 0:9870f526ddd1 107
RenBuggy 0:9870f526ddd1 108 /****************************************************************
RenBuggy 0:9870f526ddd1 109 * Function: stop() *
RenBuggy 0:9870f526ddd1 110 * *
RenBuggy 0:9870f526ddd1 111 * Brings the RenBuggy to a complete stop *
RenBuggy 0:9870f526ddd1 112 * *
RenBuggy 0:9870f526ddd1 113 * Inputs: none *
RenBuggy 0:9870f526ddd1 114 * *
RenBuggy 0:9870f526ddd1 115 * Returns: none *
RenBuggy 0:9870f526ddd1 116 ****************************************************************/
RenBuggy 0:9870f526ddd1 117 extern void stop()
RenBuggy 0:9870f526ddd1 118 {
RenBuggy 0:9870f526ddd1 119 Lmotor = Rmotor = 0;
RenBuggy 0:9870f526ddd1 120 }
RenBuggy 0:9870f526ddd1 121
DanArgust 1:91ca3ea0f578 122 extern void readButton(float time)
DanArgust 1:91ca3ea0f578 123 {
DanArgust 1:91ca3ea0f578 124 seg.DisplayDigits(0,0);
DanArgust 1:91ca3ea0f578 125 for (float i = time;i>0;i-=0.01)
DanArgust 1:91ca3ea0f578 126 {
DanArgust 1:91ca3ea0f578 127 if (CurrentButtonState == 1 and LastButtonState == 0){
DanArgust 1:91ca3ea0f578 128 rollDice();
DanArgust 1:91ca3ea0f578 129 //stop();
DanArgust 1:91ca3ea0f578 130 //right(2.0);
DanArgust 1:91ca3ea0f578 131 }
DanArgust 1:91ca3ea0f578 132 LastButtonState = CurrentButtonState;
DanArgust 1:91ca3ea0f578 133 wait(0.01);
DanArgust 1:91ca3ea0f578 134 }
DanArgust 1:91ca3ea0f578 135 }
DanArgust 1:91ca3ea0f578 136
DanArgust 1:91ca3ea0f578 137 extern int rollDice()
DanArgust 1:91ca3ea0f578 138 {
DanArgust 1:91ca3ea0f578 139 int tens;
DanArgust 1:91ca3ea0f578 140 int units;
DanArgust 1:91ca3ea0f578 141 for (int i = 20;i>0;i--){
DanArgust 1:91ca3ea0f578 142 tens = rand()%9;
DanArgust 1:91ca3ea0f578 143 units = rand()%9;
DanArgust 1:91ca3ea0f578 144 seg.DisplayDigits(tens,units);
DanArgust 1:91ca3ea0f578 145 wait(0.04);
DanArgust 1:91ca3ea0f578 146 }
DanArgust 1:91ca3ea0f578 147 return tens*10+units;
DanArgust 1:91ca3ea0f578 148 }
RenBuggy 0:9870f526ddd1 149 #endif // TIMEDMOVEMENT_C