James Hughes JRH52

Dependencies:   C12832 FXOS8700Q Fader LM75B MMA7660 mbed reScale

Smart Thermometer + Spirit Level for the FRDMK64F Device

This program displays the temperature in Celsius and Fahrenheit and allows the user to set a "temperature alarm" that activates once the the temperature exceeds a temperature value specified by the user.

This program also displays graphical "spirit levels" showing degrees of rotaion along 3 axis.

The user can cycle between screens using sw2 and sw3 buttons, set the temperature alarm by pressing up and down on the joystick, control the volume of the alarm with the pot1 knob, and control the time value of the wait function by pressing left and right on the joystick.

Files at this revision

API Documentation at this revision

Comitter:
co838_jrh52
Date:
Fri Feb 26 14:00:19 2016 +0000
Parent:
1:829f4ff1fd05
Child:
3:8a1784883bde
Commit message:
made some minor changes

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Feb 26 12:33:22 2016 +0000
+++ b/main.cpp	Fri Feb 26 14:00:19 2016 +0000
@@ -1,20 +1,20 @@
 /*
  *  This program displays the temperature in Celsius and Fahrenheit
- *  and allows the user to set a "temperature alarm" that activates 
+ *  and allows the user to set a "temperature alarm" that activates
  *  once the the temperature exceeds a temperature value specified by the user.
  *  This program also displays a graphical "spirit levels" showing
  *  degrees of rotaion along 3 axis.
- *  The user can cycle between screens using sw2 and sw3 buttons, 
+ *  The user can cycle between screens using sw2 and sw3 buttons,
  *  set the temperature alarm by pressing up and down on the joystick,
  *  control the volume of the alarm with the pot1 knob, and control the
- *  time value of the wait function by pressing left and right on 
+ *  time value of the wait function by pressing left and right on
  *  the joystick.
  *  screen 1: max temperature alarm
  *  screen 2: temperature in Celsius
  *  screen 3: temperature in Fahrenheit
  *  screen 4: spirit level
  *  screen 5: value for wait function
- *  
+ *
  */
 
 
@@ -24,9 +24,9 @@
 #include "MMA7660.h"                    /* for the accelerometer */
 #include "reScale.h"
 
-DigitalOut r_led (LED1);                /* connections for RGB LED */
-DigitalOut g_led (LED2);
-DigitalOut b_led (LED3);
+//DigitalOut r_led (LED1);                /* connections for RGB LED */
+//DigitalOut g_led (LED2);
+//DigitalOut b_led (LED3);
 
 PwmOut spkr(D6);                        /* speaker */
 
@@ -96,9 +96,12 @@
     int menu = 0;                           /* menu */
     int maxTemp = 30;                       /* value for the temperature alarm */
     double fahrenheit;                      /* for calculating fahrenheit */
-    
+
     float x, y, z;
-   
+    int xInt;
+    int yInt;
+    int zInt;
+        
     sw2_trig = 0;
     sw3_trig = 0;
     joy_up_trig = 0;
@@ -129,60 +132,58 @@
 
     void tempAlarm();
     void noAlarm();
-    void flash();
     void thermomCel(int n, int maxTemp);
-    void testMmm(int xYo, int yYo, int zYo);
+    void spiritLev(int xYo, int yYo, int zYo);
 
     /*
      * main loop
      */
     while (1) {
 
-        //flash();
-    int xInt = static_cast<int>(MMA.x());    
-    int yInt = static_cast<int>(MMA.y());
-    int zInt = static_cast<int>(MMA.z());
-      
-    x = MMA.x();
-    y = MMA.y();
-    z = MMA.z(); 
-    
-    float xYo = x * 10;
-    float yYo = y * 10;
-    float zYo = z * 10; 
-    
-    xInt = static_cast<int>(xYo);    
-    yInt = static_cast<int>(yYo);
-    zInt = static_cast<int>(zYo);
-    
-    int zTest = (10 - zYo) * 9;
-    if (zTest<0) {
-        zTest = 0;
+        x = MMA.x();
+        y = MMA.y();
+        z = MMA.z();
+
+        float xMul = x * 10;
+        float yMul = y * 10;
+        float zMul = z * 10;
+        
+        /*
+         * used for graphical spirit levels
+         */
+        int xInt = static_cast<int>(xMul);
+        int yInt = static_cast<int>(yMul);
+        int zInt = static_cast<int>(zMul);
+
+        /*
+         * change into degrees of rotation
+         */
+        int zTest = (10 - zInt) * 9;
+        if (zTest<0) {
+            zTest = 0;
         }
-    if (zTest>180) {
-        zTest = 180;
+        if (zTest>180) {
+            zTest = 180;
         }
-        
-    int xTest = (xYo * 9) + 90;
-    if (xTest<0) {
-        xTest = 0;
+
+        int xTest = (xInt * 9) + 90;
+        if (xTest<0) {
+            xTest = 0;
         }
-    if (xTest>180) {
-        xTest = 180;
+        if (xTest>180) {
+            xTest = 180;
         }
-        
-    int yTest = (yYo * 9) + 90;
-    if (yTest<0) {
-        yTest = 0;
+
+        int yTest = (yInt * 9) + 90;
+        if (yTest<0) {
+            yTest = 0;
         }
-    if (yTest>180) {
-        yTest = 180;
-        }    
-        
+        if (yTest>180) {
+            yTest = 180;
+        }
 
         double tempDouble = static_cast<double>(sensor.temp());
-        double fahrenheitPartOne = tempDouble * 1.8;
-        double fahrenheitPartTwo = fahrenheitPartOne + 32;          /* convert temperature to fahrenheit value */
+        double fahrenheitPartOne = (tempDouble * 1.8) + 32;         /* convert temperature to fahrenheit value */          
         int n = static_cast<int>(sensor.temp());                    /* convert temperature to int value (used for thermometer display)*/
 
         /*
@@ -191,9 +192,9 @@
         if(sensor.temp()<maxTemp) {
             noAlarm();
         }
-        
+
         /*
-         *  orange LED if the temperature is lower than the alarm value within 2 degrees 
+         *  orange LED if the temperature is lower than the alarm value within 2 degrees
          */
         if(sensor.temp()< maxTemp && sensor.temp()> maxTemp - 2) {
             green = 0 & 0;
@@ -206,9 +207,9 @@
         if(sensor.temp()>maxTemp) {
             tempAlarm();
         }
-        
+
         /*
-         *  display graphical thermometer and temperature alarm value 
+         *  display graphical thermometer and temperature alarm value
          *  on the first screen
          */
         if(menu==0) {
@@ -218,10 +219,10 @@
             thermomCel(n, maxTemp);
             wait(waitTime);
         }
-        
+
         /*
-         *  display graphical thermometer and temperature in celsius 
-         *  on the second screen  
+         *  display graphical thermometer and temperature in celsius
+         *  on the second screen
          */
         if(menu==1) {
             lcd.cls();
@@ -230,36 +231,44 @@
             thermomCel(n, maxTemp);
             wait(waitTime);
         }
-        
+
         /*
-         *  display graphical thermometer and temperature in fahrenheit 
-         *  on the third screen  
+         *  display graphical thermometer and temperature in fahrenheit
+         *  on the third screen
          */
         if(menu==2) {
             lcd.cls();
             lcd.locate(0,3);
-            lcd.printf("Fahrenheit: %.1lf\n",fahrenheitPartTwo); //changed this to double
+            lcd.printf("Fahrenheit: %.1lf\n",fahrenheitPartOne); //changed this to fahrenheitPartOne
             thermomCel(n, maxTemp);
             wait(waitTime);
         }
-
+        
+        /*
+         *  display spirit level
+         *  on the fourth screen
+         */
         if(menu==3) {
-        lcd.cls();
-        lcd.locate(0,3);
-        testMmm(xYo, yYo, zYo);  
-        lcd.locate(20,0);
-        lcd.printf("%.1d", xTest);
-        lcd.locate(60,0);
-        lcd.printf("%.1d", yTest);
-        lcd.locate(100,0);
-        lcd.printf("%.1d", zTest);
-        wait(waitTime);
+            lcd.cls();
+            lcd.locate(0,3);
+            spiritLev(xMul, yMul, zMul);
+            lcd.locate(20,0);
+            lcd.printf("%.1d", xTest);
+            lcd.locate(60,0);
+            lcd.printf("%.1d", yTest);
+            lcd.locate(100,0);
+            lcd.printf("%.1d", zTest);
+            wait(waitTime);
         }
-
+        
+        /*
+         *  display wait time value
+         *  on the fifth screen
+         */
         if(menu==4) {
             lcd.cls();
             lcd.locate(0,3);
-            lcd.printf("%.1f", waitTime);
+            lcd.printf("Wait time value: %.1f", waitTime);
             wait(waitTime);
         }
 
@@ -273,7 +282,7 @@
             }
             sw2_trig = 0;
         }
-        
+
         /*
          *  pressing left trigger button cycles down through the screens
          */
@@ -284,7 +293,7 @@
             }
             sw3_trig = 0;
         }
-        
+
         /*
          *  pressing up on the joystick increases the temperature alarm value by 1
          */
@@ -293,7 +302,7 @@
             //set max value in here
             joy_up_trig = 0;
         }
-        
+
         /*
          *  pressing down on the joystick decreases the temperature alarm value by 1
          */
@@ -302,16 +311,16 @@
             //set min value in here
             joy_down_trig = 0;
         }
-        
+
         /*
          *  pressing right on the joystick increases the wait value value by 0.25
          */
         if(joy_right_trig) {
             waitTime = waitTime + 0.25;
-            //set min value in here
+            //set max value in here
             joy_right_trig = 0;
         }
-        
+
         /*
          *  pressing left on the joystick decreases the wait value value by 0.25
          */
@@ -319,8 +328,7 @@
             waitTime = waitTime - 0.25;
             if(waitTime<0.25) {
                 waitTime = 0.25;
-            }
-            //set min value in here
+            }            
             joy_left_trig = 0;
         }
 
@@ -357,21 +365,12 @@
     red = 1;
 }
 
-void flash()
-{
-    r_led = 0;
-    g_led = 1;
-    b_led = 1;
-    //b_led = !b_led;
-    //wait(1.00);
-}
-
 /*
  *  display the graphical thermometer
  */
 void thermomCel(int n, int maxTemp)
 {
-    lcd.rect(0, 25, 120, 30, 50);
+    lcd.rect(0, 25, 127, 30, 50);
     lcd.fillrect(60, 25, 60 + n, 30, 50);
     lcd.circle(60, 19, 2, 50);
     lcd.line(60, 25, 60, 23, 50);
@@ -379,13 +378,16 @@
     lcd.line(60 + maxTemp, 25, 60 + maxTemp, 20, 50);
 }
 
-void testMmm(int xYo, int yYo, int zYo)
+/*
+ *  display the spirit level
+ */
+void spiritLev(int xYo, int yYo, int zYo)
 {
-        lcd.rect(20, 10, 30, 30, 50);
-        lcd.fillrect(20, 20, 30, 20 - xYo, 50);
-        lcd.rect(60, 10, 70, 30, 50);
-        lcd.fillrect(60, 20, 70, 20 - yYo, 50);
-        lcd.rect(100, 10, 110, 30, 50);
-        lcd.fillrect(100, 30 - (10 - zYo), 110, 30, 50);
-        //lcd.fillrect(60, 25, 60 + n, 30, 50);
+    lcd.rect(20, 10, 30, 30, 50);
+    lcd.fillrect(20, 20, 30, 20 - xYo, 50);
+    lcd.rect(60, 10, 70, 30, 50);
+    lcd.fillrect(60, 20, 70, 20 - yYo, 50);
+    lcd.rect(100, 10, 110, 30, 50);
+    lcd.fillrect(100, 30 - (10 - zYo), 110, 30, 50);
+    //lcd.fillrect(60, 25, 60 + n, 30, 50);
 }