Program that reads data from the FRDM-KL25Z capacitive slider and accellerometer and sends it to the serial port in JSON format.
Dependencies: MMA8451Q TSI mbed
Fork of FRDM_TSI by
This code is part of a project for the course in Computer Engineering "Progettazione di Applicazioni per Dispositivi Embedded (Designing of Applications for Embedded Devices)" , held by professor Omero Tuzzi, at the University of Trieste.
The code reads data from the board sensors (capacitive touch slider and accelerometer) and makes the RGB led assume a color according to the readings. It also sends a JSON-format message about the readings to the USB Serial Port.
main.cpp@7:ebf728afc308, 2014-09-21 (annotated)
- Committer:
- remarkus89
- Date:
- Sun Sep 21 16:48:20 2014 +0000
- Revision:
- 7:ebf728afc308
- Parent:
- 6:38459146ad89
Added comments to explain the code.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
remarkus89 | 7:ebf728afc308 | 1 | // Inclusion of necessary libraries |
emilmont | 0:0f00f07ebde0 | 2 | #include "mbed.h" |
chris | 1:51b1b688179a | 3 | #include "TSISensor.h" |
subhrajitmitra | 5:7f7b888f616b | 4 | #include "MMA8451Q.h" |
remarkus89 | 6:38459146ad89 | 5 | #include "stdio.h" |
subhrajitmitra | 5:7f7b888f616b | 6 | |
remarkus89 | 7:ebf728afc308 | 7 | // Declaration of constants |
subhrajitmitra | 5:7f7b888f616b | 8 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
remarkus89 | 6:38459146ad89 | 9 | #define ACC_THRESH 0.1 |
remarkus89 | 6:38459146ad89 | 10 | #define WAIT_TIME 0.01 |
emilmont | 0:0f00f07ebde0 | 11 | |
emilmont | 0:0f00f07ebde0 | 12 | int main(void) { |
remarkus89 | 7:ebf728afc308 | 13 | // Declare accelerometer |
subhrajitmitra | 5:7f7b888f616b | 14 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); |
subhrajitmitra | 5:7f7b888f616b | 15 | |
remarkus89 | 7:ebf728afc308 | 16 | // Declare components of the RGB led |
remarkus89 | 6:38459146ad89 | 17 | PwmOut led_blue(LED_BLUE); |
remarkus89 | 6:38459146ad89 | 18 | PwmOut led_green(LED_GREEN); |
remarkus89 | 6:38459146ad89 | 19 | PwmOut led_red(LED_RED); |
subhrajitmitra | 5:7f7b888f616b | 20 | |
remarkus89 | 7:ebf728afc308 | 21 | // Declare capacitive touch slider |
subhrajitmitra | 5:7f7b888f616b | 22 | float tp=0; |
subhrajitmitra | 5:7f7b888f616b | 23 | TSISensor tsi; |
remarkus89 | 7:ebf728afc308 | 24 | |
remarkus89 | 7:ebf728afc308 | 25 | // Declare USB Serial Port |
remarkus89 | 6:38459146ad89 | 26 | Serial pc(USBTX, USBRX); |
remarkus89 | 7:ebf728afc308 | 27 | |
remarkus89 | 7:ebf728afc308 | 28 | // Turn off all the components of the RGB led |
remarkus89 | 6:38459146ad89 | 29 | led_blue = 1; |
remarkus89 | 6:38459146ad89 | 30 | led_green = 1; |
remarkus89 | 6:38459146ad89 | 31 | led_red = 1; |
subhrajitmitra | 5:7f7b888f616b | 32 | |
subhrajitmitra | 5:7f7b888f616b | 33 | while (1) |
subhrajitmitra | 5:7f7b888f616b | 34 | { |
remarkus89 | 7:ebf728afc308 | 35 | // Read the slider and assign the readed value to the tp variable. |
remarkus89 | 7:ebf728afc308 | 36 | // tp assumes a value between 0 (user is not touching the slider) |
remarkus89 | 7:ebf728afc308 | 37 | // and 1 (user is touching the upper part of the slider). |
remarkus89 | 6:38459146ad89 | 38 | tp = tsi.readPercentage(); |
remarkus89 | 7:ebf728afc308 | 39 | |
remarkus89 | 7:ebf728afc308 | 40 | // If the user is NOT touching the slider, then make RGB led assume |
remarkus89 | 7:ebf728afc308 | 41 | // a color accourding to the declivity measured by the accelerometer. |
remarkus89 | 7:ebf728afc308 | 42 | // Green intensity is used to represent the vertical axe (y), |
remarkus89 | 7:ebf728afc308 | 43 | // Blue intensity is used to represent the horizontal one (x). |
remarkus89 | 6:38459146ad89 | 44 | if (tp == 0){ |
remarkus89 | 6:38459146ad89 | 45 | led_red = 1; |
remarkus89 | 6:38459146ad89 | 46 | if (abs(acc.getAccX()) >= ACC_THRESH) |
remarkus89 | 6:38459146ad89 | 47 | led_blue = 1-abs(acc.getAccX()); |
remarkus89 | 6:38459146ad89 | 48 | else |
remarkus89 | 6:38459146ad89 | 49 | led_blue = 1; |
remarkus89 | 6:38459146ad89 | 50 | if (abs(acc.getAccY()) >= ACC_THRESH) |
remarkus89 | 6:38459146ad89 | 51 | led_green = 1-abs(acc.getAccY()); |
remarkus89 | 6:38459146ad89 | 52 | else |
remarkus89 | 6:38459146ad89 | 53 | led_green = 1; |
remarkus89 | 7:ebf728afc308 | 54 | // If the user is touching the upper part of the slider, make the RGB led |
remarkus89 | 7:ebf728afc308 | 55 | // assume the Red color |
remarkus89 | 6:38459146ad89 | 56 | } else if (tp >= 0.5) { |
remarkus89 | 6:38459146ad89 | 57 | led_blue = 1; |
remarkus89 | 6:38459146ad89 | 58 | led_green = 1; |
remarkus89 | 6:38459146ad89 | 59 | led_red = 0; |
remarkus89 | 7:ebf728afc308 | 60 | // If the user is touching the lower part of the slider, make the RGB led |
remarkus89 | 7:ebf728afc308 | 61 | // assume the White color |
remarkus89 | 6:38459146ad89 | 62 | } else { |
remarkus89 | 6:38459146ad89 | 63 | led_blue = 0; |
remarkus89 | 6:38459146ad89 | 64 | led_green = 0; |
remarkus89 | 6:38459146ad89 | 65 | led_red = 0; |
remarkus89 | 6:38459146ad89 | 66 | } |
remarkus89 | 7:ebf728afc308 | 67 | // Send a JSON-format message of the measurements to the Serial Port |
remarkus89 | 6:38459146ad89 | 68 | pc.printf("{ \"slider\" : %f, \"acc_x\" : %f, \"acc_y\" : %f, \"acc_z\" : %f }\n", tp, acc.getAccX(), acc.getAccY(), acc.getAccZ()); |
remarkus89 | 7:ebf728afc308 | 69 | // Wait a fixed amount of time before repeating the whole while-cycle |
remarkus89 | 6:38459146ad89 | 70 | wait(WAIT_TIME); |
emilmont | 0:0f00f07ebde0 | 71 | } |
chris | 1:51b1b688179a | 72 | } |