This is a program that reads from the accelerator and is very flexible regarding many things.

Dependencies:   MMA8451Q mbed tsi_sensor

Committer:
my_username
Date:
Sat Jul 12 02:14:46 2014 +0000
Revision:
0:f60be651a150
Progetto embedded

Who changed what in which revision?

UserRevisionLine numberNew contents of line
my_username 0:f60be651a150 1 #include "mbed.h" //libreria mbed standard
my_username 0:f60be651a150 2 #include "MMA8451Q.h" //libreria per usare l'acceleratore
my_username 0:f60be651a150 3 #include "tsi_sensor.h" //libreria per usare lo slider
my_username 0:f60be651a150 4
my_username 0:f60be651a150 5 #define MMA8451_I2C 0x1d<<1 //indirizzo dell' acceleratore
my_username 0:f60be651a150 6 #define PIN_SLI_10 10 // pin che identificano...
my_username 0:f60be651a150 7 #define PIN_SLI_9 9 //...lo slider
my_username 0:f60be651a150 8 #define RANGE 2 //valore arbitrario usato per rilevare se lo slider viene toccato
my_username 0:f60be651a150 9 #define SENSITIVITY 0 //cambiando il valore di una delle 3 assi di una quantità maggiore della SENSITIVITY, avviene la scrittura sulla porta seriale
my_username 0:f60be651a150 10 #define AXES 3 //numero degli assi
my_username 0:f60be651a150 11 #define SHUT_DOWN 1 //valore del LED spento
my_username 0:f60be651a150 12 #define MIN_DENSITY 0.9 //minimo valore della densità per far restare acceso il LED
my_username 0:f60be651a150 13 //#define RED 1 //codice del colore rosso
my_username 0:f60be651a150 14 //#define GREEN 2 //codice del colore verde
my_username 0:f60be651a150 15 //#define BLUE 3 //codice del colore blu
my_username 0:f60be651a150 16 #define BLUE_MARGIN_Y -1/3.0 //margine dopo il quale il LED diventa blu ( si riferisce all'asse y )
my_username 0:f60be651a150 17 #define RED_MARGIN_Z 0 //margine dopo il quale il LED diventa rosso ( si riferisce all'asse z )
my_username 0:f60be651a150 18
my_username 0:f60be651a150 19 PwmOut red(LED1);
my_username 0:f60be651a150 20 PwmOut green(LED2);
my_username 0:f60be651a150 21 PwmOut blue(LED3);
my_username 0:f60be651a150 22 float density = 0;
my_username 0:f60be651a150 23
my_username 0:f60be651a150 24 void printAxes( float* xyz, float* xyzPrinted ); //scrive i valori xyz sulla porta seriale
my_username 0:f60be651a150 25 void ledChange( float* xyz ); //metodo che verifica in quale colore cambiare il LED
my_username 0:f60be651a150 26 void makeRed();
my_username 0:f60be651a150 27 void makeGreen();
my_username 0:f60be651a150 28 void makeBlue();
my_username 0:f60be651a150 29 void densityChange( TSIAnalogSlider tsi ); //cambia la densità del LED dato l'input dallo slider
my_username 0:f60be651a150 30
my_username 0:f60be651a150 31 int main(){
my_username 0:f60be651a150 32
my_username 0:f60be651a150 33 MMA8451Q acc( PTE25, PTE24, MMA8451_I2C ); //creazione dell'oggetto che controlla l'acceleratore
my_username 0:f60be651a150 34 TSIAnalogSlider tsi( PIN_SLI_9, PIN_SLI_10, RANGE ); //creazione dell'oggetto che controlla lo slider
my_username 0:f60be651a150 35
my_username 0:f60be651a150 36 float* xyz = new float[AXES];
my_username 0:f60be651a150 37 float* xyzPrinted = new float[AXES];
my_username 0:f60be651a150 38
my_username 0:f60be651a150 39 while(true){
my_username 0:f60be651a150 40 acc.getAccAllAxis( xyz );
my_username 0:f60be651a150 41 if( SENSITIVITY == 0 ) //il cambiamento di densità si svolge solo in questo caso per motivi pratici
my_username 0:f60be651a150 42 densityChange( tsi );
my_username 0:f60be651a150 43 ledChange( xyz );
my_username 0:f60be651a150 44 printAxes( xyz, xyzPrinted );
my_username 0:f60be651a150 45 }
my_username 0:f60be651a150 46 }
my_username 0:f60be651a150 47
my_username 0:f60be651a150 48 void printAxes( float* xyz, float* xyzPrinted ){//, int color ){
my_username 0:f60be651a150 49
my_username 0:f60be651a150 50 for(int i = 0; i < AXES; i += 1)
my_username 0:f60be651a150 51 if( abs(xyz[i] - xyzPrinted[i]) > SENSITIVITY ){
my_username 0:f60be651a150 52 for( int i = 0; i < AXES; i += 1 ){
my_username 0:f60be651a150 53 //printf("%c: %.2f ", (unsigned char)i+88, xyz[i]);
my_username 0:f60be651a150 54 printf("%.2f:", xyz[i]); //stampa valori dell'inclinazione delimitati con ":"
my_username 0:f60be651a150 55 xyzPrinted[i] = xyz[i]; // |
my_username 0:f60be651a150 56 } // V
my_username 0:f60be651a150 57 printf("\n" ); //in aggiunta stampa codice colore ( cancellato, era: printf("%d\r\n", color); )
my_username 0:f60be651a150 58 fflush(stdout);
my_username 0:f60be651a150 59 }
my_username 0:f60be651a150 60 }
my_username 0:f60be651a150 61
my_username 0:f60be651a150 62 void ledChange( float* xyz ){
my_username 0:f60be651a150 63
my_username 0:f60be651a150 64 //int color;
my_username 0:f60be651a150 65 if( xyz[1] < BLUE_MARGIN_Y ){ //xyz[1] è l'asse y
my_username 0:f60be651a150 66 makeBlue();
my_username 0:f60be651a150 67 //color = BLUE;
my_username 0:f60be651a150 68 }
my_username 0:f60be651a150 69 else if( xyz[2] < RED_MARGIN_Z ){ //xyz[2] è l'asse z
my_username 0:f60be651a150 70 makeRed();
my_username 0:f60be651a150 71 //color = RED;
my_username 0:f60be651a150 72 }
my_username 0:f60be651a150 73 else{
my_username 0:f60be651a150 74 makeGreen();
my_username 0:f60be651a150 75 //color = GREEN;
my_username 0:f60be651a150 76 }
my_username 0:f60be651a150 77
my_username 0:f60be651a150 78 //return color;
my_username 0:f60be651a150 79 }
my_username 0:f60be651a150 80
my_username 0:f60be651a150 81 void makeRed(){
my_username 0:f60be651a150 82
my_username 0:f60be651a150 83 red = density;
my_username 0:f60be651a150 84 green = SHUT_DOWN;
my_username 0:f60be651a150 85 blue = SHUT_DOWN;
my_username 0:f60be651a150 86 }
my_username 0:f60be651a150 87
my_username 0:f60be651a150 88 void makeGreen(){
my_username 0:f60be651a150 89
my_username 0:f60be651a150 90 red = SHUT_DOWN;
my_username 0:f60be651a150 91 green = density;
my_username 0:f60be651a150 92 blue = SHUT_DOWN;
my_username 0:f60be651a150 93 }
my_username 0:f60be651a150 94
my_username 0:f60be651a150 95 void makeBlue(){
my_username 0:f60be651a150 96
my_username 0:f60be651a150 97 red = SHUT_DOWN;
my_username 0:f60be651a150 98 green = SHUT_DOWN;
my_username 0:f60be651a150 99 blue = density;
my_username 0:f60be651a150 100 }
my_username 0:f60be651a150 101
my_username 0:f60be651a150 102 void densityChange( TSIAnalogSlider tsi ){
my_username 0:f60be651a150 103
my_username 0:f60be651a150 104 if( tsi.readDistance() != 0 ) //se lo slider viene toccato
my_username 0:f60be651a150 105 if( (density = SHUT_DOWN - tsi.readPercentage()) > MIN_DENSITY ) //aggiorna la densità e controlla se il LED è "quasi spento"
my_username 0:f60be651a150 106 density = SHUT_DOWN; //spegne il LED completamente
my_username 0:f60be651a150 107 }