Keiarash Zoughi's Multi-Sensor for weather detection.

Dependencies:   BMP180 N5110 mbed

main.cpp

Committer:
el13kz
Date:
2015-05-08
Revision:
9:5ebf44c85f77
Parent:
8:956a130ebdf5
Child:
10:d62ac368381c

File content as of revision 9:5ebf44c85f77:

#include "mbed.h"
#include "N5110.h"
#include "BMP180.h"

#define PI 3.14159265359

//LCD inputs
N5110 lcd(p7,p8,p9,p10,p11,p13,p26);

//serial port for debugging
Serial serial(USBTX,USBRX);

//LED outputs
BusOut leds(LED4,LED3,LED2,LED1);  // LEDs for display


//select button
DigitalIn pushbutton1(p19);


//barometer sensor input
BMP180 bmp180(p28,p27);   // SDA, SCL

//warning LED
PwmOut Redled(p24);

//globals
int selectedOption1 = 0;
int buttonPressed = 0;
int selectedOption2 = 0;
int selectedOption3 = 0;
int selectedOption4 = 0;

//boundary conditions - screen
int cells[84][48];

Timer t1;
Timer t2;
Timer t3;
Timer t4;
Timer t5;
Timer t6;


void check()
{
    lcd.clear();

    Measurement measurement;  // measurement structure declared in BMP180 class


    // read values (T in Celsius and P in mb) and print over serial port
    measurement = bmp180.readValues();
    serial.printf("T = %.2f C P = %.2f mb\n",measurement.temperature,measurement.pressure);

    // 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
    float temperature = measurement.temperature;
    // display on screen
char buffer[14];  // each charact
int length = sprintf(buffer,"T = %.2f 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,5);           // display on screen



    if(temperature > 30.0 ) {

        lcd.printString("TEMP HIGH!",1,1);
        Redled=1.0;



    } else if(temperature < 3.0) {

        lcd.printString("TEMP LOW!",1,1);
        Redled= 1.0;


    } else {
        lcd.printString("TEMP OK!",1,1);
        Redled = 0.0;
    }


    wait(3);
lcd.clear();


}

void pie(){
    lcd.clear();
     // example of how to draw circles
        
        lcd.drawCircle(42,24,22,0);  // x,y,radius,transparent with outline


        for (int x = 0; x < WIDTH ; x+=10) {
            // x0,y0,x1,y1,type 0-white,1-black,2-dotted
            lcd.drawLine(42,24,62,14,1);
            lcd.drawLine(42,24,62,34,1);





}
wait(5);

lcd.clear();
}



//shows pressure reading and temperature
void measurement()
{
    lcd.clear();

    Measurement measurement;  // measurement structure declared in BMP180 class
    while(1) {

        // read values (T in Celsius and P in mb) and print over serial port
        measurement = bmp180.readValues();
        serial.printf("T = %.2f C P = %.2f mb\n",measurement.temperature,measurement.pressure);

        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 = measurement.temperature;
        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 = measurement.pressure;  // same idea with floats
        length = sprintf(buffer,"P = %.2f mb",pressure);
        if (length <= 14)
            lcd.printString(buffer,0,2);
    }
}

void temperature()
{
    lcd.clear();

    Measurement measurement;  // measurement structure declared in BMP180 class
    t1.start();
    while(t1.read()<5) {

        // read values (T in Celsius and P in mb) and print over serial port
        measurement = bmp180.readValues();
        serial.printf("T = %.2f C P = %.2f mb\n",measurement.temperature,measurement.pressure);

        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 = measurement.temperature;
        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);           //

    }
t1.stop();
lcd.clear();

}

void pressure()
{
    lcd.clear();

    Measurement measurement;  // measurement structure declared in BMP180 class
    
    t2.start();

    while(t2.read()<5) {

        // read values (T in Celsius and P in mb) and print over serial port
        measurement = bmp180.readValues();
        serial.printf("T = %.2f C P = %.2f mb\n",measurement.temperature,measurement.pressure);

        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

//
        float pressure = measurement.pressure;  // same idea with floats
        int length = sprintf(buffer,"P = %.2f mb",pressure);
        if (length <= 14)
            lcd.printString(buffer,0,1);
    }
t2.stop();
lcd.clear();

}



void altitude()  // creating a new reading from the current ones, this appoximates the altitude in metres.
{

    lcd.clear();

    Measurement measurement;  // measurement structure declared in BMP180 class


t3.start();

    while(t3.read()<5) {
        // read values (T in Celsius and P in mb) and print over serial port
        measurement = bmp180.readValues();
        char buffer[14];
        int temperature = measurement.temperature;
        float pressure = measurement.pressure;  // same idea with floats

        float altitude = -(log(pressure/1013.25)*1.38e-23*temperature)/(9.81*28.95);
        serial.printf("A = %.2f m",altitude);
        int length = sprintf(buffer,"A = %.2f m",altitude); // print formatted data to buffer
        // it is important the format specifier ensures the length will fit in the buffer
        if (length <= 14)  // if string wi
            lcd.printString(buffer,0,1);           // display on screen


            lcd.drawLine(4,48,24,28,1);
            lcd.drawLine(24,28,44,48,1);
            lcd.drawLine(34,38,44,28,1);
            lcd.drawLine(44,28,64,48,1);
            


    }
t3.stop();
lcd.clear();

}
//pushbutton - 0

//plots graph of temperature point by point
void graph()
{
    lcd.clear();

    Measurement measurement;  // measurement structure declared in BMP180 class
    int i = 0;
    float graph[84]= {0};
    
    
   t4.start(); 
    while(t4.read()<15) {

        // read values (T in Celsius and P in mb) and print over serial port
        measurement = bmp180.readValues();
        //serial.printf("T = %.2f C P = %.2f mb\n",measurement.temperature,measurement.pressure);

        // 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 = measurement.temperature;
//       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 = measurement.pressure;  // same idea with floats
//        length = sprintf(buffer,"P = %.2f mb",pressure);
//        if (length <= 14)
//            lcd.printString(buffer,0,2);

        graph[i]= temperature ;
        lcd.plotArray(graph);
        wait(0.1);  // short delay until next reading
        i++;
        if (i>83) {
            i=0;
            lcd.clear();
        }
    }

t4.stop();

lcd.clear();
}




void graphScreen(){
    
 lcd.clear();
    lcd.printString("Hold button!",6,2);//select measure menu
    wait(1);
    lcd.clear();
    selectedOption1 = 0;

    while(1) {

        lcd.drawRect(70,6,10,10,1);
        lcd.drawRect(70,21,10,10,0);
        lcd.drawRect(70,37,10,10,0);//check box 3
        lcd.printString("line graph",1,1);
        lcd.printString("pie chart",1,3);
        lcd.printString("go back",1,5);

        lcd.printString(">",62,1);
        selectedOption1 = 1;
        if (pushbutton1)
            break;
        wait(2);
        lcd.clear();

        lcd.drawRect(70,6,10,10,0);
        lcd.drawRect(70,21,10,10,1);
        lcd.drawRect(70,37,10,10,0);//check box 3
        lcd.printString("line graph",1,1);
        lcd.printString("pie chart",1,3);

        lcd.printString("go back",1,5);
        lcd.printString(">",62,3);
        selectedOption1 = 2;
        if (pushbutton1)
            break;
        wait(2);
        lcd.clear();

        lcd.drawRect(70,6,10,10,0);
        lcd.drawRect(70,21,10,10,0);
        lcd.drawRect(70,37,10,10,1);//check box 3
        lcd.printString("line graph",1,1);
        lcd.printString("pie chart",1,3);
        lcd.printString("go back",1,5);
        lcd.printString(">",62,5);
        selectedOption1 = 3;
        if (pushbutton1)
            break;
        wait(2);
        lcd.clear();

    }

    if(selectedOption1 == 1) {

        lcd.clear();
    }

    if(selectedOption1 == 2) {

        graph();

    }

    if(selectedOption1 == 3) {

        pie();
    }


}   



void measurementsScreen()
{
    lcd.clear();
    lcd.printString("Hold button!",6,2);//select measure menu
    wait(1);
    lcd.clear();
    selectedOption2 = 0;

    while(1) {

        lcd.drawRect(70,6,10,10,1);
        lcd.drawRect(70,21,10,10,0);
        lcd.drawRect(70,37,10,10,0);//check box 3
        lcd.printString("temperature",1,1);
        lcd.printString("altitude",1,3);
        lcd.printString("pressure",1,5);

        lcd.printString(">",62,1);
        selectedOption2 = 1;
        if (pushbutton1)
            break;
        wait(2);
        lcd.clear();

        lcd.drawRect(70,6,10,10,0);
        lcd.drawRect(70,21,10,10,1);
        lcd.drawRect(70,37,10,10,0);//check box 3
        lcd.printString("temperature",1,1);
        lcd.printString("pressure",1,5);

        lcd.printString("altitude",1,3);
        lcd.printString(">",62,3);
        selectedOption2 = 2;
        if (pushbutton1)
            break;
        wait(2);
        lcd.clear();

        lcd.drawRect(70,6,10,10,0);
        lcd.drawRect(70,21,10,10,0);
        lcd.drawRect(70,37,10,10,1);//check box 3
        lcd.printString("temperature",1,1);
        lcd.printString("altitude",1,3);
        lcd.printString("pressure",1,5);
        lcd.printString(">",62,5);
        selectedOption2 = 3;
        if (pushbutton1)
            break;
        wait(2);
        lcd.clear();

    }

    if(selectedOption2 == 1) {

        pressure();
    }

    if(selectedOption2 == 2) {

        temperature();

    }

    if(selectedOption2 == 3) {

        altitude();
    }


}

void introScreen()
{

    lcd.printString("Welcome to",5,1);
    lcd.printString("Keiarash's",5,2);
    lcd.printString("Multi-Sensor",5,3);
    Redled= 1.0;


}


void extrasScreen()
{
    lcd.clear();
    lcd.printString("Hold Button!",6,2);//select measure menu
    wait(1);
    lcd.clear();
    selectedOption3 = 0;

    while(1) {

        lcd.drawRect(70,6,10,10,1);
        lcd.drawRect(70,21,10,10,0);

        lcd.printString("check",1,1);
        lcd.printString(">",62,1);
        selectedOption3 = 1;
        if (pushbutton1)
            break;
        wait(2);
        lcd.clear();

        lcd.drawRect(70,6,10,10,0);
        lcd.drawRect(70,21,10,10,1);
        lcd.printString("go back",1,3);
        lcd.printString(">",62,3);
        selectedOption3 = 2;
        if (pushbutton1)
            break;
        wait(2);
        lcd.clear();

    }

    if(selectedOption3 == 1) {

        lcd.clear();
    }

    if(selectedOption3 == 2) {

        check();
        wait(0.5);

    }
}



void mainScreen()
{
    lcd.clear();
    lcd.printString("Hold Button!",6,2);//select measure menu

    wait(1);
    lcd.clear();

    selectedOption4 = 0;

    while(1) {//initialse pushbutton as 0

        //check boxes-options to be selected
        lcd.drawRect(70,6,10,10,1);//check box 1
        lcd.drawRect(70,21,10,10,0);//check box 2
        lcd.drawRect(70,37,10,10,0);//check box 3

        //option titles-appropriate to check boxes
        lcd.printString("extras",1,1);//select measure menu
        lcd.printString("graph",1,3);//select settings menu
        lcd.printString("measure",1,5);//select grpah plot
        lcd.printString(">",62,1);//pointer
        selectedOption4 = 1;
        if (pushbutton1)
            break;

        wait(2);
        lcd.clear();

        lcd.drawRect(70,6,10,10,0);
        lcd.drawRect(70,21,10,10,1);
        lcd.drawRect(70,37,10,10,0);


        lcd.printString("extras",1,1);
        lcd.printString("graph",1,3);
        lcd.printString("measure",1,5);
        lcd.printString(">",62,3);
        selectedOption4 = 2;
        if (pushbutton1)
            break;


        wait(2);
        lcd.clear();

        lcd.drawRect(70,6,10,10,0);
        lcd.drawRect(70,21,10,10,0);
        lcd.drawRect(70,37,10,10,1);


        lcd.printString("extras",1,1);
        lcd.printString("graph",1,3);
        lcd.printString("measure",1,5);
        lcd.printString(">",62,5);
        selectedOption4 = 3;
        if (pushbutton1)
            break;


        wait(2);
        lcd.clear();


    }

    if(selectedOption4 == 1) {

        measurementsScreen();
    }

    if(selectedOption4 == 2) {

        extrasScreen();
    }

    if(selectedOption4 == 3) {

        graphScreen();
    }
}











//main loop
int main()
{
   pushbutton1.mode(PullUp);//initial value 0
    // initiliase barometer
    bmp180.init();

    lcd.init();
    introScreen();
    wait(3);
    Redled =0;
    

   while(1) {
   
    lcd.clear();
    lcd.printString("main menu...",1,2);
    wait(2);
    mainScreen();
    


    }
}