compass

Dependencies:   mbed

main.cpp

Committer:
s1210160
Date:
2016-06-15
Revision:
1:88b3169e16f8
Parent:
0:2656331a8ea3

File content as of revision 1:88b3169e16f8:

#include "mbed.h"

DigitalIn compass0(p7);
DigitalIn compass1(p8);
DigitalIn compass2(p11);
DigitalIn compass3(p12);

int main()
{
    
    int compass[4] = {0, 0, 0, 0};
    int c = 0;

    while(1) {
        wait(0.1);
        
        c = 0;
        if(compass0) {
            compass[0] = 1;
            c += 1;
        } else compass[0] = 0;
        if(compass1) {
            compass[1] = 1;
            c += 2;
        } else compass[1] = 0;
        if(compass2) {
            compass[2] = 1;
            c += 4;
        } else compass[2] = 0;
        if(compass3) {
            compass[3] = 1;
            c += 8;
        } else compass[3] = 0;

        switch(c) {
            case 0: {
                printf("南: ");
                break;
            }
            case 1:
                printf("西南西: ");
                break;

            case 2:
                printf("東北東: ");
                break;

            case 3:
                printf("北: ");
                break;

            case 4:
                printf("南南東: ");
                break;

            case 5:
                printf("西: ");
                break;

            case 6:
                printf("東: ");
                break;

            case 7:
                printf("北北西: ");
                break;

            case 8:
                printf("南南西: ");
                break;

            case 9:
                printf("南西: ");
                break;

            case 10:
                printf("北東: ");
                break;

            case 11:
                printf("北北東: ");
                break;

            case 12:
                printf("南東: ");
                break;

            case 13:
                printf("西北西: ");
                break;

            case 14:
                printf("東南東: ");
                break;

            case 15:
                printf("北西: ");
                break;

            default:
                break;
        }

        for(int i=0; i<4; i++) {
            printf("%d", compass[i]);
        }
        printf("\n");
    }
    return 0;
}