Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 5:8582a28cf944, committed 2019-01-17
- Comitter:
- nthompson22
- Date:
- Thu Jan 17 06:04:45 2019 +0000
- Parent:
- 4:19878f73f3e3
- Commit message:
- Final Product
Changed in this revision
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Tap.cpp	Thu Jan 17 06:04:45 2019 +0000
@@ -0,0 +1,77 @@
+#include "Tap.h"
+
+I2C tapI2C(p9, p10);
+InterruptIn tapInterrupt(p5);
+char buff[2];
+const int accelAddr = 0x53 << 1;
+volatile int i;
+
+void tapsConfig(void) {  //configures the settings for the accelerometer (same settings as mini project 6 converted to I2C)
+    buff[0] = 0x1D;
+    buff[1] = 80;
+    tapI2C.write(accelAddr, buff, 2);
+    wait(0.02);
+    buff[0] = 0x21;  
+    buff[1] = 0x10;
+    tapI2C.write(accelAddr, buff, 2);
+    wait(0.02);
+    buff[0] = 0x22;
+    buff[1] = 0x05;
+    tapI2C.write(accelAddr, buff, 2);
+    wait(0.02);
+    buff[0] = 0x23;
+    buff[1] = 0xFF;
+    tapI2C.write(accelAddr, buff, 2);
+    wait(0.02);
+    buff[0] = 0x2A;
+    buff[1] = 0x07;
+    tapI2C.write(accelAddr, buff, 2);
+    wait(0.02);
+    buff[0] = 0x2E;
+    buff[1] = 0x60;
+    tapI2C.write(accelAddr, buff, 2);
+    wait(0.02);
+    buff[1] = 0x2F;
+    buff[0] = 0x60;
+    tapI2C.write(accelAddr, buff, 2);
+    wait(0.02);
+    buff[0] = 0x2D;
+    buff[1] = 0x08;
+    tapI2C.write(accelAddr, buff, 2);
+    wait(0.02);
+    tapInterrupt.rise(&tapsHappened);
+    tapsHappened(); // resets the interrupt, same as mini project 6
+}
+
+void tapsHappened(void) {
+    char tapByte;
+    bool absol = true;
+    bool unit = true;
+    buff[0]=0x30;
+    tapI2C.write(accelAddr,buff,1);
+    tapI2C.read(accelAddr,buff,1);
+    tapByte = buff[0]; // gets a value for tapByte
+    if (tapByte & 0x40) { //based on the value of tapByte, determines if there was a single or double tap
+        unit = !unit; // changes the value of unit and absol based on the number of taps
+    }
+    if (tapByte & 0x20) { 
+        absol = !absol;
+    }
+    if (unit == 1 && absol == 1) { /* based on the values of unit and absol, determines the value of i,
+    which is used to determine the units of the temperature displayed */
+        i = 0;
+    }
+    if (unit == 1 && absol == 0) {
+        i = 1;
+    }
+    if (unit == 0 && absol == 1) {
+        i = 2;
+    }
+    if (unit == 0 && absol == 0) {
+        i = 3;
+    }
+}
+int getSetting(int i){ /* the value of i is set in a void function (which must be void because it is an interrupt)
+This function is an int function that returns the value of i, allowing us to call it for the value of i from main.cpp */
+    return i;
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Tap.h Thu Jan 17 06:04:45 2019 +0000 @@ -0,0 +1,9 @@ +#ifndef TAP_H +#define TAP_H +#include "mbed.h" +extern I2C tapI2C; +extern InterruptIn tapInterrupt; +void tapsHappened(void); +void tapsConfig(void); +int getSetting(int i); +#endif \ No newline at end of file
--- a/Temperature.cpp	Thu Jan 17 00:12:40 2019 +0000
+++ b/Temperature.cpp	Thu Jan 17 06:04:45 2019 +0000
@@ -1,35 +1,43 @@
 #include "Temperature.h"
 I2C i2cTemp(p9, p10);
+volatile short rawTemp;
     
-float tempFar(float temp) {
+float tempFar(float temp) { // converts temperature to farenheit
     float far;
-    far = (temp*9/5)+32;
+    far = (((0.0625 * rawTemp)*(9/5))+32);
     return far;
 }
 
-float tempRan(float temp) {
+float tempRan(float temp) { // converts temperature to rankine
     float ran;
-    ran = (temp*9/5)+491.67;
+    ran = (((0.0625 * rawTemp)*(9/5))+491.67);
     return ran;
 }
 
-float tempKel(float temp){
+float tempKel(float temp){ // cocnverts temperature to kelvin
     float kel;
-    kel = temp+273;
+    kel = ((0.0625 * rawTemp)+273);
     return kel;
 }
 
-void tempConfig(void) {
-    
+void tempConfig(void) { // configures the correct settings for the temperature sensor when the program starts
+    char buff[3];
+    const int tempAddr = 0x90;
+    buff[0] = 0x01;
+    buff[1] = 0x60;
+    buff[2] = 0xA0;
+    i2cTemp.write(tempAddr, buff, 3);
+    buff[0] = 0x00;
+    i2cTemp.write(tempAddr, buff, 1);    
 }
 
-float readTemp(rawTemp) {
-    short rawTemp;
+float readTemp() { // converts rawTemp (the raw data from the temperature sensor) to degrees celsius
     float temp;
     char buff[2];
     const int tempAddr = 0x90;
-    tempSensor.read(tempAddr, buff, 2);
+    i2cTemp.read(tempAddr, buff, 2);
     rawTemp = (buff[0] <<8) + buff[1];
     rawTemp = rawTemp >> 4;
     temp = 0.0625 * rawTemp;
+    return temp;
 }
\ No newline at end of file
--- a/Temperature.h Thu Jan 17 00:12:40 2019 +0000 +++ b/Temperature.h Thu Jan 17 06:04:45 2019 +0000 @@ -1,10 +1,10 @@ #ifndef TEMPERATURE_H #define TEMPERATURE_H #include "mbed.h" -extern i2cTemp; +extern I2C i2cTemp; float tempFar(float temp); float tempRan(float temp); float tempKel(float temp); void tempConfig(void); -float readTemp(float rawTemp); +float readTemp(); #endif \ No newline at end of file
--- a/main.cpp	Thu Jan 17 00:12:40 2019 +0000
+++ b/main.cpp	Thu Jan 17 06:04:45 2019 +0000
@@ -1,6 +1,29 @@
 #include "mbed.h"
+#include "Temperature.h"
 #include "TextLCD.h"
+#include "Tap.h"
+
+TextLCD lcd(p19, p20, p21, p22, p23, p24);
 
-main() {
-    
+int main() {
+    tempConfig();
+    tapsConfig();
+    float temp;
+    int i;
+    while(1) {
+        i = getSetting(i); // gets new value of i when an interrupt (tapping) changes it
+        switch(i) { // converts to correct units based on the value of i
+            case 0: temp = readTemp();
+            break;
+            case 1: temp = tempKel(temp);
+            break;
+            case 2: temp = tempFar(temp);
+            break;
+            case 3: temp = tempRan(temp);
+            break;
+        }
+        lcd.printf("%1.2f",temp); // display temperature on the lcd
+        wait(1); // updates the temperature every one second
+        lcd.cls();
+    }
 }
\ No newline at end of file
    