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 12:33:22 2016 +0000
Parent:
0:55c7a177fdbb
Child:
2:26a7c23e3513
Commit message:
added spirit level
; added comments

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Feb 25 14:25:47 2016 +0000
+++ b/main.cpp	Fri Feb 26 12:33:22 2016 +0000
@@ -1,7 +1,27 @@
+/*
+ *  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 a 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.
+ *  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
+ *  
+ */
+
+
 #include "mbed.h"
-#include "LM75B.h"
-#include "C12832.h"
-#include "MMA7660.h" /* for the accelerometer */
+#include "LM75B.h"                      /* temperature */
+#include "C12832.h"                     /* LCD */
+#include "MMA7660.h"                    /* for the accelerometer */
 #include "reScale.h"
 
 DigitalOut r_led (LED1);                /* connections for RGB LED */
@@ -11,9 +31,9 @@
 PwmOut spkr(D6);                        /* speaker */
 
 //DigitalOut led(LED1);                   /* shield LEDs */
-DigitalOut green(D5);
+DigitalOut red(D5);
 DigitalOut blue(PTC12);
-DigitalOut red(D9);
+DigitalOut green(D9);
 
 InterruptIn sw2_int (PTC6);             /* interrupts for on-board switches */
 InterruptIn sw3_int (PTA4);
@@ -22,7 +42,6 @@
 InterruptIn joy_left (PTC11);
 InterruptIn joy_right (PTC10);
 InterruptIn joy_fire (PTB23);
-
 AnalogIn pot1 (A0);
 AnalogIn pot2 (A1);
 
@@ -30,7 +49,12 @@
 static volatile int sw3_trig;
 static volatile int joy_up_trig;
 static volatile int joy_down_trig;
+static volatile int joy_right_trig;
+static volatile int joy_left_trig;
 
+/*
+ * interrupt handlers
+ */
 void sw2_interrupt (void)
 {
     sw2_trig = 1;
@@ -51,118 +75,208 @@
     joy_down_trig = 1;
 }
 
-// Using Arduino pin notation
-C12832 lcd(D11, D13, D12, D7, D10);
-LM75B sensor(D14,D15);
+void joy_right_interrupt (void)
+{
+    joy_right_trig = 1;
+}
+
+void joy_left_interrupt (void)
+{
+    joy_left_trig = 1;
+}
+
+// pins
+C12832 lcd(D11, D13, D12, D7, D10); /* LCD */
+LM75B sensor(D14,D15); /* temperature sensor */
 MMA7660 MMA(D14, D15); /* accelerometer */
 
 int main ()
 {
-    double waitTime = 0.5;
-    int menu = 0;    
-    int maxTemp = 30;
-    double fahrenheit;
-    double tempC = sensor.temp();
-            
+    double waitTime = 0.5;                  /* value for wait function */
+    int menu = 0;                           /* menu */
+    int maxTemp = 30;                       /* value for the temperature alarm */
+    double fahrenheit;                      /* for calculating fahrenheit */
+    
+    float x, y, z;
+   
     sw2_trig = 0;
     sw3_trig = 0;
     joy_up_trig = 0;
     joy_down_trig = 0;
-    
+    joy_right_trig = 0;
+
     sw2_int.mode (PullUp);
     sw2_int.fall (&sw2_interrupt);
-    
+
     sw3_int.mode (PullUp);
     sw3_int.fall (&sw3_interrupt);
-    
+
     joy_up.mode (PullUp);
     joy_up.fall (&joy_up_interrupt);
-    
+
     joy_down.mode (PullUp);
     joy_down.fall (&joy_down_interrupt);
-        
+
+    joy_right.mode (PullUp);
+    joy_right.fall (&joy_right_interrupt);
+
+    joy_left.mode (PullUp);
+    joy_left.fall (&joy_left_interrupt);
+
     //fahrenheit = 9.00 / 5.00 * tempC + 32;
     //double fahrenheitPartOne = sensor.temp() * 1.8;
     //double fahrenheitPartTwo = fahrenheitPartOne + 32;
-        
+
     void tempAlarm();
     void noAlarm();
     void flash();
     void thermomCel(int n, int maxTemp);
-    
+    void testMmm(int xYo, int yYo, int zYo);
+
+    /*
+     * main loop
+     */
     while (1) {
-        
-    double tempDouble = static_cast<double>(sensor.temp());    
-    double fahrenheitPartOne = tempDouble * 1.8;
-    double fahrenheitPartTwo = fahrenheitPartOne + 32;    
-    int n = static_cast<int>(sensor.temp());
+
+        //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;
+        }
+    if (zTest>180) {
+        zTest = 180;
+        }
         
-        flash();
-                                               
-        if(sensor.temp()<maxTemp) {                   
-            spkr=0;                       
-            green = 7 & 4;
-            blue = 0 & 0;
-            red = 0 & 0;           
-            } 
-            
-            if(sensor.temp()< maxTemp && sensor.temp()> maxTemp - 2){                        
-            green = 0 & 0;
-            blue = 7 & 4;
-            red = 0 & 0;                        
-            }
-            
-            if(sensor.temp()>maxTemp) {               
-                tempAlarm();                
-            }
-                
-        if(menu==0) {
-        lcd.cls();
-        lcd.locate(0,3);
-        lcd.printf("Max Temp Alarm: %.1d", maxTemp);
-        thermomCel(n, maxTemp);
-        wait(waitTime);
+    int xTest = (xYo * 9) + 90;
+    if (xTest<0) {
+        xTest = 0;
+        }
+    if (xTest>180) {
+        xTest = 180;
+        }
+        
+    int yTest = (yYo * 9) + 90;
+    if (yTest<0) {
+        yTest = 0;
+        }
+    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 */
+        int n = static_cast<int>(sensor.temp());                    /* convert temperature to int value (used for thermometer display)*/
+
+        /*
+         *  blue LED if the temperature is lower than the alarm value
+         */
+        if(sensor.temp()<maxTemp) {
+            noAlarm();
         }
         
-        if(menu==1) {
-        lcd.cls();
-        lcd.locate(0,3);
-        lcd.printf("Celsius: %.1f\n", sensor.temp());
-        thermomCel(n, maxTemp);        
-        wait(waitTime);
+        /*
+         *  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;
+            blue = 7 & 4;
+            red = 0 & 0;
+        }
+        /*
+         *  alarm triggered if the temperature exceeds the temperature alarm value
+         */
+        if(sensor.temp()>maxTemp) {
+            tempAlarm();
         }
         
+        /*
+         *  display graphical thermometer and temperature alarm value 
+         *  on the first screen
+         */
+        if(menu==0) {
+            lcd.cls();
+            lcd.locate(0,3);
+            lcd.printf("Max Temp Alarm: %.1d", maxTemp);
+            thermomCel(n, maxTemp);
+            wait(waitTime);
+        }
+        
+        /*
+         *  display graphical thermometer and temperature in celsius 
+         *  on the second screen  
+         */
+        if(menu==1) {
+            lcd.cls();
+            lcd.locate(0,3);
+            lcd.printf("Celsius: %.1f\n", sensor.temp());
+            thermomCel(n, maxTemp);
+            wait(waitTime);
+        }
+        
+        /*
+         *  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
-        thermomCel(n, maxTemp);
-        wait(waitTime);
+            lcd.cls();
+            lcd.locate(0,3);
+            lcd.printf("Fahrenheit: %.1lf\n",fahrenheitPartTwo); //changed this to double
+            thermomCel(n, maxTemp);
+            wait(waitTime);
         }
-                
+
         if(menu==3) {
         lcd.cls();
-        lcd.locate(0,3); 
-        lcd.printf("MAG: X=%4.1f Y=%4.1f Z=%4.1f\r\n", MMA.x(), MMA.y(), MMA.z());  
-        wait(waitTime);    
+        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);
+        }
+
+        if(menu==4) {
+            lcd.cls();
+            lcd.locate(0,3);
+            lcd.printf("%.1f", waitTime);
+            wait(waitTime);
+        }
+
+        /*
+         *  pressing right trigger button cycles up through the screens
+         */
+        if (sw2_trig) {
+            menu++;
+            if(menu>4) {
+                menu = 4;
+            }
+            sw2_trig = 0;
         }
         
-        if(menu==4) {
-        lcd.cls();
-        lcd.locate(0,3); 
-        lcd.printf("%.1d", n);
-        wait(waitTime);    
-        }
-        
-        
-        if (sw2_trig) {            
-            menu++;
-            if(menu>4){
-                menu = 4;
-            }                      
-            sw2_trig = 0;     
-        }
-        
+        /*
+         *  pressing left trigger button cycles down through the screens
+         */
         if (sw3_trig) {
             menu--;
             if(menu<0) {
@@ -171,61 +285,107 @@
             sw3_trig = 0;
         }
         
+        /*
+         *  pressing up on the joystick increases the temperature alarm value by 1
+         */
         if(joy_up_trig) {
             maxTemp = maxTemp + 1;
             //set max value in here
             joy_up_trig = 0;
         }
         
+        /*
+         *  pressing down on the joystick decreases the temperature alarm value by 1
+         */
         if(joy_down_trig) {
             maxTemp = maxTemp - 1;
             //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
+            joy_right_trig = 0;
+        }
+        
+        /*
+         *  pressing left on the joystick decreases the wait value value by 0.25
+         */
+        if(joy_left_trig) {
+            waitTime = waitTime - 0.25;
+            if(waitTime<0.25) {
+                waitTime = 0.25;
+            }
+            //set min value in here
+            joy_left_trig = 0;
+        }
 
     }
-    
+
+}
+/*
+ *  turn the LED red and make an alarm noise
+ *  pot1 controls the speaker volume
+ */
+void tempAlarm()
+{
+    float volume = pot1;
+
+    for (float i=2000.0; i<5000.0; i+=100) {
+        spkr.period(5.0/i);
+        spkr=volume;
+        wait(0.01);
+    }
+
+    green = 1;
+    blue = 1;
+    red = 0;
 }
 
-void tempAlarm() 
+/*
+ * turns the LED blue and stops the speaker
+ */
+void noAlarm()
 {
-            float volume = pot1;
-            
-            for (float i=2000.0; i<5000.0; i+=100) {
-            spkr.period(5.0/i);
-            spkr=volume;  
-            wait(0.01); 
-            }
-             
-            green = 0;
-            blue = 1; 
-            red = 1;    
-}
-
-void noAlarm() 
-{
-            spkr=0;                        
-            green = 1;
-            blue = 0;
-            red = 1;
+    spkr=0;
+    green = 1;
+    blue = 0;
+    red = 1;
 }
 
 void flash()
 {
-        r_led = 0;
-        g_led = 1;
-        b_led = 1;   
-        //b_led = !b_led;
-        //wait(1.00);        
+    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.fillrect(60, 25, 60 + n, 30, 50);
-        lcd.circle(60, 19, 2, 50);
-        lcd.line(60, 25, 60, 23, 50);
-        lcd.line(60 + n, 25, 60 + n, 20, 50);
-        lcd.line(60 + maxTemp, 25, 60 + maxTemp, 20, 50);
+    lcd.rect(0, 25, 120, 30, 50);
+    lcd.fillrect(60, 25, 60 + n, 30, 50);
+    lcd.circle(60, 19, 2, 50);
+    lcd.line(60, 25, 60, 23, 50);
+    lcd.line(60 + n, 25, 60 + n, 20, 50);
+    lcd.line(60 + maxTemp, 25, 60 + maxTemp, 20, 50);
 }
 
+void testMmm(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);
+}