Controller for snake

Dependencies:   MMA8451Q TSI mbed

Fork of foodcontroller3 by Serpentine

Committer:
danlock10y
Date:
Thu Jun 09 09:27:59 2016 +0000
Revision:
1:6dd9eca697db
Parent:
0:c5a6d47904dc
Child:
2:a6db66722be0
Food controller outputting left right forward and back as well as velocity slider

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 1:6dd9eca697db 33 if (tsi.readPercentage() != 0)
danlock10y 1:6dd9eca697db 34 {
danlock10y 1:6dd9eca697db 35 gled = 1.0 - tsi.readPercentage();
danlock10y 1:6dd9eca697db 36 }//endif touch sensor
pemb4660 0:c5a6d47904dc 37
danlock10y 1:6dd9eca697db 38 if((accX <= 0.1f) && (accX >= -0.1f))
danlock10y 1:6dd9eca697db 39 {
danlock10y 1:6dd9eca697db 40 if (right == true)
danlock10y 1:6dd9eca697db 41 {
danlock10y 1:6dd9eca697db 42 righthigh = 1;
danlock10y 1:6dd9eca697db 43 rightlow = 0;
danlock10y 1:6dd9eca697db 44 } //endif
danlock10y 1:6dd9eca697db 45 else
danlock10y 1:6dd9eca697db 46 {
danlock10y 1:6dd9eca697db 47 lefthigh = 1;
danlock10y 1:6dd9eca697db 48 leftlow = 0;
danlock10y 1:6dd9eca697db 49 }//endelse
danlock10y 1:6dd9eca697db 50 }//endif
pemb4660 0:c5a6d47904dc 51
danlock10y 1:6dd9eca697db 52 if (accX > 0.1)
danlock10y 1:6dd9eca697db 53 {
danlock10y 1:6dd9eca697db 54 lefthigh = 0;
danlock10y 1:6dd9eca697db 55 leftlow = 0;
danlock10y 1:6dd9eca697db 56 righthigh = 1;
danlock10y 1:6dd9eca697db 57 rightlow = 0;
danlock10y 1:6dd9eca697db 58 forwardhigh = 0;
danlock10y 1:6dd9eca697db 59 forwardlow = 0;
danlock10y 1:6dd9eca697db 60 backwardhigh = 0;
danlock10y 1:6dd9eca697db 61 backwardlow = 0;
danlock10y 1:6dd9eca697db 62 right = true;
danlock10y 1:6dd9eca697db 63 }//endif
pemb4660 0:c5a6d47904dc 64
danlock10y 1:6dd9eca697db 65 if (accX < -0.1)
danlock10y 1:6dd9eca697db 66 {
danlock10y 1:6dd9eca697db 67 lefthigh = 1;
danlock10y 1:6dd9eca697db 68 leftlow = 0;
danlock10y 1:6dd9eca697db 69 righthigh = 0;
danlock10y 1:6dd9eca697db 70 rightlow = 0;
danlock10y 1:6dd9eca697db 71 forwardhigh = 0;
danlock10y 1:6dd9eca697db 72 forwardlow = 0;
danlock10y 1:6dd9eca697db 73 backwardhigh = 0;
danlock10y 1:6dd9eca697db 74 backwardlow = 0;
danlock10y 1:6dd9eca697db 75 right = false;
danlock10y 1:6dd9eca697db 76 }//endif
danlock10y 1:6dd9eca697db 77 wait(0.1);
danlock10y 1:6dd9eca697db 78
danlock10y 1:6dd9eca697db 79 if((acc.getAccY() <= 0.1) && (acc.getAccY() >= -0.1))
danlock10y 1:6dd9eca697db 80 {
danlock10y 1:6dd9eca697db 81 if (forward == true)
danlock10y 1:6dd9eca697db 82 {
pemb4660 0:c5a6d47904dc 83 forwardhigh = 1;
danlock10y 1:6dd9eca697db 84 forwardlow = 0;
danlock10y 1:6dd9eca697db 85 }//endif
danlock10y 1:6dd9eca697db 86 else
danlock10y 1:6dd9eca697db 87 {
pemb4660 0:c5a6d47904dc 88 backwardhigh = 1;
danlock10y 1:6dd9eca697db 89 backwardlow = 0;
danlock10y 1:6dd9eca697db 90 }//endelse
danlock10y 1:6dd9eca697db 91 }//endif
pemb4660 0:c5a6d47904dc 92
danlock10y 1:6dd9eca697db 93 if (acc.getAccY() > 0.1)
danlock10y 1:6dd9eca697db 94 {
danlock10y 1:6dd9eca697db 95 lefthigh = 0;
danlock10y 1:6dd9eca697db 96 leftlow = 0;
danlock10y 1:6dd9eca697db 97 righthigh = 0;
danlock10y 1:6dd9eca697db 98 rightlow = 0;
danlock10y 1:6dd9eca697db 99 forwardhigh = 1;
danlock10y 1:6dd9eca697db 100 forwardlow = 0;
danlock10y 1:6dd9eca697db 101 backwardhigh = 0;
danlock10y 1:6dd9eca697db 102 backwardlow = 0;
pemb4660 0:c5a6d47904dc 103 forward = true;
danlock10y 1:6dd9eca697db 104 }//endif
pemb4660 0:c5a6d47904dc 105
danlock10y 1:6dd9eca697db 106 if (acc.getAccY() < -0.1)
danlock10y 1:6dd9eca697db 107 {
danlock10y 1:6dd9eca697db 108 lefthigh = 0;
danlock10y 1:6dd9eca697db 109 leftlow = 0;
danlock10y 1:6dd9eca697db 110 righthigh = 0;
danlock10y 1:6dd9eca697db 111 rightlow = 0;
danlock10y 1:6dd9eca697db 112 forwardhigh = 0;
danlock10y 1:6dd9eca697db 113 forwardlow = 0;
danlock10y 1:6dd9eca697db 114 backwardhigh = 1;
danlock10y 1:6dd9eca697db 115 backwardlow = 0;
pemb4660 0:c5a6d47904dc 116 forward = false;
danlock10y 1:6dd9eca697db 117 }//endif
pemb4660 0:c5a6d47904dc 118 wait(0.1);
danlock10y 1:6dd9eca697db 119 }//endwhile
danlock10y 1:6dd9eca697db 120 }//endmain