RenBuggyTimed with edited function and access to seven segment display

Dependencies:   SevenSegmentDisplay mbed

Fork of 1-RenBuggyTimed by Ren Buggy

Revision:
1:91ca3ea0f578
Parent:
0:9870f526ddd1
--- a/TimedMovement.cpp	Fri Mar 11 10:36:38 2016 +0000
+++ b/TimedMovement.cpp	Wed Apr 13 07:04:28 2016 +0000
@@ -1,6 +1,6 @@
 /*********************************************************
 *TimedMovement.cpp                                       *
-*Author: Elijah Orr                                      *
+*Author: Dan Argust                                      *
 *                                                        *  
 *A library of functions that can be used to control the  * 
 *RenBuggy.                                               *
@@ -12,6 +12,7 @@
 /* necessary includes */
 #include "mbed.h"
 #include "TimedMovement.h"
+#include "SevenSegmentDisplay.h"
 
 /* PwmOut is a class included in the mbed.h library that allows
 a pin to be configured as a PWM output. This is used to control
@@ -21,6 +22,11 @@
 PwmOut Lmotor(LeftMotorPin);
 PwmOut Rmotor(RightMotorPin);
 
+DigitalIn CurrentButtonState(p7);
+bool LastButtonState = 0;
+
+SevenSegmentDisplay seg(0);
+
 /* Function definitions contain the code that will execute when 
 the function is called. These must have the same return type
 and parameters as the function declarations. */
@@ -35,16 +41,31 @@
 *                                                               *
 * Returns: none                                                 *
 ****************************************************************/
+extern void hold(float time)
+{
+    seg.DisplayDigits(0,0);
+    for (float i = time;i>0;i-=0.01){
+        seg.DisplayDigits((int)i/10,(int)i%10);
+        //if (CurrentButtonState == 1 and LastButtonState == 0){
+        //    rollDice();
+        //}
+        //LastButtonState = CurrentButtonState;
+        wait(0.01);
+    }
+}
+
 extern void forward(float time)
 {
     /* Lmotor and Rmotor are set to 1.0 (i.e. the motors will 
     operate at full speed). As both motors will move at the 
     same speed, the RenBuggy will go directly forward. */
+    //Lmotor = Rmotor = 1.0;
     Lmotor = Rmotor = 1.0;
     /* the program will wait here for the length of time passed 
-    to the function before continuing. wait() is a function 
-    provided from mbed.h; a loop could be used instead. */
-    wait(time);
+    to the function before continuing. hold() is used to wait a
+    number of seconds and display them on the seven seg display */
+    hold(time);
+    stop();
 }
 
 /****************************************************************
@@ -60,8 +81,10 @@
 extern void left(float time)
 {
     Rmotor = 1.0;
-    Lmotor = 0.2;
-    wait(time);
+    Lmotor = 0.0;
+    hold(time);
+    //readButton(time);
+    stop();
 }
 
 /****************************************************************
@@ -77,8 +100,9 @@
 extern void right(float time)
 {
     Lmotor = 1.0;
-    Rmotor = 0.2;
-    wait(time);
+    Rmotor = 0.0;
+    hold(time);
+    stop();
 }
 
 /****************************************************************
@@ -95,4 +119,31 @@
     Lmotor = Rmotor = 0;
 }
 
+extern void readButton(float time)
+{
+    seg.DisplayDigits(0,0);
+    for (float i = time;i>0;i-=0.01)
+    {
+        if (CurrentButtonState == 1 and LastButtonState == 0){
+            rollDice();
+            //stop();
+            //right(2.0);
+        }
+        LastButtonState = CurrentButtonState;
+        wait(0.01);
+    }
+}
+
+extern int rollDice()
+{
+    int tens;
+    int units;
+    for (int i = 20;i>0;i--){
+        tens = rand()%9;
+        units = rand()%9;
+        seg.DisplayDigits(tens,units);
+        wait(0.04);
+    }
+    return tens*10+units;
+}
 #endif // TIMEDMOVEMENT_C
\ No newline at end of file