Andorid のコントローラーとして使用します。

Dependencies:   MMA8451Q TextLCD mbed

main.cpp

Committer:
NANAPAPA0308
Date:
2014-10-10
Revision:
0:ffae524e5e92

File content as of revision 0:ffae524e5e92:

#include "mbed.h"
#include <math.h>
#include "MMA8451Q.h"
#include "TextLCD.h"
#define MMA8451_I2C_ADDRESS (0x1d<<1)


    //  LCD (RS, E, D4, D5, D6, D7)
    TextLCD lcd(PTA13, PTD5, PTA4, PTA5, PTC8, PTC9); // LCD Shield for Arduino (DFR00009)
    //AnalogIn button(PTB0);  // board button
    PwmOut backlight(PTD0);
    Serial serial(USBTX,USBRX);
    DigitalIn BK(PTE5);
    DigitalIn ACC(PTE4);
    DigitalIn LIGHT(PTE3);
    DigitalIn WR(PTE2);
    DigitalIn WL(PTB11);
    DigitalIn WIPE(PTB10);
    int BK1,ACC1,LIGHT1,WR1,WL1,WIPE1;
    DigitalOut led(LED_RED);
    int main() {
    backlight = 1;  // ON: 1, OFF: 0
    MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
    //if(!BK ){
    //BK1 = 1;}
    //else {BK1=0;}
    
    PwmOut rled(LED_RED);
    PwmOut gled(LED_GREEN);
    PwmOut bled(LED_BLUE);
    double hnd,hnd1;
    while (1) {
        if(!BK ){BK1=1;}
        else {BK1=0;}
        if(!ACC ){ACC1=1;}
        else {ACC1=0;}
        if(!LIGHT ){LIGHT1=1;}
        else {LIGHT1=0;}
        if(!WR ){WR1=1;}
        else {WR1=0;}
        if(!WL ){WL1=1;}
        else {WL1=0;}
        if(!WIPE ){WIPE1=1;}
        else {WIPE1=0;}
        rled = 1.0 - abs(acc.getAccX());
        gled = 1.0 - abs(acc.getAccY());
        bled = 1.0 - abs(acc.getAccZ());
        lcd.locate(0,0);
        lcd.printf("X=%1.2f", acc.getAccX());  // button value
        lcd.printf(" ");
        lcd.locate(8,0);
        lcd.printf("Y=%1.2f", acc.getAccY());  // button value
        lcd.printf(" ");
        lcd.locate(0,1);
        lcd.printf("Z=%1.2f", acc.getAccZ());  // button value
        if( acc.getAccX() > 0.99)
        {hnd1=0.99;}
        else if(acc.getAccX() < -0.99)
        {hnd1=-0.99;}
        else{
        hnd1 = acc.getAccX();
        }
        if( acc.getAccY() < -0.1 )
        {
        if( acc.getAccX() < -0.2 )
        {hnd1=-0.99;}
        {}
        }
        {}
        if( acc.getAccY() < -0.1 )
        {
        if( acc.getAccX() > 0.2 )
        {hnd1=0.99;}
        {}
        }
        {}
        
        
        hnd = asin(hnd1)/3.14*180;
        lcd.printf(" ");
        lcd.locate(8,1);
        lcd.printf("H=%3.1f", hnd);   // ハンドル
        serial.printf("{\"x\":%5.2f,\"y\":%5.2f,\"z\":%5.2f,\"hnd\":%3.1f,\"BK1\":%d,\"ACC1\":%d,\"LIGHT1\":%d,\"WR1\":%d,\"WL1\":%d,\"WIPE1\":%d,}\r\n",acc.getAccX(), acc.getAccY(), acc.getAccZ(),hnd,BK1,ACC1,LIGHT1,WR1,WL1,WIPE1);
        wait(0.3);
    }
}