Joseph Boettcher / Mbed 2 deprecated mythermostat

Dependencies:   4DGL-uLCD-SE_ PinDetect SDFileSystem mbed

Fork of mythermostat by jim hamblen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TMP36.h"
00003 #include "SDFileSystem.h"
00004 #include "uLCD_4DGL.h"
00005 #include "PinDetect.h"
00006 #include "Speaker.h"
00007 #include "Shiftbrite.h"
00008 #include <math.h>
00009 #include <iostream>
00010 #include "stdio.h"
00011 
00012 // use class to setup temperature sensor pins
00013 TMP36 myTMP36(p15);  //Analog in
00014 
00015 // use class to setup microSD card filesystem
00016 SDFileSystem sd(p5, p6, p7, p8, "sd");
00017 
00018 // use class to setup the  Color LCD
00019 uLCD_4DGL uLCD(p28, p27, p29); // create a global uLCD object
00020 
00021 // use class to setup pushbuttons pins
00022 PinDetect pb1(p23);
00023 PinDetect pb2(p24);
00024 PinDetect pb3(p25);
00025 PinDetect pb4(p26);
00026 
00027 // use class to setup speaker pin
00028 Speaker mySpeaker(p21); //PWM out
00029 
00030 // use class to setup Shiftbrite pins
00031 Shiftbrite myShiftbrite(p9, p10, p11, p12, p13);// ei li di n/c ci
00032 
00033 // use class to setup Mbed's four on-board LEDs
00034 DigitalOut myLED1(LED1);
00035 DigitalOut myLED2(LED2);
00036 DigitalOut myLED3(LED3);
00037 DigitalOut myLED4(LED4);
00038 
00039 //also setting any unused analog input pins to digital outputs reduces A/D noise a bit
00040 //see http://mbed.org/users/chris/notebook/Getting-best-ADC-performance/
00041 DigitalOut P16(p16);
00042 DigitalOut P17(p17);
00043 DigitalOut bluepin(p18);
00044 DigitalOut greenpin(p19);
00045 DigitalOut redpin(p20);
00046 
00047 // Global variables used in callbacks and main program
00048 // C variables in interrupt routines should use volatile keyword
00049 //int volatile heat_setting=78; // heat to temp
00050 //int volatile cool_setting=68; // cool to temp
00051 //bool volatile mode=false; // heat or cool mode
00052 int target_temp;
00053 int Prior_temp = 75;
00054 double tempF = 75;
00055 int volatile heat_setpoint = 77;
00056 int volatile cool_setpoint = 68;
00057 int volatile heat_setpoint_C = 25;
00058 int volatile cool_setpoint_C = 20;
00059 int mode = 0;
00060 int centigrade = 0;
00061 
00062 // Callback routine is interrupt activated by a debounced pb1 hit
00063 void pb1_hit_callback (void)
00064 {
00065     target_temp++;
00066     mySpeaker.PlayNote(2000.0, 0.05, 1.0);
00067 }
00068 // Callback routine is interrupt activated by a debounced pb2 hit
00069 void pb2_hit_callback (void)
00070 {
00071     target_temp--;
00072     mySpeaker.PlayNote(2000.0, 0.05, 1.0);
00073 }
00074 // Callback routine is interrupt activated by a debounced pb3 hit
00075 void pb3_hit_callback (void)
00076 {
00077     mode =  (mode + 1) % 3; // Cycle forward  by 1
00078     mySpeaker.PlayNote(3000.0, 0.025, 1.0);
00079 }
00080 void pb4_hit_callback (void)
00081 {
00082     wait(.001);
00083     centigrade = (centigrade + 1) % 2;
00084 }
00085 
00086 int main()
00087 {
00088     float Current_temp=0.0;
00089 
00090     // Use internal pullups for the three pushbuttons
00091     pb1.mode(PullUp);
00092     pb2.mode(PullUp);
00093     pb3.mode(PullUp);
00094     pb4.mode(PullUp);
00095     // Delay for initial pullup to take effect
00096     wait(.01);
00097     // Setup Interrupt callback functions for a pb hit
00098     pb1.attach_deasserted(&pb1_hit_callback);
00099     pb2.attach_deasserted(&pb2_hit_callback);
00100     pb3.attach_deasserted(&pb3_hit_callback);
00101     pb4.attach_deasserted(&pb4_hit_callback);
00102     // Start sampling pb inputs using interrupts
00103     pb1.setSampleFrequency();
00104     pb2.setSampleFrequency();
00105     pb3.setSampleFrequency();
00106     pb4.setSampleFrequency();
00107     // pushbuttons now setup and running
00108     
00109     printf("Hello PC World\n\r"); // need terminal application running on PC to see this output
00110     uLCD.printf("\n\rJoe's Thermostat\n\r"); // LCD
00111     mySpeaker.PlayNote(500.0, 1.0, 1.0); // Speaker buzz
00112     enum Statetype { Off = 0, Heat_off, Heat_on, Cool_off, Cool_on };
00113     Statetype state = Off;
00114     uLCD.locate(3,6);
00115     uLCD.printf("OFF         ");
00116     // State machine code below will need changes and additions
00117     while (1) {
00118             
00119         while(1) {
00120             Prior_temp = tempF;
00121             Current_temp = myTMP36; //Read temp sensor
00122             tempF = (9.0*Current_temp)/5.0+ 32.0;
00123             double tempC = Current_temp;
00124             
00125             
00126             switch (mode) {
00127             case (0):
00128                 uLCD.locate(3,6);
00129                 uLCD.printf("OFF         ");
00130                 //target_temp = 75;
00131                 break;
00132             case (1):
00133                 uLCD.locate(3,6);
00134                 uLCD.color(RED);
00135                 uLCD.printf("HEAT        ");
00136                 uLCD.color(GREEN);
00137                 //if (centigrade == 0)
00138                   //  target_temp = heat_setpoint;
00139                 //else
00140                   //  target_temp = heat_setpoint_C;
00141                 break;
00142             case (2):
00143                 uLCD.locate(3,6);
00144                 uLCD.color(LBLUE);
00145                 uLCD.printf("COOL        ");
00146                 uLCD.color(GREEN);
00147                // if (centigrade == 0)
00148                  //   target_temp = cool_setpoint;
00149                 //else
00150                   //  target_temp = cool_setpoint_C;
00151                 break;
00152             }
00153             
00154             
00155             
00156             if (centigrade == 1)
00157                 tempF = tempC;
00158             //tempF = round(tempF);
00159             
00160             uLCD.locate(2,10);
00161             if (centigrade == 0)
00162             {
00163                 uLCD.text_width(2);
00164                 uLCD.text_height(2);
00165                 uLCD.printf("%5.0F F \n\r", tempF);  
00166                 uLCD.text_width(1);
00167                 uLCD.text_height(1);
00168             }
00169             if (centigrade == 1)
00170             {
00171                 uLCD.text_width(2);
00172                 uLCD.text_height(2);
00173                 uLCD.printf("%5.0F C \n\r", tempC);
00174                 uLCD.text_width(1);
00175                 uLCD.text_height(1);
00176             }
00177                 
00178             if (mode == 0)
00179             {
00180                 state = Off;
00181                 greenpin = 1;
00182                 redpin = 0;
00183                 bluepin = 0;
00184             }
00185             if (mode == 1)
00186             {
00187                 if (tempF > target_temp + 1)
00188                     {
00189                         if (Prior_temp < target_temp + 1)
00190                         {
00191                             mySpeaker.PlayNote(500.0, 0.025, 1.0);
00192                         }
00193                         greenpin = 0;
00194                         redpin = 0;
00195                         bluepin = 0;
00196                         state = Heat_off;
00197                     }
00198                 if (tempF < target_temp - 1)
00199                     {
00200                         if (Prior_temp > target_temp - 1)
00201                         {
00202                             mySpeaker.PlayNote(500.0, 0.025, 1.0);
00203                         }
00204                         greenpin = 0;
00205                         redpin = 1;
00206                         bluepin = 0;
00207                         state = Heat_on;
00208                     }
00209             }
00210             if (mode == 2)
00211             {
00212                 if (tempF > target_temp + 1)
00213                     {
00214                         if (Prior_temp < target_temp + 1)
00215                         {
00216                             mySpeaker.PlayNote(1000.0, 0.025, 1.0);
00217                         }
00218                         greenpin = 0;
00219                         redpin = 0;
00220                         bluepin = 1;
00221                         state = Cool_on;
00222                     }
00223                 if (tempF < target_temp - 1)
00224                     {
00225                         if (Prior_temp > target_temp - 1)
00226                         {
00227                             mySpeaker.PlayNote(1000.0, 0.025, 1.0);
00228                         }
00229                         greenpin = 0;
00230                         redpin = 0;
00231                         bluepin = 0;
00232                         state = Cool_off;
00233                     }
00234             }
00235             uLCD.locate(3,5);
00236             if (mode == 1) {
00237                 uLCD.color(RED);
00238                 uLCD.printf("Heat to: %2d", target_temp);
00239                 uLCD.color(GREEN);
00240                 myLED1 = 1;
00241                 myLED2 = 0; }
00242             if (mode == 2) {
00243                 uLCD.color(LBLUE);
00244                 uLCD.printf("Cool to: %2d", target_temp);
00245                 uLCD.color(GREEN);
00246                 myLED1 = 0;
00247                 myLED2 = 1; }
00248             if (mode == 0) {
00249                 uLCD.printf("                ", target_temp);
00250                 myLED1 = 0;
00251                 myLED2 = 0; }
00252                 
00253                 switch (state) {
00254                     case Heat_off:
00255                         myLED3 = 0;
00256                         myLED4 = 0;
00257                         //state = Heat_on;
00258                         uLCD.locate(3,4);
00259                         uLCD.printf("Heat Off");
00260                         break;
00261                     case Heat_on:
00262                         myLED3 = 0;
00263                         myLED4 = 1;
00264                         //state = Heat_off;
00265                         uLCD.locate(3,4);
00266                         uLCD.color(RED);
00267                         uLCD.printf("Heat On  ");
00268                         uLCD.color(GREEN);
00269                         break;
00270                     case Cool_off:
00271                         myLED3 = 0;
00272                         myLED4 = 0;
00273                         //state = Cool_on;
00274                         uLCD.locate(3,4);
00275                         uLCD.printf("Cool Off");
00276                         break;
00277                     case Cool_on:
00278                         myLED3 = 1;
00279                         myLED4 = 0;
00280                         //state = Cool_off;
00281                         uLCD.locate(3,4);
00282                         uLCD.color(LBLUE);
00283                         uLCD.printf("Cool On  ");
00284                         uLCD.color(GREEN);
00285                         break;
00286                     case Off:
00287                         myLED4 = 0;
00288                         uLCD.locate(3,4);
00289                         uLCD.printf("         ");
00290                         break;
00291                 }
00292         
00293                 wait(0.33);
00294         }
00295     }
00296 }