Marcus Parker / Mbed 2 deprecated Thermostat_NucleoF401

Dependencies:   RepeatButton TMP36 GZ TextLCD mbed

Fork of Thermostat_Nucleo by Sapphire

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /************************************************************************************
00002 Copyright (c) 2014 Marcus Parker  Sapphire Research and Electronics ltd
00003     
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010  
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013  
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 THE SOFTWARE. 
00021 
00022 This program uses the following libraries:  
00023 mbed by mbed_official                       
00024 TextLCD by Simon_Ford                       
00025 TMP36GZ by Tyler_Weaver                     
00026 RepeatButton by Jeroen_Hilgers              
00027                                            
00028 Wiring instructions:                        
00029 Two external buttons -> PB_4 and PB_3 
00030 TMP36GZ pwr->3.3v, Vout->PA_0               
00031 LCD connections as below                    
00032 *****************************************************************************************/
00033 
00034 #include "mbed.h"
00035 #include "TextLCD.h"
00036 #include "TMP36GZ.h"
00037 #include "RepeatButton.h"
00038 
00039 using namespace std;
00040 #define REPEAT_DELAY 500
00041 #define REPEAT_PERIOD 100
00042 #define KEY_UP 2
00043 #define KEY_DOWN 4
00044 
00045 Ticker tick;
00046 TextLCD lcd(PC_10, PC_12, PA_13, PA_14, PA_15, PB_7); // rs, e, d4-d7
00047 TMP36GZ temp(PA_0); 
00048 DigitalOut led(LED1);
00049 
00050 KeyBuffer TheKeyBuffer(32);
00051 RepeatButton butUp(PB_4, REPEAT_DELAY, REPEAT_PERIOD, &TheKeyBuffer, KEY_UP);
00052 RepeatButton butDown(PB_3, REPEAT_DELAY, REPEAT_PERIOD, &TheKeyBuffer, KEY_DOWN);
00053 
00054 float d, level;
00055 
00056 void disp() 
00057 {
00058     lcd.locate(0, 0); 
00059     lcd.printf("%3.1f %cC", d, 223);
00060     lcd.locate(0, 1);
00061     lcd.printf("Trip %3.1f", level);  
00062 }
00063 
00064 int main() 
00065 {
00066     level = 25.0;  //Initial trip level value
00067     float leveloff;
00068     int value;
00069     
00070     tick.attach(&disp, 0.5); //Calls display every 0.5 seconds
00071     
00072     while(1) 
00073     { 
00074     d = temp.sample();  //Define d as temp celsius reading
00075     leveloff = (level - 2);
00076     
00077     for(value = TheKeyBuffer.Read(); value != -1; value = TheKeyBuffer.Read()) //Button 1 and Button 3 increase and decrease trip level
00078     {
00079         switch(value)
00080         {
00081             case KEY_UP:
00082             level += 0.1;
00083             break;
00084                 
00085             case KEY_DOWN:
00086             level -= 0.1;
00087             break;
00088         }
00089     } 
00090 
00091     if(d >= level) //Led will light up if temperature is above trip level
00092     {
00093     led = 1;
00094     }
00095     else if(d <= leveloff)
00096     {
00097     led = 0;
00098     }   
00099     } 
00100 }