Controller for snake

Dependencies:   MMA8451Q TSI mbed

Fork of foodcontroller3 by Serpentine

Committer:
danlock10y
Date:
Thu Jun 09 09:37:00 2016 +0000
Revision:
2:a6db66722be0
Parent:
1:6dd9eca697db
Child:
3:768cca7d53fe
TSI divided into 3 regions to give three speeds

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 1:6dd9eca697db 8 PwmOut gled(LED_GREEN); //Indicator for touch sensor Output
danlock10y 1:6dd9eca697db 9 TSISensor tsi; //Setup touch sensor
danlock10y 1:6dd9eca697db 10 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); //Setup accelerometer
danlock10y 1:6dd9eca697db 11 DigitalOut lefthigh(PTC11); //Test LED for left
danlock10y 1:6dd9eca697db 12 DigitalOut leftlow(PTC9);
danlock10y 1:6dd9eca697db 13 DigitalOut righthigh(PTC5); //Test LED for right
danlock10y 1:6dd9eca697db 14 DigitalOut rightlow(PTA4);
danlock10y 1:6dd9eca697db 15 DigitalOut forwardhigh(PTC16); //Test LED for forward
danlock10y 1:6dd9eca697db 16 DigitalOut forwardlow(PTD0);
danlock10y 1:6dd9eca697db 17 DigitalOut backwardhigh(PTA17); //Test LED for backward
danlock10y 1:6dd9eca697db 18 DigitalOut backwardlow(PTD1);
danlock10y 1:6dd9eca697db 19
danlock10y 1:6dd9eca697db 20 float accX=acc.getAccX(); //Measure acceleration in X direction
danlock10y 1:6dd9eca697db 21 float accY=acc.getAccY(); //Measure acceleration in Y direction
danlock10y 1:6dd9eca697db 22
danlock10y 1:6dd9eca697db 23 int Direction; //2 bit number to describe direction 0 forward assigned CW
danlock10y 1:6dd9eca697db 24 int Velocity; //2 bit number to vary velocity in game
danlock10y 1:6dd9eca697db 25
danlock10y 1:6dd9eca697db 26 bool right; //Right or left? right = 1
danlock10y 1:6dd9eca697db 27 bool forward; //Forward or Backward? forward = 1
danlock10y 1:6dd9eca697db 28
pemb4660 0:c5a6d47904dc 29 int main()
pemb4660 0:c5a6d47904dc 30 {
danlock10y 1:6dd9eca697db 31 while (1)
danlock10y 1:6dd9eca697db 32 {
danlock10y 2:a6db66722be0 33 if (tsi.readDistance() != 0)
danlock10y 1:6dd9eca697db 34 {
danlock10y 2:a6db66722be0 35 // gled = 1.0 - tsi.readPercentage();
danlock10y 2:a6db66722be0 36 if (tsi.readDistance() <= 13)
danlock10y 2:a6db66722be0 37 {
danlock10y 2:a6db66722be0 38 Velocity = 3;
danlock10y 2:a6db66722be0 39 }
danlock10y 2:a6db66722be0 40 if (tsi.readDistance() > 13 && tsi.readDistance() < 26)
danlock10y 2:a6db66722be0 41 {
danlock10y 2:a6db66722be0 42 Velocity = 2;
danlock10y 2:a6db66722be0 43 }
danlock10y 2:a6db66722be0 44 if (tsi.readDistance() >= 26)
danlock10y 2:a6db66722be0 45 {
danlock10y 2:a6db66722be0 46 Velocity = 1;
danlock10y 2:a6db66722be0 47 }
danlock10y 2:a6db66722be0 48
danlock10y 2:a6db66722be0 49 printf("x=%d\r\n",Velocity);
danlock10y 2:a6db66722be0 50 wait(0.2);
danlock10y 2:a6db66722be0 51
danlock10y 2:a6db66722be0 52
danlock10y 1:6dd9eca697db 53 }//endif touch sensor
danlock10y 2:a6db66722be0 54 /*
danlock10y 1:6dd9eca697db 55 if((accX <= 0.1f) && (accX >= -0.1f))
danlock10y 1:6dd9eca697db 56 {
danlock10y 1:6dd9eca697db 57 if (right == true)
danlock10y 1:6dd9eca697db 58 {
danlock10y 1:6dd9eca697db 59 righthigh = 1;
danlock10y 1:6dd9eca697db 60 rightlow = 0;
danlock10y 1:6dd9eca697db 61 } //endif
danlock10y 1:6dd9eca697db 62 else
danlock10y 1:6dd9eca697db 63 {
danlock10y 1:6dd9eca697db 64 lefthigh = 1;
danlock10y 1:6dd9eca697db 65 leftlow = 0;
danlock10y 1:6dd9eca697db 66 }//endelse
danlock10y 1:6dd9eca697db 67 }//endif
pemb4660 0:c5a6d47904dc 68
danlock10y 1:6dd9eca697db 69 if (accX > 0.1)
danlock10y 1:6dd9eca697db 70 {
danlock10y 1:6dd9eca697db 71 lefthigh = 0;
danlock10y 1:6dd9eca697db 72 leftlow = 0;
danlock10y 1:6dd9eca697db 73 righthigh = 1;
danlock10y 1:6dd9eca697db 74 rightlow = 0;
danlock10y 1:6dd9eca697db 75 forwardhigh = 0;
danlock10y 1:6dd9eca697db 76 forwardlow = 0;
danlock10y 1:6dd9eca697db 77 backwardhigh = 0;
danlock10y 1:6dd9eca697db 78 backwardlow = 0;
danlock10y 1:6dd9eca697db 79 right = true;
danlock10y 1:6dd9eca697db 80 }//endif
pemb4660 0:c5a6d47904dc 81
danlock10y 1:6dd9eca697db 82 if (accX < -0.1)
danlock10y 1:6dd9eca697db 83 {
danlock10y 1:6dd9eca697db 84 lefthigh = 1;
danlock10y 1:6dd9eca697db 85 leftlow = 0;
danlock10y 1:6dd9eca697db 86 righthigh = 0;
danlock10y 1:6dd9eca697db 87 rightlow = 0;
danlock10y 1:6dd9eca697db 88 forwardhigh = 0;
danlock10y 1:6dd9eca697db 89 forwardlow = 0;
danlock10y 1:6dd9eca697db 90 backwardhigh = 0;
danlock10y 1:6dd9eca697db 91 backwardlow = 0;
danlock10y 1:6dd9eca697db 92 right = false;
danlock10y 1:6dd9eca697db 93 }//endif
danlock10y 1:6dd9eca697db 94 wait(0.1);
danlock10y 1:6dd9eca697db 95
danlock10y 1:6dd9eca697db 96 if((acc.getAccY() <= 0.1) && (acc.getAccY() >= -0.1))
danlock10y 1:6dd9eca697db 97 {
danlock10y 1:6dd9eca697db 98 if (forward == true)
danlock10y 1:6dd9eca697db 99 {
pemb4660 0:c5a6d47904dc 100 forwardhigh = 1;
danlock10y 1:6dd9eca697db 101 forwardlow = 0;
danlock10y 1:6dd9eca697db 102 }//endif
danlock10y 1:6dd9eca697db 103 else
danlock10y 1:6dd9eca697db 104 {
pemb4660 0:c5a6d47904dc 105 backwardhigh = 1;
danlock10y 1:6dd9eca697db 106 backwardlow = 0;
danlock10y 1:6dd9eca697db 107 }//endelse
danlock10y 1:6dd9eca697db 108 }//endif
pemb4660 0:c5a6d47904dc 109
danlock10y 1:6dd9eca697db 110 if (acc.getAccY() > 0.1)
danlock10y 1:6dd9eca697db 111 {
danlock10y 1:6dd9eca697db 112 lefthigh = 0;
danlock10y 1:6dd9eca697db 113 leftlow = 0;
danlock10y 1:6dd9eca697db 114 righthigh = 0;
danlock10y 1:6dd9eca697db 115 rightlow = 0;
danlock10y 1:6dd9eca697db 116 forwardhigh = 1;
danlock10y 1:6dd9eca697db 117 forwardlow = 0;
danlock10y 1:6dd9eca697db 118 backwardhigh = 0;
danlock10y 1:6dd9eca697db 119 backwardlow = 0;
pemb4660 0:c5a6d47904dc 120 forward = true;
danlock10y 1:6dd9eca697db 121 }//endif
pemb4660 0:c5a6d47904dc 122
danlock10y 1:6dd9eca697db 123 if (acc.getAccY() < -0.1)
danlock10y 1:6dd9eca697db 124 {
danlock10y 1:6dd9eca697db 125 lefthigh = 0;
danlock10y 1:6dd9eca697db 126 leftlow = 0;
danlock10y 1:6dd9eca697db 127 righthigh = 0;
danlock10y 1:6dd9eca697db 128 rightlow = 0;
danlock10y 1:6dd9eca697db 129 forwardhigh = 0;
danlock10y 1:6dd9eca697db 130 forwardlow = 0;
danlock10y 1:6dd9eca697db 131 backwardhigh = 1;
danlock10y 1:6dd9eca697db 132 backwardlow = 0;
pemb4660 0:c5a6d47904dc 133 forward = false;
danlock10y 1:6dd9eca697db 134 }//endif
danlock10y 2:a6db66722be0 135 wait(0.1);*/
danlock10y 1:6dd9eca697db 136 }//endwhile
danlock10y 1:6dd9eca697db 137 }//endmain