Final version of program with changed pin for PWM, freq for led and bluetooth comma seperated values

Dependencies:   mbed TextLCD

functions.cpp

Committer:
osamugint
Date:
2018-10-26
Revision:
1:3ae60fd51e2b
Child:
4:28134b3439bc

File content as of revision 1:3ae60fd51e2b:

#include "stdafx.h"
#include "functions.h"

float temp_cal(AnalogIn temp_sen)
{
    float a[10], tempC;
    float sum = 0;
    int i;
    for(i = 0; i < 10; i++) {
        a[i] = temp_sen.read();
        wait(.05);
        sum += a[i];
    }
    tempC=(sum*3.3*10); //3.685503686
    //tempF=(9.0*tempC)/5.0 + 32.0;
    return tempC;
}

float light_cal (AnalogIn light_sen)
{
    float vl[10], vol, sum1 = 0;
    int i;
    for (i = 0; i < 10; i++) {
        vl[i] = light_sen.read();
        wait(.05);
        sum1 += vl[i];
    }
    //output voltage of light sensor
    vol = sum1*3.3*10;
    return vol;
}

void buttonMode(int &state, DigitalIn buttonMode)
{
    if (buttonMode == 0) {
        if (state == 0) {
            state = 1;
        } else if (state == 1) {
            state = 2;
        } else {
            state = 0;
        }
    }
}

int buttonUp(DigitalIn buttonUp, int &value)
{
    if (buttonUp == 0) {
        value++;
    }
    return value;
}

int buttonDown(DigitalIn buttonDown, int &value)
{
    if (buttonUp == 0) {
        value--;
    }
    return value;
}

void mainScreen(TextLCD &lcd, AnalogIn &temp_sen, AnalogIn &light_sen, int &state, int &temp_val, int &light_val, int &fan_val)
{
    int vl_temp = 0;
    float vl_light = 0;
    if (state == 0) {
        vl_temp = temp_cal(temp_sen);
        vl_light = light_cal(light_sen);
        if (temp_val != vl_temp) {
            lcd.cls();
            lcd.printf("Temp: %i\n",vl_temp);
            wait(0.1);
            temp_val = vl_temp;
        } else {
            lcd.cls();
            lcd.printf("Temp: %i\n",temp_val);
            //lcd.printf("Light: %.2f \n", light_val);
            wait(0.1);
        }
        lcd.printf("Light: %.2f \n", vl_light);
    }
    if (state == 1) {


    }
}
void optionScreen(TextLCD &lcd, int &state, int &pointer, DigitalIn &up,  DigitalIn &down)
{
    if(pointer == 0) {
        lcd.cls();
        lcd.printf("o Set Temp\n");
        lcd.printf("  Set Light");
        wait(0.2);
    }
    if (up == 0 && down == 1 && pointer == 0) {
        pointer++; // pointer = 1
        lcd.cls();
        lcd.printf("  Set Temp\n");
        lcd.printf("o Set Light");
        wait(0.2);
    }
    if (up == 0 && down == 1 && pointer == 1) {
        pointer++; // pointer = 2
        lcd.cls();
        lcd.printf("o Set Fan\n");
        lcd.printf("  Set Bluetooth");
        wait(0.2);
    }
    if (up == 0 && down == 1 && pointer == 2) {
        pointer++; // pointer = 3
        lcd.cls();
        lcd.printf("  Set Fan\n");
        lcd.printf("o Set Bluetooth");
        wait(0.2);
    }
    if (up == 0 && down == 1 && pointer == 3) {
        lcd.cls();
        lcd.printf("  Set Fan\n");
        lcd.printf("o Set Bluetooth");
        wait(0.2);
    }
    if (up == 1 && down == 0 &&  pointer == 3) {
        pointer--; // pointer = 2
        lcd.cls();
        lcd.printf("o Set Fan\n");
        lcd.printf("  Set Bluetooth");
        wait(0.2);
    }
    if (up == 1 && down == 0 &&  pointer == 2) {
        pointer--; // pointer = 1
        lcd.cls();
        lcd.printf("  Set Temp\n");
        lcd.printf("o Set Light");
        wait(0.2);
    }
    if (up == 1 && down == 0 &&  pointer == 1) {
        pointer--; // pointer = 1
        lcd.cls();
        lcd.printf("o Set Temp\n");
        lcd.printf("  Set Light");
        wait(0.2);
    }
    if (up == 1 && down == 0 &&  pointer == 0) {
        lcd.cls();
        lcd.printf("o Set Temp\n");
        lcd.printf("  Set Light");
        wait(0.2);
    }
}