Bluetooth module HC06 interfaced with KL25Z

Dependencies:   MMA8451Q TSI mbed-src

Fork of KL25Z_BT_HC06 by Moorthy Muthukrishnan

Committer:
nmoorthy2001
Date:
Tue Feb 14 08:33:59 2017 +0000
Revision:
1:013f244c937a
Parent:
0:5a3e0b8133dc
Bluetooth module HC06 interfaced with KL25Z

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jppang 0:5a3e0b8133dc 1 #include "mbed.h"
jppang 0:5a3e0b8133dc 2 #include "MMA8451Q.h"
jppang 0:5a3e0b8133dc 3 #include <cstdlib>
jppang 0:5a3e0b8133dc 4 #include <iostream>
jppang 0:5a3e0b8133dc 5
nmoorthy2001 1:013f244c937a 6 Serial HC06(PTC4,PTC3);
nmoorthy2001 1:013f244c937a 7 AnalogIn analog_value(A0);
nmoorthy2001 1:013f244c937a 8 DigitalOut buz(PTD7);
nmoorthy2001 1:013f244c937a 9
nmoorthy2001 1:013f244c937a 10 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
nmoorthy2001 1:013f244c937a 11 PinName const SDA = PTE25;
nmoorthy2001 1:013f244c937a 12 PinName const SCL = PTE24;
nmoorthy2001 1:013f244c937a 13 #elif defined (TARGET_KL05Z)
nmoorthy2001 1:013f244c937a 14 PinName const SDA = PTB4;
nmoorthy2001 1:013f244c937a 15 PinName const SCL = PTB3;
nmoorthy2001 1:013f244c937a 16 #elif defined (TARGET_K20D50M)
nmoorthy2001 1:013f244c937a 17 PinName const SDA = PTB1;
nmoorthy2001 1:013f244c937a 18 PinName const SCL = PTB0;
nmoorthy2001 1:013f244c937a 19
nmoorthy2001 1:013f244c937a 20 #else
jppang 0:5a3e0b8133dc 21
nmoorthy2001 1:013f244c937a 22 #error TARGET NOT DEFINED
nmoorthy2001 1:013f244c937a 23 #endif
nmoorthy2001 1:013f244c937a 24 #define MMA8451_I2C_ADDRESS (0x1d<<1)
nmoorthy2001 1:013f244c937a 25
nmoorthy2001 1:013f244c937a 26 int main(void)
nmoorthy2001 1:013f244c937a 27 {
nmoorthy2001 1:013f244c937a 28 HC06.baud(9600);
jppang 0:5a3e0b8133dc 29
nmoorthy2001 1:013f244c937a 30 float meas;
nmoorthy2001 1:013f244c937a 31 printf("\nAnalogIn example\n");
nmoorthy2001 1:013f244c937a 32 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
nmoorthy2001 1:013f244c937a 33
nmoorthy2001 1:013f244c937a 34 PwmOut rled(LED1);
nmoorthy2001 1:013f244c937a 35 PwmOut gled(LED2);
nmoorthy2001 1:013f244c937a 36 PwmOut bled(LED3);
nmoorthy2001 1:013f244c937a 37
nmoorthy2001 1:013f244c937a 38 printf("MMA8451 ID: %d\n", acc.getWhoAmI());
jppang 0:5a3e0b8133dc 39 using namespace std;
nmoorthy2001 1:013f244c937a 40
nmoorthy2001 1:013f244c937a 41 while(1)
nmoorthy2001 1:013f244c937a 42 {
nmoorthy2001 1:013f244c937a 43 printf("\nAnalogIn example\n");
nmoorthy2001 1:013f244c937a 44 while (true)
nmoorthy2001 1:013f244c937a 45 {
nmoorthy2001 1:013f244c937a 46 float x, y, z;
nmoorthy2001 1:013f244c937a 47 x = abs(acc.getAccX());
nmoorthy2001 1:013f244c937a 48 y = abs(acc.getAccY());
nmoorthy2001 1:013f244c937a 49 z = abs(acc.getAccZ());
nmoorthy2001 1:013f244c937a 50 rled = 1.0f - x;
nmoorthy2001 1:013f244c937a 51 gled = 1.0f - y;
nmoorthy2001 1:013f244c937a 52 bled = 1.0f - z;
nmoorthy2001 1:013f244c937a 53 wait(0.1f);
nmoorthy2001 1:013f244c937a 54 printf("X: %1.2f, Y: %1.2f, Z: %1.2f\r\n", x, y, z);
nmoorthy2001 1:013f244c937a 55 HC06.printf("\nX: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z);
nmoorthy2001 1:013f244c937a 56
nmoorthy2001 1:013f244c937a 57 meas = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
nmoorthy2001 1:013f244c937a 58 meas = meas * 3300; // Change the value to be in the 0 to 3300 range
nmoorthy2001 1:013f244c937a 59 printf("measure = %1.0f mV\r\n\n\n", meas);
nmoorthy2001 1:013f244c937a 60 HC06.printf("measure = %1.0f mV\n", meas);
nmoorthy2001 1:013f244c937a 61 if(x > 0.8){buz = 1; goto ex;}else {buz = 0;}
nmoorthy2001 1:013f244c937a 62 if(y > 0.8){buz = 1; goto ex;}else {buz = 0;}
nmoorthy2001 1:013f244c937a 63 if(z < 0.5){buz = 1; goto ex;}else {buz = 0;}
nmoorthy2001 1:013f244c937a 64
nmoorthy2001 1:013f244c937a 65 if (meas < 1850)// If the value is greater than 2V then switch the LED on
nmoorthy2001 1:013f244c937a 66 buz = 1;
nmoorthy2001 1:013f244c937a 67 else
nmoorthy2001 1:013f244c937a 68 buz = 0;
nmoorthy2001 1:013f244c937a 69 ex: wait(0.2); // 200 ms
nmoorthy2001 1:013f244c937a 70
jppang 0:5a3e0b8133dc 71 }
nmoorthy2001 1:013f244c937a 72
jppang 0:5a3e0b8133dc 73 }
nmoorthy2001 1:013f244c937a 74
jppang 0:5a3e0b8133dc 75 }