Export for Dan

Dependencies:   mbed MODSERIAL1

Revision:
5:14a21c3812ed
Parent:
4:810c3971bb3e
Child:
6:aa3219001d18
--- a/main.cpp	Thu Oct 29 20:18:49 2015 +0000
+++ b/main.cpp	Thu Oct 29 23:04:14 2015 +0000
@@ -2,6 +2,7 @@
 
 #include "mbed.h"
 #include "MODSERIAL.h"
+#include "HIH_5030.h"
 
 #define MESSAGE_BUFFER_SIZE 100
 
@@ -9,11 +10,16 @@
 DigitalOut led2(LED2);
 DigitalOut led3(LED3);
 DigitalOut led4(LED4);
+AnalogIn ain_UseA(p15);
+//AnalogIn ain_UseB(p16); //pins 16-19 are tied to ground to reduce noise
+//AnalogIn ain_UseC(p17);
+//AnalogIn ain_UseD(p18);
+//AnalogIn ain_UseE(p19);
 
 MODSERIAL messageSystem(p13, p14);
 
-//char messageBufferIncoming[MESSAGE_BUFFER_SIZE];
-int hexBufferIncoming[MESSAGE_BUFFER_SIZE];
+unsigned char messageBufferIncoming[MESSAGE_BUFFER_SIZE];
+unsigned int hexBufferIncoming[MESSAGE_BUFFER_SIZE];
 unsigned char messageBufferOutgoing[MESSAGE_BUFFER_SIZE];
 int count = 0;
 int i = 0;
@@ -27,31 +33,23 @@
     //dereference rxGetLasChar() of sys object 
     unsigned int c = sys->rxGetLastChar(); // Returns the last byte to pass through the RX interrupt handler.
     
-    //add char c to messageBufferIncoming
+    //add the character that triggered the interrupt to the incoming buffers
+    //I'm adding to an in buffer and a char buffer, but using the int buffer to determine which message it is
+        //if I want to use the char buffer to determine, then I need to use strcmp instead of =
     if(i <=MESSAGE_BUFFER_SIZE){
-     //   messageBufferIncoming[i] = c;
+        messageBufferIncoming[i] = c;
         hexBufferIncoming[i] = c;
-
-      // if (c == 0xAA) led4 = !led4;
-      //  led3 = !led3;
         i++;
-       
-        }
-        
+        }     
     count++;
-    if (count == 4){ //Need to be able to find the string anywhere in the buffer
+    if (count == 4){ //TODO: Determine if I need to be able to find a message anywhere within a bigger buffer
          //led4 = !led4;
-         wait(.05);
-
             if (hexBufferIncoming[0] == 0xAA){              //Start Calibration
-           // led1 = !led1;
                 if (hexBufferIncoming[1] == 0x55){
-             //       led2 = !led2;
                     if (hexBufferIncoming[2] == 0xFF){
-               //         led3 = !led3;
                         if (hexBufferIncoming[3] == 0x00){
                             led1 = !led1;
-                            //Add disable interrupt??
+                            //TODO: Determine if I need to add disable interrupt.  What happens if I receive while transmitting???
                             messageSystem.printf("%c", messageBufferOutgoing[0]= 0xAB);              
                             messageSystem.printf("%c", messageBufferOutgoing[1] = 0x54);
                             messageSystem.printf("%c", messageBufferOutgoing[2] = 0xFF);
@@ -141,7 +139,7 @@
                         }
                     }
                 }      
-        // memset(messageBufferIncoming, '\0', sizeof(messageBufferIncoming));
+         memset(messageBufferIncoming, '\0', sizeof(messageBufferIncoming));
          memset(hexBufferIncoming, '\0', sizeof(hexBufferIncoming));
          memset(messageBufferOutgoing, '\0', sizeof(messageBufferOutgoing));
          i = 0;
@@ -153,15 +151,73 @@
 
 void messageProcess(void) { 
      //  led1 = !led1;
-
     messageReceived = false;
 }
+/*
+Constructor
+dataPin: the IO pin connected to the sensor's data pin (pin 15)
+supplyVoltage: the voltage supplying the humidity sensor (pins 1,3 - for the HIH5030 3.3 V typical)
+referenceVoltage: motor controller's reference voltage (3.3V for the LPC1768)
+*/
 
+HIH5030::HIH5030(float dataPin, float supplyVoltage, float referenceVoltage){
+
+pin = dataPin;
+vSupply = supplyVoltage;
+vRef = referenceVoltage;
+
+/*
+Relative Humidity is calculated using the following equations taken from the datasheet:
+(1) Vout = (VSupply)(0.00636(sensorRH) + 0.1515)
+(2) sensorRH = (Vout - zeroOffset) / slope
+
+Solving (1) for sensorRH: 
+sensorRH = (Vout - (0.1515)VSupply) / (0.00636)VSupply
+
+Equate result with (2):
+zeroOffset = (0.1515)VSupply
+slope = (0.00636)VSupply
+*/ 
+
+slope = 0.00636 * vSupply;
+zeroOffset = 0.1515 * vSupply; 
+
+}
+
+
+/*
+Convert sensor reading into relative humidity using equation (2)
+*/ 
+
+float HIH5030::getSensorRH() {
+return ((vout() - zeroOffset) / slope);
+
+}
+
+/*
+Get temperature-compensated relative humity. From data sheet: 
+trueRH = sensorRH / (1.0546 - 0.00216T)
+*/
+
+float HIH5030::getTrueRH(float temperature) { 
+return getSensorRH() / (1.0546 - (0.00216 * temperature)); 
+
+}
+
+/*
+Get sensor output voltage.
+Assumes 12-bit (2^16 = 4096) A/D resolution.
+*/
+
+float HIH5030::vout() {
+return (float)(ain_UseA.read_u16()) * 3.3 / 65536;
+
+}
 
 int main() {
 
     messageReceived = false;
-   // memset(messageBufferIncoming, '\0', sizeof(messageBufferIncoming));
+    memset(messageBufferIncoming, '\0', sizeof(messageBufferIncoming));
     memset(hexBufferIncoming, '\0', sizeof(hexBufferIncoming));
     memset(messageBufferOutgoing, '\0', sizeof(messageBufferOutgoing));
     messageSystem.baud(9600);
@@ -173,10 +229,27 @@
     while(1) {
         //led1 = !led1;
         wait(.05);
+        float ad[5];
+    float supplyvoltage = 3.3;
+    float referencevoltage = 3.3;
+    float vdiv = (3.3 / 65536); //3.3 is the reference voltage (AnalogIn measures from 0V to 3.3V) and 65536 is the highest number that can be represented by a 16 bit unsigned
+    while( 1 ){
+        ad[0] = (float)ain_UseA.read_u16() * vdiv;  //16-bit unsigned short representing the current input voltage, normalised to a 16-bit value 
+      //  ad[1] = (float)ain_UseB.read_u16() * vdiv;
+      //  ad[2] = (float)ain_UseC.read_u16() * vdiv;
+      //  ad[3] = (float)ain_UseD.read_u16() * vdiv;
+      //  ad[4] = (float)ain_UseE.read_u16() * vdiv;
+        //pc.printf("%5.3f,%5.3f,%5.3f,%5.3f,%5.3f\r\n ", ad[0],ad[1],ad[2],ad[3],ad[4]);
+        HIH5030 humidity(ad[0], supplyvoltage, referencevoltage);
+        messageSystem.printf("%5.3f\r\nHumidity: ", humidity.getSensorRH());
+    led3 = 1;
+    wait(2.0);
+    led3 = 0;
+    wait(2.0);
+    }
         if (messageReceived)
         {
         //led2 = !led2;
-        wait(.05);
         messageProcess();
          }
     }