Device 1 for Iot group project.

Dependencies:   C12832 FXOS8700Q LM75B MMA7660 mbed

Committer:
co657_mh560
Date:
Mon Jan 25 20:52:49 2016 +0000
Revision:
1:9a7edbd937fd
Parent:
0:41a47fd5d1cb

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
co657_mh560 0:41a47fd5d1cb 1 #include "mbed.h"
co657_mh560 0:41a47fd5d1cb 2 #include "LM75B.h" /* On sheild Temp sensor */
co657_mh560 0:41a47fd5d1cb 3 #include "C12832.h" /* On sheild LCD */
co657_mh560 0:41a47fd5d1cb 4 #include "FXOS8700Q.h"
co657_mh560 0:41a47fd5d1cb 5
co657_mh560 0:41a47fd5d1cb 6 PwmOut speaker (D6);
co657_mh560 0:41a47fd5d1cb 7 Serial host (USBTX, USBRX);
co657_mh560 0:41a47fd5d1cb 8 LM75B temp (D14, D15);
co657_mh560 0:41a47fd5d1cb 9 C12832 lcd (D11, D13, D12, D7, D10);
co657_mh560 0:41a47fd5d1cb 10 Serial xbee (D1, D0);
co657_mh560 1:9a7edbd937fd 11 DigitalOut green_led_shield (PTA2); /**/
co657_mh560 1:9a7edbd937fd 12 DigitalOut red_led_shield (PTC4); /* Shield LEDs */
co657_mh560 1:9a7edbd937fd 13 DigitalOut blue_led_shield (PTA0); /**/
co657_mh560 1:9a7edbd937fd 14 I2C i2c(PTE25, PTE24);
co657_mh560 1:9a7edbd937fd 15 FXOS8700QMagnetometer mag(i2c, FXOS8700CQ_SLAVE_ADDR1);
co657_mh560 0:41a47fd5d1cb 16
co657_mh560 1:9a7edbd937fd 17 //Global variables
co657_mh560 1:9a7edbd937fd 18 char input = '0';
co657_mh560 0:41a47fd5d1cb 19 char alert = 'A'; // degree
co657_mh560 1:9a7edbd937fd 20 char warn = 'W'; // temp
co657_mh560 1:9a7edbd937fd 21 char history = '0';
co657_mh560 0:41a47fd5d1cb 22 float x,y,z,tp, degree;
co657_mh560 0:41a47fd5d1cb 23
co657_mh560 1:9a7edbd937fd 24 /* Makes the LED red */
co657_mh560 1:9a7edbd937fd 25 void makeLEDRed (void)
co657_mh560 0:41a47fd5d1cb 26 {
co657_mh560 1:9a7edbd937fd 27 green_led_shield = 0;
co657_mh560 1:9a7edbd937fd 28 red_led_shield = 255;
co657_mh560 1:9a7edbd937fd 29 blue_led_shield = 0;
co657_mh560 1:9a7edbd937fd 30 }
co657_mh560 1:9a7edbd937fd 31
co657_mh560 1:9a7edbd937fd 32 /* Makes the LED green */
co657_mh560 1:9a7edbd937fd 33 void makeLEDGreen (void)
co657_mh560 1:9a7edbd937fd 34 {
co657_mh560 1:9a7edbd937fd 35 green_led_shield = 255;
co657_mh560 1:9a7edbd937fd 36 red_led_shield = 0;
co657_mh560 1:9a7edbd937fd 37 blue_led_shield = 0;
co657_mh560 0:41a47fd5d1cb 38 }
co657_mh560 0:41a47fd5d1cb 39
co657_mh560 0:41a47fd5d1cb 40 int main()
co657_mh560 0:41a47fd5d1cb 41 {
co657_mh560 0:41a47fd5d1cb 42 host.baud(38400);
co657_mh560 1:9a7edbd937fd 43 makeLEDGreen();
co657_mh560 0:41a47fd5d1cb 44 float anti = abs(degree);
co657_mh560 1:9a7edbd937fd 45 mag.enable();
co657_mh560 0:41a47fd5d1cb 46 for(;;)
co657_mh560 0:41a47fd5d1cb 47 {
co657_mh560 1:9a7edbd937fd 48 //Gather the data from the magnetometer
co657_mh560 1:9a7edbd937fd 49 motion_data_counts_t mag_raw;
co657_mh560 1:9a7edbd937fd 50 mag.getAxis(mag_raw);
co657_mh560 1:9a7edbd937fd 51 float magx;
co657_mh560 1:9a7edbd937fd 52 float magy;
co657_mh560 1:9a7edbd937fd 53 float degree = abs(atan2(magx,magy)*180*7/22);
co657_mh560 1:9a7edbd937fd 54 mag.getX(magx);
co657_mh560 1:9a7edbd937fd 55 mag.getY(magy);
co657_mh560 1:9a7edbd937fd 56 //Print the data onto the LCD
co657_mh560 0:41a47fd5d1cb 57 lcd.cls();
co657_mh560 1:9a7edbd937fd 58 lcd.locate(1,1);
co657_mh560 1:9a7edbd937fd 59 lcd.printf("Magnetometer Degree: %.3f", degree);
co657_mh560 1:9a7edbd937fd 60 lcd.locate(1,11);
co657_mh560 1:9a7edbd937fd 61 tp = temp.read();
co657_mh560 1:9a7edbd937fd 62 lcd.printf("Temperature: %.3f C", tp);
co657_mh560 1:9a7edbd937fd 63 host.printf("%2.1f%3.0f%c\r\n", tp,degree,input);
co657_mh560 1:9a7edbd937fd 64 xbee.printf("%2.1f%3.0f%c", tp,degree,input);
co657_mh560 1:9a7edbd937fd 65 wait_ms(500);
co657_mh560 1:9a7edbd937fd 66 input = '0';
co657_mh560 1:9a7edbd937fd 67
co657_mh560 0:41a47fd5d1cb 68
co657_mh560 0:41a47fd5d1cb 69 // If the door moves, Send alert
co657_mh560 1:9a7edbd937fd 70 if (abs(anti-degree) > 160 )
co657_mh560 0:41a47fd5d1cb 71 {
co657_mh560 1:9a7edbd937fd 72 makeLEDRed();
co657_mh560 1:9a7edbd937fd 73 speaker.period(1/500);
co657_mh560 1:9a7edbd937fd 74 speaker=0.5;
co657_mh560 1:9a7edbd937fd 75
co657_mh560 1:9a7edbd937fd 76 input = alert;
co657_mh560 0:41a47fd5d1cb 77
co657_mh560 0:41a47fd5d1cb 78 }
co657_mh560 1:9a7edbd937fd 79
co657_mh560 1:9a7edbd937fd 80 // If the temp goes too high or too low, send warning
co657_mh560 0:41a47fd5d1cb 81 if (tp < 0 || tp > 45)
co657_mh560 0:41a47fd5d1cb 82 {
co657_mh560 1:9a7edbd937fd 83 makeLEDRed();
co657_mh560 1:9a7edbd937fd 84 speaker.period(1/500);
co657_mh560 1:9a7edbd937fd 85 speaker=0.5;
co657_mh560 1:9a7edbd937fd 86 input = warn;
co657_mh560 0:41a47fd5d1cb 87 }
co657_mh560 1:9a7edbd937fd 88
co657_mh560 0:41a47fd5d1cb 89 //Receiving char response
co657_mh560 0:41a47fd5d1cb 90 if(xbee.readable())
co657_mh560 0:41a47fd5d1cb 91 {
co657_mh560 1:9a7edbd937fd 92 input = xbee.getc();
co657_mh560 1:9a7edbd937fd 93 switch(input)
co657_mh560 1:9a7edbd937fd 94 {
co657_mh560 1:9a7edbd937fd 95 case 'y':
co657_mh560 1:9a7edbd937fd 96 makeLEDGreen();
co657_mh560 1:9a7edbd937fd 97 speaker=0;
co657_mh560 1:9a7edbd937fd 98 break;
co657_mh560 0:41a47fd5d1cb 99
co657_mh560 1:9a7edbd937fd 100 case 's':
co657_mh560 1:9a7edbd937fd 101 speaker=0;
co657_mh560 1:9a7edbd937fd 102 break;
co657_mh560 1:9a7edbd937fd 103 }
co657_mh560 0:41a47fd5d1cb 104 }
co657_mh560 0:41a47fd5d1cb 105 }
co657_mh560 1:9a7edbd937fd 106 }