
Part 2
Diff: main.cpp
- Revision:
- 5:75cd0f7649ca
- Parent:
- 4:efbc5085138a
- Child:
- 6:b4a7a3e46018
--- a/main.cpp Tue Feb 19 17:29:20 2019 +0000 +++ b/main.cpp Wed Feb 20 18:28:12 2019 +0000 @@ -19,18 +19,6 @@ #define Zn Magenta #define IN Black -// to convert back to string -char colours[8][8] = { - /*0*/"White", - /*1*/"Yellow", - /*2*/"Magenta", - /*3*/"Red", - /*4*/"Cyan", - /*5*/"Green", - /*6*/"Blue", - /*7*/"Black" - }; - /* assuming (x^2 + y^2 + z^2)^0.5 = 1 G-> shared between 2 axes = 0.707 value @@ -52,20 +40,6 @@ MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); Serial pc(USBTX, USBRX); // tx, rx -void led_decode(int ip){ // drives LEDs - rled = (ip & 0b0100); - gled = (ip & 0b0010); - bled = (ip & 0b0001); -} - -void state_report(int ip){//reports on state - static int old_state; - if(ip != old_state){ - pc.printf("Orientation state now: %s \n\r", colours[ip]); - old_state = ip; - }//end if - }//end func - int orientation_find(float x, float y, float z){ if (abs(x) > TH){ if (x > 0){ @@ -99,46 +73,93 @@ int main(void) { int state = IN; - //MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); - //Serial pc(USBTX, USBRX); // tx, rx + int tick = 0; + int system = 1; - pc.printf("MMA8451 ID: %d\n\r", acc.getWhoAmI()); //added carriage return + //still needed: + pc.printf("MMA8451 ID: %d\n\r", acc.getWhoAmI()); + //init leds + rled = 1; + gled = 1; + bled = 1; + + while (true) { float x, y, z; x = acc.getAccX(); y = acc.getAccY(); z = acc.getAccZ(); - state = orientation_find(x,y,z); - - led_decode(state); - - int orientation = 1; - int count; - - switch(orientation){ - case 1: //no orientation state - if(state != IN){ // if state is not IN: - state_report(state); - orientation = 2; - count = 3; + state = orientation_find(x,y,z); + + switch(system){ + case 1: //initial flat state + if(tick > 100 && state == Yn){ + pc.printf("passed first stage \n\r"); + system = 2; + tick = 0; + } + else if( tick < 100 && state == Yn){ + system = 6; + } + else if(state != Zp && state != IN){ + system = 5; } break; - case 2: //orientation state - state_report(state); //always report - if(state == IN && count > 0){ //trying to transition back - count--; + case 2: //right orientation state + if(tick > 20 && tick < 60 && state == Xn){ + pc.printf("passed second stage \n\r"); + system = 3; + tick = 0; + } + else if((tick > 20 && state == Xn) || tick > 60 ){ + system = 6; + } + else if(state != Yn && state != IN){ + system = 5; + } + break; + + case 3: //upwards orientation + if(tick > 40 && tick < 80 && state == Zp){ + pc.printf("passed third stage \n\r"); + system = 4; + tick = 0; + } + else if((tick > 40 && state == Zp) || tick > 80 ){ + system = 6; + } + else if(state != Xn && state != IN){ + system = 5; } - else if(state == IN && count == 0){ //transition back to no orientation - orientation = 1; - } - else if(state != IN){ //reset as there is an orientation - count = 3; - } - }// end switch + break; + + case 4://flat success + pc.printf("Sequence is complete\n\r"); + system = 7; + gled = 0; + break; + + case 5://sequence fail + pc.printf("Sequence Error \n\r"); + system = 7; + rled = 0; + break; + + case 6://timing fail + pc.printf("Time error \n\r"); + system = 7; + rled = 0; + break; + + case 7://idle state + // nothing happens + break; - wait(0.3); + }// end switch + wait(0.1); // 100ms timing accuracy, + tick++; }//end while }//end main