Basic RenBuggy program - Start here with RenBuggy

Dependencies:   SevenSegmentDisplay mbed

Fork of Renbed_Buggy_Basics by Miskin Project

Files at this revision

API Documentation at this revision

Comitter:
MiskinPrj
Date:
Thu Apr 21 08:59:08 2016 +0000
Child:
1:9f6d495cda75
Commit message:
First save and publish

Changed in this revision

SevenSegmentDisplay.lib Show annotated file Show diff for this revision Revisions of this file
buggy_functions.cpp Show annotated file Show diff for this revision Revisions of this file
buggy_functions.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SevenSegmentDisplay.lib	Thu Apr 21 08:59:08 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/jf1452/code/SevenSegmentDisplay/#cb7339a2e196
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buggy_functions.cpp	Thu Apr 21 08:59:08 2016 +0000
@@ -0,0 +1,97 @@
+/*********************************************************
+*buggy_functions.cpp                                     *
+*Author: Elijah Orr & Dan Argust                         *
+*                                                        *
+*A library of functions that can be used to control the  *
+*RenBuggy.                                               *
+*********************************************************/
+
+#ifndef BUGGY_FUNCTIONS_C
+#define BUGGY_FUNCTIONS_C
+
+/* necessary includes */
+#include "mbed.h"
+#include "buggy_functions.h"
+#include "SevenSegmentDisplay.h"
+
+// PwmOut is used to control the motors
+PwmOut Lmotor(LeftMotorPin);
+PwmOut Rmotor(RightMotorPin);
+
+//Lmotor = Rmotor = 0;
+
+//DigitalIn is used as a button
+DigitalIn CurrentButtonState(p7);
+//This bool is used to store the buttons state (pressed/not pressed)
+bool LastButtonState = 0;
+
+//This creates an object called "seg" that controls the seven segment display
+SevenSegmentDisplay seg(0);
+
+/* Functions (listed below) contain the code that runs the buggy.
+These functions can be used in the main.cpp file */
+
+
+extern void hold(float time) //waits for (time) seconds
+{
+    seg.DisplayDigits(0,0); //Displays the digits 0 and 0
+    for (float i = time;i>0;i-=0.01){ //For every hundreth of a second, display the time remaining
+        seg.DisplayDigits((int)i/10,(int)i%10);
+        wait(0.01);
+    }
+}
+
+extern void forward(float time) //moves forward for (time) seconds
+{
+    Lmotor = Rmotor = 1.0; //set the left and right motor to 1.0 (full speed)
+    hold(time); //wait for (time) seconds while the motors are on
+    stop(); //stops the motors
+}
+
+extern void left(float time) //moves left for (time) seconds
+{
+    Rmotor = 1.0; //set the right motor to full speed
+    Lmotor = 0.0; //set the left motor to off
+    hold(time); //waits for (time) seconds
+    stop(); //stops the motors
+}
+
+extern void right(float time) //moves right for (time) seconds
+{
+    Lmotor = 1.0; //set the left motor to full speed
+    Rmotor = 0.0; //set the right motor to off
+    hold(time); //waits for (time) seconds
+    stop(); //stops the motors
+}
+
+extern void stop() //stops the motors
+{
+    Lmotor = Rmotor = 0; //set the speed of each motor to 0
+}
+
+extern void readButton(float time) //checks if the button is pressed for (time) seconds
+{
+    seg.DisplayDigits(0,0); //displays 0 0 on the seven segment display
+    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)
+    {
+        if (CurrentButtonState == 1 and LastButtonState == 0){
+            rollDice(); //rolls the dice if the button is pressed
+        }
+        LastButtonState = CurrentButtonState;
+        wait(0.01);
+    }
+}
+
+extern int rollDice() //a function that randomly generates a number and displays it on the seven segment display
+{
+    int tens; //declare two variables, tens and units to store the number to be displayed
+    int units;
+    for (int i = 20;i>0;i--){
+        tens = rand()%9; //generate a random number from 0-9 and store it in "tens"
+        units = rand()%9;
+        seg.DisplayDigits(tens,units); //display the numbers stored in tens and units
+        wait(0.04);
+    }
+    return tens*10+units;
+}
+#endif // BUGGY_FUNCTIONS_C
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/buggy_functions.h	Thu Apr 21 08:59:08 2016 +0000
@@ -0,0 +1,37 @@
+/*********************************************************
+*buggy_functions.h                                       *
+*Author: Elijah Orr & Dan Argust                         *
+*                                                        *
+*A library of functions that can be used to control the  *
+*RenBuggy.                                               *
+*********************************************************/
+
+/* include guards are used to prevent problems caused by 
+multiple definitions */
+#ifndef BUGGY_FUNCTIONS_H
+#define BUGGY_FUNCTIONS_H
+
+/* mbed.h must be included in this file also */
+#include "mbed.h"
+
+/* #define LeftMotorPin p5 tells the preprocessor to replace
+any mention of LeftMotorPin with p5 etc. */
+#define LeftMotorPin p5
+#define RightMotorPin p6
+
+/* these are function prototypes that declare all the functions
+in the library.*/
+extern void forward(float); //Move the buggy forward for (float) seconds
+extern void left(float); //Turn left for (float) seconds
+extern void right(float); //Turn right for (float) seconds
+extern void hold(float); //Hold the buggy in an idle state for (float) seconds
+extern void stop(); //Stop all motors
+
+extern void readButton(float); //Similar to hold, but scans for button presses while waiting
+extern int rollDice(); //Randomly generate a number a display it on the seven segment display
+
+/* stop() and rollDice() do not need to be giving a float
+to be run unlike the other functions which use a (float)
+to know how long they should run for */
+
+#endif // BUGGY_FUNCTIONS_H
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Apr 21 08:59:08 2016 +0000
@@ -0,0 +1,30 @@
+/*********************************************************
+*Ren_Buggy_Basics                                        *
+*Author: Elijah Orr & Dan Argust                         *
+*                                                        *  
+*This program demonstates use of a library of functions  *
+*(buggy_functions) to control the movement of the        *
+*RenBuggy.                                               *
+*********************************************************/
+
+#include "mbed.h" //"mbed.h" is a library that makes it easier to program microcontrollers
+
+#include "buggy_functions.h" //"buggy_functions.h" contains the functions that we will use to move the buggy
+
+/* The basic functions available are:
+*    
+*    forward(time);
+*    left(time);
+*    right(time);
+*    hold(time);
+*
+* see buggy_functions.h for a full list.
+*/
+
+
+int main() //int main is run automatically. Place your program here
+{
+    forward(5.5); //move the buggy forward for 5.5 seconds
+    hold(5); //wait for 16 seconds
+    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Apr 21 08:59:08 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/082adc85693f
\ No newline at end of file