ultrasonic distance sensor - time headerfile

Dependencies:   N5110 PowerControl SRF02 beep mbed

Fork of Ultrasonicdistancesensor by I Adam

ultrasonic.cpp

Committer:
probrania7
Date:
2015-05-05
Revision:
0:7a82ad48df33
Child:
1:920749af1300

File content as of revision 0:7a82ad48df33:

#include "mbed.h"
#include "N5110.h"
#include "beep.h"
#include "SRF02.h"
#include "timeMgr.h"


//    VCC,SCE,RST,D/C,MOSI,SCLK,LED
N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
// Can also power (VCC) directly from VOUT (3.3 V) -
// Can give better performance due to current limitation from GPIO pin

//          SDA, SCL    
SRF02 sensor(p28,p27);

Beep beep1(p21);
AnalogIn potIn (p20);

 DigitalOut Led1 (p24);
 DigitalOut Led2 (p14);
 DigitalOut Led3 (p25);
 DigitalOut Led4 (p23);   
//=========================
InterruptIn button1 (p17);
InterruptIn button2 (p29);
InterruptIn button3 (p30);


//===============================
int button1Flag = 0;
int button2Flag = 0;
int button3Flag = 0; 
int msgFlag = 1;

//function declarations
//=========================
void introMessage();
void endMessage();
void errorHandler();
void menu();
void demo();
void cursorSelect(int, int, int, int);
void cursorMove(int);
void beeper(float, float);
void introMessageTimer();
void menuLevel(float);
void startMessage();
void displayClock();
//====================================
void distMeasure();
void saveData();
void adjustBrightness(float);
void systemTest();
//------------------------
void button1Pressed();
void button2Pressed();
void button3Pressed();
//Global variables initialisation
//=================================
//Timer introTimer;

int menuLevelFlag = 0;
int brightFlag = 0;
int distFlag = 0;
int clockFlag = 0;
//------------------------------


int main()
{
    // first need to initialise display
    lcd.init();
   
   // button2.rise(&button2Pressed);
   
    beeper(1000, 0.5); 
 
    serial.attach(&serialISR); // attach serial ISR
    
    set_time(1424535031); // initialise time to 1st January 1970
    
    
    introMessage();
    
    button1.rise(&button1Pressed); 
    button3.rise(&button3Pressed);
//-----------------------------------------------------
   
//-----------------------------------------------------
     
    while(1) 
    { 
        if (msgFlag)
        {
            startMessage(); 
        }  

        if (button1Flag)
        {       
            msgFlag=0;
            menu(); 
            menuLevelFlag = 1; 
        } 
        
        if (menuLevelFlag)
        {
            button1Flag =0;
            menuLevel(potIn);
        }
        
        if (distFlag)
        {
            menuLevelFlag = 0;
            distMeasure();
            if(button1)
            {
                distFlag = 0;         
            } 
                   
        } 
        
       if (brightFlag)
        {       
            menuLevelFlag = 0;
            adjustBrightness(potIn); 
            if (button1)
            {
                brightFlag = 0;  
            }    
        }
        if (clockFlag)
        {
            menuLevelFlag = 0;
            displayClock();
            if (button1)
            {
                clockFlag = 0;   
            }       
        }        
     }   
}

void introMessage()
    {
        //introducto text a
        lcd.drawLine(4, 0, 79, 0, 1);
        lcd.printString("Ultrasonic",13,1);
        lcd.printString("Distance",18,2);
        lcd.printString("Sensor",23,3);
        lcd.drawLine(4, 45, 79, 45, 1);
        
        wait(1.3);
        lcd.clear();
        
        //introductory text b
        lcd.drawLine(4, 0, 79, 0, 1);
        lcd.printString("University",13,1);
        lcd.printString("Of",35,2);
        lcd.printString("Leeds",26,3);
        lcd.drawLine(4, 45, 79, 45, 1);
        
        wait(1.3);
        lcd.clear();
        
        //introductory text c
        lcd.drawLine(4, 0, 79, 0, 1);
        lcd.printString("Ibrahim Adam",5,1);
        lcd.printString("200789110",10,2);
        lcd.printString("Yr2 - 2015",8,3);
        lcd.drawLine(4, 45, 79, 45, 1);
        
        wait(1.3);
        lcd.clear();
        
        //introductory text c
        lcd.drawLine(4, 0, 79, 0, 1);
        lcd.printString("Elec 2645",15,1);
        lcd.printString("Embedded SYS",8,2);
        lcd.printString("Project",21,3);
        lcd.drawLine(4, 45, 79, 45, 1);
        
        msgFlag = 1;
        
        wait(1.3);
        lcd.clear();
    }
    
    void mainMenu()
    {
           
    }    

    void endMessage()
    {
        //end text
        lcd.drawLine(4, 0, 79, 0, 1);
        lcd.printString("Thank You",12,1);
        lcd.printString("Goodbye",19,2);
        
        lcd.drawLine(4, 45, 79, 45, 1);
        
        wait(1);
        lcd.clear();   
    }    
    
    void errorHandler()
    {
        //end text
        lcd.drawLine(4, 0, 79, 0, 1);
        lcd.printString("Error Help",12,1);
      
        
        wait(0.2);
        lcd.clear();   
    }    
    
    void adjustBrightness(float x)
    {
        if ((x >= 0) && (x < 0.2))
        {   
            lcd.setBrightness(0);
            lcd.printString("Light 0%",13,1);    
        }
        
        if ((x >= 0.2) && (x < 0.4))
        {
            lcd.setBrightness(0.25);
            lcd.printString("Light 25%",13,1);
        }
        
        if ((x >= 0.4) && (x < 0.6))
        {
            lcd.setBrightness(0.5);
            lcd.printString("Light 50%",13,1);  
        }
        
        if ((x >= 0.6) && (x < 0.8))
        {
            lcd.setBrightness(0.75);
            lcd.printString("Light 75%",13,1); 
               
        }
        if (x >= 0.8) 
        {
            lcd.setBrightness(1);
            lcd.printString("Light 100%",13,1);   
        }
        lcd.clear();    
    }   
    
void menu()
{       
    lcd.clear();             
    lcd.printString("-MAIN MENU-",9,0);
    lcd.drawLine(4, 7, 79, 7, 1);
    lcd.printString("Dist read",15,1);
    lcd.printString("Save data",15,2);
    lcd.printString("brightness",15,3);
    lcd.printString("SYS test",15,4);                               
}  

void cursorSelect(int xDimm, int xLimm, int yDimm, int yLimm)
{
    int i,j;
             
    //clear selecton column
    for (i = 1; i < 6; i++)
    {
        for(j = 1; j < 46; j++)
        {
            lcd.clearPixel(i,j);
        }   
    }    
    
    //particular selection
    for (i = xDimm; i < xLimm; i++)
    {
        for(j = yDimm; j < yLimm; j++)
        {
            lcd.setPixel(i,j);   
        }       
    } 
    lcd.refresh();          
}         

void demo()
{
    // these are default settings so not strictly needed
    lcd.normalMode();      // normal colour mode
        lcd.setBrightness(0.5); // put LED backlight on 50%

        // can directly print strings at specified co-ordinates
        lcd.printString("Hello, World!",0,0);

        char buffer[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
        // so can display a string of a maximum 14 characters in length
        // or create formatted strings - ensure they aren't more than 14 characters long
        int temperature = 27;
        int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer
        // it is important the format specifier ensures the length will fit in the buffer
        if (length <= 14)  // if string will fit on display
            lcd.printString(buffer,0,1);           // display on screen

        float pressure = 1012.3;  // same idea with floats
        length = sprintf(buffer,"P = %.2f mb",pressure);
        if (length <= 14)
            lcd.printString(buffer,0,2);

        // can also print individual characters at specified place
        lcd.printChar('X',5,3);

        // draw a line across the display at y = 40 pixels (origin top-left)
        for (int i = 0; i < WIDTH; i++) 
        {
            lcd.setPixel(i,40);
        }
        // need to refresh display after setting pixels
        lcd.refresh();

        // can also check status of pixels using getPixel(x,y)

        wait(5.0);
        lcd.clear();            // clear display
        lcd.inverseMode();      // invert colours
        lcd.setBrightness(1.0); // put LED backlight on full

        float array[84];

        for (int i = 0; i < 84; i++) 
        {
            array[i] = 0.5 + 0.5*sin(i*2*3.14/84);
        }

        // can also plot graphs - 84 elements only
        // values must be in range 0.0 - 1.0
        lcd.plotArray(array);
        wait(5.0);
        lcd.clear();
        lcd.normalMode();      // normal colour mode back
        lcd.setBrightness(0.5); // put LED backlight on 50%

        // example of drawing lines
        for (int x = 0; x < WIDTH ; x+=10) 
        {
            // x0,y0,x1,y1,type 0-white,1-black,2-dotted
            lcd.drawLine(0,0,x,HEIGHT,2);
        }

        wait(5.0);
        lcd.clear();

        // example of how to draw circles
        lcd.drawCircle(WIDTH/2,HEIGHT/2,20,1);  // x,y,radius,black fill
        lcd.drawCircle(WIDTH/2,HEIGHT/2,10,2);  // x,y,radius,white fill
        lcd.drawCircle(WIDTH/2,HEIGHT/2,30,0);  // x,y,radius,transparent with outline

        wait(5.0);
        lcd.clear();

        // example of how to draw rectangles
        //          origin x,y,width,height,type
        lcd.drawRect(10,10,50,30,1);  // filled black rectangle
        lcd.drawRect(15,15,20,10,2);  // filled white rectange (no outline)
        lcd.drawRect(2,2,70,40,0);    // transparent, just outline

        wait(5.0);
        lcd.clear();       
}     

//Interrupt service routines
//===========================
void button1Pressed()
{
    button1Flag = 1;
}       
void button2Pressed()
{
    button2Flag = 1;   
}    
void button3Pressed()
{
    button3Flag = 1;    
}   

//beep sound based on beeper frequency and beeper time 
//entered respectively
void beeper(float beeperFreq, float beeperTime)
{
    beep1.beep(beeperFreq,beeperTime);       
} 
     
void menuLevel(float lvl)
{
    if ((lvl >= 0) && (lvl < 0.25))
    {   
        cursorSelect(1, 6, 9, 14);
        if (button2)
        {   
            distFlag = 1;
            brightFlag = 0; 
            clockFlag = 0;
        }  
            
    }
    if ((lvl >= 0.25) && (lvl < 0.5))
    {
        cursorSelect(1, 6, 17, 22);
    }
    if ((lvl >= 0.5) && (lvl < 0.75))
    {
        cursorSelect(1, 6, 25, 30); 
        if (button2)
        {
            brightFlag = 1;
            distFlag =0; 
            clockFlag = 0;
        }  
          
    }
    if ((lvl >= 0.75) && (lvl <= 1))
    {
        cursorSelect(1, 6, 33, 38);
        if (button2)
        {
            clockFlag = 1;
            distFlag = 0;
            brightFlag = 0;     
        }          
    }
                                   
}    
void clockA()
{
    lcd.printString("Set clock",35,0);       
} 
void distMeasure()
{
    lcd.clear();
    lcd.printString("Measure",15,0);
    
    // read sensor distance in cm and print over serial port
    char buffer[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
        // so can display a string of a maximum 14 characters in length
        // or create formatted strings - ensure they aren't more than 14 characters long
    int distance = sensor.getDistanceCm();
    int length = sprintf(buffer,"Dist = %2d cm",distance); // print formatted data to buffer
        // it is important the format specifier ensures the length will fit in the buffer
    if (length <= 14)  // if string will fit on display
    {
        lcd.printString(buffer,0,1);           // display on screen
    }
    
   // serial.printf("Distance = %d cm\n",distance);
    serial.printf("\n\n\r %s, %0.2f C\n",buffer, length);
    
}  
  
void saveData()
{
    lcd.printString("Save data",36,0);
}      
void systemTest()
{
    lcd.printString("System test",30,0);
}    
void startMessage()
{
    lcd.printString("Press",25,2);
    lcd.printString("Start Button",8,3); 
    //displayClock();
           
} 
void displayClock()
{
    timer2.attach(&timer2Expired,1.0);
    serial.attach(&serialISR); // attach serial ISR
    lcd.clear();
    
    lcd.printString("-SYS CLOCK-",9,0);
    
    if (timer2Flag)
    {
        getCurrentTime();   
    }    
        
    if (setTimeFlag) 
    {
        chkUpdateTime();         
    }  
    
    lcd.printString(buffer2,4,2);           // display on screen
}