Embedded Systems Project Mateusz Loboda 200843098

Dependencies:   N5110 SRF02-Mateusz mbed

main.cpp

Committer:
el14ml
Date:
2016-05-04
Revision:
0:3403a3415306
Child:
1:32b9ad362749

File content as of revision 0:3403a3415306:

/*
* @file main.cpp
* @Distance Sensor Project
* @brief Main file containing functions and int main().
* @author Mateusz Loboda
* @date April 2016
*/


//sd card
//mbed leds come on sometimes;
//make 4k mid freq
//fix buzzer beeping


#include "mbed.h"
#include "main.h"


int main()

{
    init_K64F();
    lcd.init();

    initialScreen();
    out.attach(timeout_isr,4);
    lcd.refresh();
    sleep();
    lcd.clear();
    lcd.refresh();
    initialArray();
    ticker.attach(&ticker_isr,0.1);


    while(1) {
        delay = rate;

        buzzerPeriod = 1/((4000-(2*averageDistance))+200); // BUZZER, 200+200 = 400 = maximum range so freq proportional to distance, org 1/(200-avg)+200

        if (g_ticker_flag) {

            g_ticker_flag = 0;
            if ( g_button2_flag == 0) {

                buzzer = delay; // duty cycle
                buzzer.period(buzzerPeriod); //set pwm to my freq
                // average distance calculated
                get_averageDistance();
                redLedIndicator();
                drawDistanceBars();
                

            } else if (g_button2_flag == 1) {
               // lcd.clear();
                  moveArrayElements();
                  get_averageDistance();
                  adjacentArrayElements();
                  plotAxes();
                  plotDistancePoint();
              //  modeTwo();

                lcd.refresh();

            }
            lcd.refresh();
            sleep();
        }
    }
}
void init_K64F()
{
    // on-board LEDs are active-low, so set pin high to turn them off.
    r_led = 1;
    g_led = 1;
    b_led = 1;
    button2.fall(&mode); //under this condition call function
    button2.mode(PullUp); //enable internal pull up resistor
    button1.fall(&units);
    button1.mode(PullUp);

}
void units()
{
    g_button1_flag =!g_button1_flag ;   // set flag in ISR
   // r_led = !r_led;
}
void mode()
{
    g_button2_flag =!g_button2_flag ;
   // b_led = !b_led;
  // lcd.clear();
   //lcd.refresh();
}
void timeout_isr()
{
}
void ticker_isr()
{
    g_ticker_flag = 1;
}
void initialScreen()
{
    lcd.printString("DISTANCE",18,0);
    lcd.printString("SENSOR",22,1);
    lcd.printString("Mateusz Loboda",0,3);
    lcd.printString("200843098",16,4);
    lcd.refresh();
}
void initialArray()
{
    //graphArray[82]; //initialize graph array, 2 pixels used for y axis so not 84
    for (int i=0; i<83; i++) { // before entering the grpah mode with the button, it is alreadt plotting points but they cannot be seen as they are above max range of 200
        graphArray[i] = 201; // has to be more thn 200
    }
    lcd.clear();
}
void get_averageDistance()
{
    for ( int i=0 ; i<5; i++) {
        int distance = sensor.getDistanceCm();
        if (distance < 400) { //better averages random annomous values not even considere
            averageDistance += distance; //assignment by sum
        } else {
            i--;  //if distance > 400 do not take that reading into account go again
        }
    }
    averageDistance = averageDistance/5;
   lcd.clear();
}

void redLedIndicator()
{
    if (averageDistance<=30) {
        // r_led = 1;
        //   g_led = 1;
        //  b_led = 1;
        myled = 1;
        lcd.clear();
        lcd.printString(" ***COLLISION ",2,1);
        lcd.printString(" WARNING***   ",8,3);
        lcd.refresh();
        buzzer = delay; // duty cycle
        buzzer.period(0.001); //set pwm to my freq
    } else {
        r_led = 1;
        g_led = 1;
        b_led = 1;
        myled = 0;
    }
    if (averageDistance >30) {
        lcd.clear();
        char str[10];
        if (g_button1_flag == 0) {

            sprintf(str,"%.2f",averageDistance);
            lcd.printString("cm",62,4);

        } else  if (g_button1_flag == 1) {
            sprintf(str,"%.2f",averageDistance*0.393701);
            lcd.printString("in",60,4);

        }
        lcd.printString("                                  ",0,3);
        lcd.printString(str,20,4);
        lcd.refresh();
    }
}
void drawDistanceBars()
{
    if(averageDistance>380) {
        lcd.drawRect(67,2,5,16,1);

    }
    if(averageDistance>330) {
        lcd.drawRect(59,2,5,16,1);

    }
    if(averageDistance>280) {
        lcd.drawRect(51,2,5,16,1);

    }
    if(averageDistance>230) {
        lcd.drawRect(43,2,5,16,1);

    }
    if(averageDistance>180) {
        lcd.drawRect(35,2,5,16,1);

    }
    if(averageDistance>130) {
        lcd.drawRect(27,2,5,16,1);

    }
    if(averageDistance>80) {
        lcd.drawRect(19,2,5,16,1); //Draw 2 Bars

    }
    if(averageDistance>30) {
        lcd.drawRect(12,2,5,16,1); // 1 rectangle
    }
    lcd.refresh();
}
void moveArrayElements()
{

    for (int i=81 ; i>0; i--) {
        //move elements of distance to right
        graphArray[i] = graphArray[i-1];  //moving each element of array to right
    }
    
    /*    for ( int i=0 ; i<5; i++) {
        int distance = sensor.getDistanceCm();
        if (distance < 400) { // decrease random error
            averageDistance += distance;
        } else {
            i--;
        }
    }
    //lcd.clear();
    averageDistance = averageDistance/5; */
   //get_averageDistance();
    
}
void adjacentArrayElements()
{

    graphArray[0]= averageDistance; //array element is average distance float
    if ((int)graphArray[0] != (int)graphArray[1]) { //if value of new array element is different to last one red led comes on
        myled=1;
        buzzer = delay; // duty cycle
        buzzer.period(buzzerPeriod); //set pwm to my freq

    } else {
        myled=0;
        buzzer=0;
    }
}

void plotAxes()
{
    //i is pixel, plotting x axis
    for ( int i=0; i<84; i++) {
        lcd.setPixel(i,46);
        lcd.setPixel(i,47);
    }
    //plotting y axis
    for ( int i=0; i<48; i++) {
        lcd.setPixel(0,i);
        lcd.setPixel(1,i);
    }
}
void plotDistancePoint()
{

    // i in this loop is element of the array NOT pixel
    for (int i=0; i<82; i++) {
        //x position + y position)
        int p = (int)(45-(graphArray[i]/(200/46))); //convert array element float distance to integer pixel
        //convert from float distance to integer pixel
        lcd.setPixel(83-i,p); // plot in pixel 84 at the right height , this pixel is then moved to right and new pixel is plotted
    }
    lcd.refresh();
}
void modeTwo()
{
    for (int i=81 ; i>0; i--) {
        //move elements of distance to right
        graphArray[i] = graphArray[i-1];  //moving each element of array to right
    }
    for ( int i=0 ; i<5; i++) {
        int distance = sensor.getDistanceCm();
        if (distance < 400) { // decrease random error
            averageDistance += distance;
        } else {
            i--;
        }
    }
    lcd.clear();
    averageDistance = averageDistance/5;


    graphArray[0]= averageDistance; //plot distance
    if ((int)graphArray[0] != (int)graphArray[1]) {
        myled=1;
        buzzer = delay; // duty cycle
        buzzer.period(buzzerPeriod); //set pwm to my freq
    } else {
        myled=0;
        buzzer=0;
    }

    for ( int i=0; i<84; i++) {
        lcd.setPixel(i,46);
        lcd.setPixel(i,47);
    }
    for ( int i=0; i<48; i++) {
        lcd.setPixel(0,i);
        lcd.setPixel(1,i);
    }
    // i in this loop is element of the array NOT pixel
    for (int i=0; i<82; i++) {
        //x position + y position)
        int p = (int)(45-(graphArray[i]/(200/46)));
        //convert from float distance to integer pixel
        lcd.setPixel(83-i,p);
    }
}













/*switch(averageDistance) {
      case 380 :
      lcd.drawRect(67,2,5,16,1);
      break;
      case 330 :
       lcd.drawRect(59,2,5,16,1);
       break;
       case 280 :
         lcd.drawRect(51,2,5,16,1);
         break;
         case 230 :
         lcd.drawRect(43,2,5,16,1);
         break;
         case 180 :
         lcd.drawRect(35,2,5,16,1);
         break;
         case 130 :
         lcd.drawRect(27,2,5,16,1);
         break;
         case 80 :
                lcd.drawRect(19,2,5,16,1); //Draw 2 Bars
                break;
                case 30 :
                  lcd.drawRect(12,2,5,16,1); // 1 rectangle
                  break;
                  } */