First test programme for the RenBuggy

Dependencies:   SevenSegmentDisplay mbed

Fork of Renbed_Buggy_Basics by Renishaw

buggy_functions.cpp

Committer:
MiskinPrj
Date:
2016-04-21
Revision:
0:23373ebd6d3a
Child:
1:9f6d495cda75

File content as of revision 0:23373ebd6d3a:

/*********************************************************
*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