Final Version of food controller

Dependencies:   MMA8451Q TSI mbed

Fork of foodcontroller_NoWiFi by Serpentine

Committer:
danlock10y
Date:
Thu Jun 09 14:44:14 2016 +0000
Revision:
5:c4bc5a92390a
Parent:
4:1961b60aa8fb
LED Display updated to WASD format for directino and colour coded speed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pemb4660 0:c5a6d47904dc 1 #include "mbed.h"
pemb4660 0:c5a6d47904dc 2 #include "TSISensor.h"
pemb4660 0:c5a6d47904dc 3 #include "MMA8451Q.h"
pemb4660 0:c5a6d47904dc 4
pemb4660 0:c5a6d47904dc 5 #define MMA8451_I2C_ADDRESS (0x1d<<1)
pemb4660 0:c5a6d47904dc 6 Serial pc(USBTX, USBRX); // tx, rx
pemb4660 0:c5a6d47904dc 7
danlock10y 5:c4bc5a92390a 8 DigitalOut gled(LED_GREEN); //Indicator for touch sensor Output speed 1
danlock10y 5:c4bc5a92390a 9 DigitalOut bled(LED_BLUE); //Indicator for touch sensor Output speed 2
danlock10y 5:c4bc5a92390a 10 DigitalOut rled(LED_RED); //Indicator for touch sensor Output speed 3
danlock10y 5:c4bc5a92390a 11 TSISensor tsi; //Setup touch sensor
danlock10y 5:c4bc5a92390a 12 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); //Setup accelerometer
danlock10y 5:c4bc5a92390a 13 DigitalOut lefthigh(PTC7); //Test LED for left
danlock10y 5:c4bc5a92390a 14 DigitalOut leftlow(PTA1);
danlock10y 5:c4bc5a92390a 15 DigitalOut righthigh(PTC4); //Test LED for right
danlock10y 5:c4bc5a92390a 16 DigitalOut rightlow(PTA12);
danlock10y 5:c4bc5a92390a 17 DigitalOut forwardhigh(PTC0); //Test LED for forward
danlock10y 5:c4bc5a92390a 18 DigitalOut forwardlow(PTC3);
danlock10y 5:c4bc5a92390a 19 DigitalOut backwardhigh(PTA2); //Test LED for backward
danlock10y 5:c4bc5a92390a 20 DigitalOut backwardlow(PTD4);
danlock10y 1:6dd9eca697db 21
danlock10y 1:6dd9eca697db 22 int Direction; //2 bit number to describe direction 0 forward assigned CW
danlock10y 1:6dd9eca697db 23 int Velocity; //2 bit number to vary velocity in game
danlock10y 1:6dd9eca697db 24
danlock10y 1:6dd9eca697db 25 bool right; //Right or left? right = 1
danlock10y 1:6dd9eca697db 26 bool forward; //Forward or Backward? forward = 1
danlock10y 1:6dd9eca697db 27
pemb4660 0:c5a6d47904dc 28 int main()
pemb4660 0:c5a6d47904dc 29 {
danlock10y 1:6dd9eca697db 30 while (1)
danlock10y 1:6dd9eca697db 31 {
danlock10y 3:768cca7d53fe 32
danlock10y 3:768cca7d53fe 33 float accX=acc.getAccX(); //Measure acceleration in X direction
danlock10y 3:768cca7d53fe 34 float accY=acc.getAccY(); //Measure acceleration in Y direction
danlock10y 3:768cca7d53fe 35
danlock10y 3:768cca7d53fe 36 if (tsi.readDistance() != 0)
danlock10y 5:c4bc5a92390a 37 {
danlock10y 2:a6db66722be0 38 if (tsi.readDistance() <= 13)
danlock10y 2:a6db66722be0 39 {
danlock10y 2:a6db66722be0 40 Velocity = 3;
danlock10y 5:c4bc5a92390a 41 rled = 0;
danlock10y 5:c4bc5a92390a 42 bled = 1;
danlock10y 5:c4bc5a92390a 43 gled = 1;
danlock10y 2:a6db66722be0 44 }
danlock10y 2:a6db66722be0 45 if (tsi.readDistance() > 13 && tsi.readDistance() < 26)
danlock10y 2:a6db66722be0 46 {
danlock10y 2:a6db66722be0 47 Velocity = 2;
danlock10y 5:c4bc5a92390a 48 rled = 1;
danlock10y 5:c4bc5a92390a 49 bled = 0;
danlock10y 5:c4bc5a92390a 50 gled = 1;
danlock10y 2:a6db66722be0 51 }
danlock10y 2:a6db66722be0 52 if (tsi.readDistance() >= 26)
danlock10y 2:a6db66722be0 53 {
danlock10y 2:a6db66722be0 54 Velocity = 1;
danlock10y 5:c4bc5a92390a 55 rled = 1;
danlock10y 5:c4bc5a92390a 56 bled = 1;
danlock10y 5:c4bc5a92390a 57 gled = 0;
danlock10y 2:a6db66722be0 58 }
danlock10y 5:c4bc5a92390a 59
danlock10y 2:a6db66722be0 60 printf("x=%d\r\n",Velocity);
danlock10y 2:a6db66722be0 61 wait(0.2);
danlock10y 2:a6db66722be0 62
danlock10y 2:a6db66722be0 63
danlock10y 3:768cca7d53fe 64 }//endif touch sensor
danlock10y 5:c4bc5a92390a 65
danlock10y 1:6dd9eca697db 66 if (accX > 0.1)
danlock10y 1:6dd9eca697db 67 {
danlock10y 3:768cca7d53fe 68 right = false;
danlock10y 5:c4bc5a92390a 69 //printf("left \r\n");
danlock10y 1:6dd9eca697db 70 }//endif
pemb4660 0:c5a6d47904dc 71
danlock10y 1:6dd9eca697db 72 if (accX < -0.1)
danlock10y 1:6dd9eca697db 73 {
danlock10y 3:768cca7d53fe 74 right = true;
danlock10y 5:c4bc5a92390a 75 //printf("right \r\n");
danlock10y 1:6dd9eca697db 76 }//endif
danlock10y 3:768cca7d53fe 77 wait(0.1);
danlock10y 1:6dd9eca697db 78
danlock10y 3:768cca7d53fe 79 if (accY > 0.1)
danlock10y 1:6dd9eca697db 80 {
danlock10y 3:768cca7d53fe 81 forward = false;
danlock10y 5:c4bc5a92390a 82 //printf("back \r\n");
danlock10y 1:6dd9eca697db 83 }//endif
pemb4660 0:c5a6d47904dc 84
danlock10y 3:768cca7d53fe 85 if (accY < -0.1)
danlock10y 1:6dd9eca697db 86 {
danlock10y 3:768cca7d53fe 87 forward = true;
danlock10y 5:c4bc5a92390a 88 //printf("for \r\n");
danlock10y 1:6dd9eca697db 89 }//endif
danlock10y 3:768cca7d53fe 90
danlock10y 3:768cca7d53fe 91 wait(0.1);
danlock10y 3:768cca7d53fe 92
danlock10y 4:1961b60aa8fb 93 if(abs(accY) > abs(accX))
danlock10y 3:768cca7d53fe 94 {
danlock10y 4:1961b60aa8fb 95 if(forward == true)
danlock10y 4:1961b60aa8fb 96 {
danlock10y 4:1961b60aa8fb 97 Direction = 0;
danlock10y 5:c4bc5a92390a 98
danlock10y 5:c4bc5a92390a 99 lefthigh = 0;
danlock10y 5:c4bc5a92390a 100 leftlow = 0;
danlock10y 5:c4bc5a92390a 101 righthigh = 0;
danlock10y 5:c4bc5a92390a 102 rightlow = 0;
danlock10y 5:c4bc5a92390a 103 forwardhigh = 1;
danlock10y 5:c4bc5a92390a 104 forwardlow = 0;
danlock10y 5:c4bc5a92390a 105 backwardhigh = 0;
danlock10y 5:c4bc5a92390a 106 backwardlow = 0;
danlock10y 5:c4bc5a92390a 107
danlock10y 4:1961b60aa8fb 108 }//endif
danlock10y 4:1961b60aa8fb 109 else
danlock10y 3:768cca7d53fe 110 {
danlock10y 5:c4bc5a92390a 111 Direction = 2;
danlock10y 5:c4bc5a92390a 112
danlock10y 5:c4bc5a92390a 113 lefthigh = 0;
danlock10y 5:c4bc5a92390a 114 leftlow = 0;
danlock10y 5:c4bc5a92390a 115 righthigh = 0;
danlock10y 5:c4bc5a92390a 116 rightlow = 0;
danlock10y 5:c4bc5a92390a 117 forwardhigh = 0;
danlock10y 5:c4bc5a92390a 118 forwardlow = 0;
danlock10y 5:c4bc5a92390a 119 backwardhigh = 1;
danlock10y 5:c4bc5a92390a 120 backwardlow = 0;
danlock10y 5:c4bc5a92390a 121
danlock10y 4:1961b60aa8fb 122 }//endelse
danlock10y 4:1961b60aa8fb 123 }//endif
danlock10y 4:1961b60aa8fb 124 else
danlock10y 4:1961b60aa8fb 125 {
danlock10y 4:1961b60aa8fb 126 if(right == true)
danlock10y 4:1961b60aa8fb 127 {
danlock10y 5:c4bc5a92390a 128 Direction = 1;
danlock10y 5:c4bc5a92390a 129
danlock10y 5:c4bc5a92390a 130 lefthigh = 0;
danlock10y 5:c4bc5a92390a 131 leftlow = 0;
danlock10y 5:c4bc5a92390a 132 righthigh = 1;
danlock10y 5:c4bc5a92390a 133 rightlow = 0;
danlock10y 5:c4bc5a92390a 134 forwardhigh = 0;
danlock10y 5:c4bc5a92390a 135 forwardlow = 0;
danlock10y 5:c4bc5a92390a 136 backwardhigh = 0;
danlock10y 5:c4bc5a92390a 137 backwardlow = 0;
danlock10y 5:c4bc5a92390a 138
danlock10y 4:1961b60aa8fb 139 }//endif
danlock10y 4:1961b60aa8fb 140 else
danlock10y 4:1961b60aa8fb 141 {
danlock10y 5:c4bc5a92390a 142 Direction = 3;
danlock10y 5:c4bc5a92390a 143
danlock10y 5:c4bc5a92390a 144 lefthigh = 1;
danlock10y 5:c4bc5a92390a 145 leftlow = 0;
danlock10y 5:c4bc5a92390a 146 righthigh = 0;
danlock10y 5:c4bc5a92390a 147 rightlow = 0;
danlock10y 5:c4bc5a92390a 148 forwardhigh = 0;
danlock10y 5:c4bc5a92390a 149 forwardlow = 0;
danlock10y 5:c4bc5a92390a 150 backwardhigh = 0;
danlock10y 5:c4bc5a92390a 151 backwardlow = 0;
danlock10y 5:c4bc5a92390a 152
danlock10y 4:1961b60aa8fb 153 }//endelse
danlock10y 4:1961b60aa8fb 154 }//endelse
danlock10y 4:1961b60aa8fb 155
danlock10y 4:1961b60aa8fb 156 printf("Direction = %d \r\n", Direction);
danlock10y 4:1961b60aa8fb 157
danlock10y 1:6dd9eca697db 158 }//endwhile
danlock10y 1:6dd9eca697db 159 }//endmain
danlock10y 5:c4bc5a92390a 160