Jakobi Blackburn / Mbed 2 deprecated UltrasonicDistanceSensor-el13jb

Dependencies:   N5110WN PowerControl SRF02 mbed

main.cpp

Committer:
JakBlackburn
Date:
2015-03-20
Revision:
2:a1eaa4d74b63
Parent:
1:7f151ab172cb
Child:
3:00933efbe463

File content as of revision 2:a1eaa4d74b63:

/**
@file main.cpp

@brief Program implementation

*/
#include "main.h"



int main() {
    pc.baud(9600);  //sets baud rate
    timer.attach(&timerExpired,0.5);
    LogTog.rise(&logButtonPressed);
    UnitTog.rise(&unitToggle);
    pc.attach(&serialISR); // attach serial ISR
    
    char buffer[30]; // buffer used to store time string
    set_time(1427846400); // initialise time to 1st April 2015 00:00:00
    
    BLED.period(0.02);  //sets the frequency of the Backlight at 50Hz
    lcd.init(); //to initilize the display
    checkerBoard();
    wait(0.2);
    lcd.clear();
    
    introTune(); // plays the intro tune
    lcd.printString("Parking",20,0);
    lcd.printString("Sensor",6,1);
    lcd.printString("by",35,2);
    lcd.printString("Jakobi",24,3);
    lcd.printString("Blackburn",16,4);
    wait(0.2);
    lcd.clear();
    while(1) {
        if(timerFlag){
            
            timerFlag = 0; //resets flag
            time_t seconds = time(NULL); // get current time
            // format time into a string (time and date)
            strftime(buffer, 30 , "%d/%m/%y %R", localtime(&seconds));
            float dist = getDistance(); //reads the distance and sets it to a float value.
            //prints the value to the serial port.
            if(state==1){pc.printf("%s , Distance = %.2f %s \n",buffer,dist*unitX,units);}//prints the value to the serial port.
            else if(state !=1){pc.printf("%s , Distance = %.0f %s \n",buffer,dist*unitX,units);}//prints the value to the serial port.
           
            leds=15;
            logging(buffer,dist);
            leds=0;
         }
          if (setTimeFlag) { // if updated time has been sent
            setTimeFlag = 0; // clear flag
            setTime(); // update time
        }
        Sleep();
    }
}

void timerExpired(){
    timerFlag=1;   
}


void warnings(){
    WLED=1;
    volume= BuzVol.read();
    Buzzer.PlayNote(800.0, 0.1, volume);
    WLED=0;   
}
float getDistance(){
    int dist0 = sensor.getDistanceCm();
    wait(0.1*scanSpeed);
    int dist1 = sensor.getDistanceCm();
    wait(0.1*scanSpeed);
    int dist2 = sensor.getDistanceCm();
    wait(0.1*scanSpeed);
    int dist3 = sensor.getDistanceCm();
    wait(0.1*scanSpeed);
    int dist4 = sensor.getDistanceCm();
    wait(0.1*scanSpeed);
    int dist5 = sensor.getDistanceCm();
    wait(0.1*scanSpeed);
    int dist6 = sensor.getDistanceCm();
    wait(0.1*scanSpeed);
    int dist7 = sensor.getDistanceCm();
    wait(0.1*scanSpeed);
    int dist8 = sensor.getDistanceCm();
    wait(0.1*scanSpeed);
    int dist9 = sensor.getDistanceCm();
    distance=(dist0+dist1+dist2+dist3+dist4+dist5+dist6+dist7+dist8+dist9)/10; 
    setScanSpeed();
    warnings();
    return distance;
}

void unitToggle(){
    state = fsm[state].nextState[UnitTog]; // read input and update current state
    unitX = fsm[state].unitMultiple; // set output depending on current state
    units= fsm[state].Unit; // 
    wait(0.2);
    pc.printf("unit multiple = %.2f \n",unitX);
    pc.printf("Units = %s \n",units);
       
}

void setScanSpeed(){
    if(distance>=100){
        scanSpeed=2; // sets the speed to normal    
    }
    else if(distance>=25){
        scanSpeed=1; // double speed        
    }
    else if(distance<25){
        scanSpeed=0.25; // quad speed 
    }
    else error(1);
    
    pc.printf("Scan Speed = %.2f/times a second \n\n",(1/scanSpeed));
}
void error(int E){
    while(1){
        leds=0;
        wait(0.2);
        leds=E;
        wait(0.2);
    }
}

void logButtonPressed()
{
    logButtonFlag = !logButtonFlag; //flips the flag
    if(logButtonFlag) {
        LLED=1; // Logging LED will be on if the device is logging
        pc.printf("-------\n Data Logging\n\r");
    } else {
        LLED=0; // Logging LED will be off if the device isn't logging
        pc.printf("-------\n Stopped Logging Data\n\r");
    }
    wait(0.2);
}

void logging(char* data,float data1)
{
    if(logButtonFlag) {
        FILE *fp = fopen("/local/log.csv", "a"); // open 'log.csv' for appending
        // if the file doesn't exist it is created, if it exists, data is appended to the end
        fprintf(fp," %s , %.2f\n ",data,data1); // print string to file
        fclose(fp); // close file
        pc.printf("Data Logged\n\r");
        LLED=1; //keeps the led on if it is logging
    } else {
        pc.printf("WARNING : Unlogged Data\n\r");
        LLED=0;
    }
}

void setTime()
{
// print time for debugging
    pc.printf("set time - %s",rxString);
// atoi() converts a string to an integer
    int time = atoi(rxString);
// update the time
    set_time(time);
     pc.printf("set time - %d",time);
}
void serialISR()
{
// when a serial interrupt occurs, read rx string into buffer
    pc.gets(rxString,16);
// set flag
    setTimeFlag = 1;
}

void introTune()
{
    Buzzer.PlayNote(1319.0,0.084,1.0);
    wait(0.01);
    Buzzer.PlayNote(1319.0,0.084,1.0);
    wait(0.094);
    Buzzer.PlayNote(1319.0,0.084,1.0);
    wait(0.094);
    Buzzer.PlayNote(1047.0,0.084,1.0);
    wait(0.01);
    Buzzer.PlayNote(1319.0,0.084,1.0);
    wait(0.094);
    Buzzer.PlayNote(1568.0,0.084,1.0);
    wait(0.26);
    Buzzer.PlayNote(784.0,0.084,1.0);
}

void checkerBoard() // creates a checked pattern on the lcd
{
    for(int i=0; i<84; i+=2) {
        for(int j=0; j<48; j+=2) {
            lcd.setPixel(i,j);
        }
    }
    lcd.refresh();
}