complete
Dependencies: 4DGL-uLCD-SE_ PinDetect SDFileSystem mbed
Fork of mythermostat by
Diff: main.cpp
- Revision:
- 5:c73ebb00e86d
- Parent:
- 4:9a4d22a279b3
- Child:
- 6:5ba4232c5e1c
--- a/main.cpp Thu Jan 23 16:47:05 2014 +0000 +++ b/main.cpp Tue Sep 06 21:36:17 2016 +0000 @@ -8,6 +8,9 @@ #include "Speaker.h" // must add your new class code to the project file Shiftbrite.h #include "Shiftbrite.h" +#include <math.h> +#include <iostream> +#include "stdio.h" // use class to setup temperature sensor pins TMP36 myTMP36(p15); //Analog in @@ -22,6 +25,7 @@ PinDetect pb1(p23); PinDetect pb2(p24); PinDetect pb3(p25); +PinDetect pb4(p26); // use class to setup speaker pin Speaker mySpeaker(p21); //PWM out @@ -35,41 +39,52 @@ DigitalOut myLED3(LED3); DigitalOut myLED4(LED4); - - //also setting any unused analog input pins to digital outputs reduces A/D noise a bit //see http://mbed.org/users/chris/notebook/Getting-best-ADC-performance/ DigitalOut P16(p16); DigitalOut P17(p17); -DigitalOut P18(p18); -DigitalOut P19(p19); -DigitalOut P20(p20); - - - +DigitalOut bluepin(p18); +DigitalOut greenpin(p19); +DigitalOut redpin(p20); // Global variables used in callbacks and main program // C variables in interrupt routines should use volatile keyword -int volatile heat_setting=78; // heat to temp -int volatile cool_setting=68; // cool to temp -bool volatile mode=false; // heat or cool mode +//int volatile heat_setting=78; // heat to temp +//int volatile cool_setting=68; // cool to temp +//bool volatile mode=false; // heat or cool mode +int target_temp; +int Prior_temp = 75; +double tempF = 75; +int volatile heat_setpoint = 77; +int volatile cool_setpoint = 68; +int volatile heat_setpoint_C = 25; +int volatile cool_setpoint_C = 20; +int mode = 0; +int centigrade = 0; // Callback routine is interrupt activated by a debounced pb1 hit void pb1_hit_callback (void) { -// ADD CODE HERE + target_temp++; + mySpeaker.PlayNote(2000.0, 0.05, 1.0); } // Callback routine is interrupt activated by a debounced pb2 hit void pb2_hit_callback (void) { -// ADD CODE HERE + target_temp--; + mySpeaker.PlayNote(2000.0, 0.05, 1.0); } // Callback routine is interrupt activated by a debounced pb3 hit void pb3_hit_callback (void) { -// ADD CODE HERE + mode = (mode + 1) % 3; // Cycle forward by 1 + mySpeaker.PlayNote(3000.0, 0.025, 1.0); } - +void pb4_hit_callback (void) +{ + wait(.001); + centigrade = (centigrade + 1) % 2; +} int main() { @@ -79,63 +94,228 @@ pb1.mode(PullUp); pb2.mode(PullUp); pb3.mode(PullUp); + pb4.mode(PullUp); // Delay for initial pullup to take effect wait(.01); // Setup Interrupt callback functions for a pb hit pb1.attach_deasserted(&pb1_hit_callback); pb2.attach_deasserted(&pb2_hit_callback); pb3.attach_deasserted(&pb3_hit_callback); + pb4.attach_deasserted(&pb4_hit_callback); // Start sampling pb inputs using interrupts pb1.setSampleFrequency(); pb2.setSampleFrequency(); pb3.setSampleFrequency(); + pb4.setSampleFrequency(); // pushbuttons now setup and running // start I/O examples - DELETE THIS IN YOUR CODE..BUT WILL USE THESE I/O IDEAS ELSEWHERE // since all this compiles - the needed *.h files for these are in the project // - Current_temp = myTMP36; //Read temp sensor + printf("Hello PC World\n\r"); // need terminal application running on PC to see this output - uLCD.printf("\n\rHello LCD World\n\r"); // LCD + uLCD.printf("\n\rJoe's Thermostat\n\r"); // LCD mySpeaker.PlayNote(500.0, 1.0, 1.0); // Speaker buzz - myShiftbrite.write( 0, 50 ,0); // Green RGB LED +// myShiftbrite.write( 0, 50 ,0); // Green RGB LED // SD card write file example - prints error message on PC when running until SD card hooked up // Delete to avoid run time error - mkdir("/sd/mydir", 0777); // set up directory and permissions - FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); //open SD - if(fp == NULL) { - error("Could not open file for write\n"); - } - fprintf(fp, "Hello SD Card World!"); // write SD - fclose(fp); // close SD card +// mkdir("/sd/mydir", 0777); // set up directory and permissions +// FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); //open SD +// if(fp == NULL) { +// error("Could not open file for write\n"); +// } +// fprintf(fp, "Hello SD Card World!"); // write SD +// fclose(fp); // close SD card // // end I/O examples - - + enum Statetype { Off = 0, Heat_off, Heat_on, Cool_off, Cool_on }; + Statetype state = Off; + uLCD.locate(3,6); + uLCD.printf("OFF "); // State machine code below will need changes and additions while (1) { - { - enum Statetype { Heat_off = 0, Heat_on }; - Statetype state = Heat_off; - while(1) { + + while(1) { + Prior_temp = tempF; + Current_temp = myTMP36; //Read temp sensor + tempF = (9.0*Current_temp)/5.0+ 32.0; + double tempC = Current_temp; + + + switch (mode) { + case (0): + uLCD.locate(3,6); + uLCD.printf("OFF "); + //target_temp = 75; + break; + case (1): + uLCD.locate(3,6); + uLCD.color(RED); + uLCD.printf("HEAT "); + uLCD.color(GREEN); + //if (centigrade == 0) + // target_temp = heat_setpoint; + //else + // target_temp = heat_setpoint_C; + break; + case (2): + uLCD.locate(3,6); + uLCD.color(LBLUE); + uLCD.printf("COOL "); + uLCD.color(GREEN); + // if (centigrade == 0) + // target_temp = cool_setpoint; + //else + // target_temp = cool_setpoint_C; + break; + } + + + + if (centigrade == 1) + tempF = tempC; + //tempF = round(tempF); + + uLCD.locate(2,10); + if (centigrade == 0) + { + uLCD.text_width(2); + uLCD.text_height(2); + uLCD.printf("%5.0F F \n\r", tempF); + uLCD.text_width(1); + uLCD.text_height(1); + } + if (centigrade == 1) + { + uLCD.text_width(2); + uLCD.text_height(2); + uLCD.printf("%5.0F C \n\r", tempC); + uLCD.text_width(1); + uLCD.text_height(1); + } + + if (mode == 0) + { + state = Off; + greenpin = 1; + redpin = 0; + bluepin = 0; + } + if (mode == 1) + { + if (tempF > target_temp + 1) + { + if (Prior_temp < target_temp + 1) + { + mySpeaker.PlayNote(500.0, 0.025, 1.0); + } + greenpin = 0; + redpin = 0; + bluepin = 0; + state = Heat_off; + } + if (tempF < target_temp - 1) + { + if (Prior_temp > target_temp - 1) + { + mySpeaker.PlayNote(500.0, 0.025, 1.0); + } + greenpin = 0; + redpin = 1; + bluepin = 0; + state = Heat_on; + } + } + if (mode == 2) + { + if (tempF > target_temp + 1) + { + if (Prior_temp < target_temp + 1) + { + mySpeaker.PlayNote(1000.0, 0.025, 1.0); + } + greenpin = 0; + redpin = 0; + bluepin = 1; + state = Cool_on; + } + if (tempF < target_temp - 1) + { + if (Prior_temp > target_temp - 1) + { + mySpeaker.PlayNote(1000.0, 0.025, 1.0); + } + greenpin = 0; + redpin = 0; + bluepin = 0; + state = Cool_off; + } + } + uLCD.locate(3,5); + if (mode == 1) { + uLCD.color(RED); + uLCD.printf("Heat to: %2d", target_temp); + uLCD.color(GREEN); + myLED1 = 1; + myLED2 = 0; } + if (mode == 2) { + uLCD.color(LBLUE); + uLCD.printf("Cool to: %2d", target_temp); + uLCD.color(GREEN); + myLED1 = 0; + myLED2 = 1; } + if (mode == 0) { + uLCD.printf(" ", target_temp); + myLED1 = 0; + myLED2 = 0; } + switch (state) { case Heat_off: + myLED3 = 0; myLED4 = 0; - state = Heat_on; + //state = Heat_on; + uLCD.locate(3,4); + uLCD.printf("Heat Off"); break; case Heat_on: + myLED3 = 0; myLED4 = 1; - state = Heat_off; + //state = Heat_off; + uLCD.locate(3,4); + uLCD.color(RED); + uLCD.printf("Heat On "); + uLCD.color(GREEN); + break; + case Cool_off: + myLED3 = 0; + myLED4 = 0; + //state = Cool_on; + uLCD.locate(3,4); + uLCD.printf("Cool Off"); + break; + case Cool_on: + myLED3 = 1; + myLED4 = 0; + //state = Cool_off; + uLCD.locate(3,4); + uLCD.color(LBLUE); + uLCD.printf("Cool On "); + uLCD.color(GREEN); + break; + case Off: + myLED4 = 0; + uLCD.locate(3,4); + uLCD.printf(" "); break; } + wait(0.33); // heartbeat LED - common debug tool // blinks as long as code is running and not locked up - myLED1=!myLED1; - } + //myLED1=!myLED1; } } } \ No newline at end of file