Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "USBKeyboard.h" 00003 00004 Serial pc(USBTX, USBRX); 00005 00006 int sumX; // Used for zero averaging 00007 int sumY; // Used for zero averaging 00008 int sumZ; // Used for zero averaging 00009 00010 // Up/Down Accel // 00011 AnalogIn inputUDx(PTB0); 00012 AnalogIn inputUDy(PTB1); 00013 AnalogIn inputUDz(PTB2); 00014 00015 int xUD1=0; // X-Offset 00016 int yUD1=0; // Y-Offset 00017 int zUD1=0; // Z-Offset 00018 00019 int xUD=0; // variable for x axes 00020 int yUD=0; // variable for y axes 00021 int zUD=0; // variable for z axes 00022 00023 // ----------- // 00024 00025 // Left/Right Accel // 00026 AnalogIn inputLRx(PTB3); 00027 AnalogIn inputLRy(PTC2); 00028 AnalogIn inputLRz(PTC1); 00029 00030 int xLR1=0; // X-Offset 00031 int yLR1=0; // Y-Offset 00032 int zLR1=0; // Z-Offset 00033 00034 int xLR=0; // variable for x axes 00035 int yLR=0; // variable for y axes 00036 int zLR=0; // variable for z axes 00037 // ----------- // 00038 00039 // Debugging LEDs // 00040 PwmOut led1(LED1); 00041 PwmOut led2(LED2); 00042 PwmOut led3(LED3); 00043 // ----------- // 00044 00045 // ISRs // 00046 Timeout debounceUp; 00047 Timeout debounceDown; 00048 Timeout debounceLeft; 00049 Timeout debounceRight; 00050 00051 Timeout middleUp; 00052 Timeout middleDown; 00053 Timeout middleLeft; 00054 Timeout middleRight; 00055 00056 bool mFUp = 1; 00057 bool mFDown = 1; 00058 bool mFLeft = 1; 00059 bool mFRight = 1; 00060 00061 bool dwn = 0; 00062 bool up = 0; 00063 bool lft = 0; 00064 bool rht = 0; 00065 00066 // Debounce ISRs // 00067 void DBUp(){up = 0;} 00068 00069 void DBDown(){dwn = 0;} 00070 00071 void DBLeft(){lft = 0;} 00072 00073 void DBRight(){rht = 0;} 00074 00075 //---------------// 00076 00077 // Middle Flag ISRs// 00078 void midUp(){mFUp = 1;} 00079 00080 void midDown(){mFDown = 1;} 00081 00082 void midLeft(){mFLeft = 1;} 00083 00084 void midRight(){mFRight = 1;} 00085 //---------------// 00086 00087 int main() { 00088 pc.baud(9600); 00089 wait(1); 00090 00091 for(int i =0; i < 10;i++){ 00092 xUD = inputUDx.read_u16(); 00093 yUD = inputUDy.read_u16(); 00094 zUD = inputUDz.read_u16(); 00095 sumX += xUD; 00096 sumY += yUD; 00097 sumZ += zUD; 00098 } 00099 00100 xUD1 = sumX/10; 00101 yUD1 = sumY/10; 00102 zUD1 = sumZ/10; 00103 00104 sumX = 0; // Reset sum terms for LR controller 00105 sumY = 0; 00106 sumZ = 0; 00107 00108 for(int i =0; i < 10;i++){ 00109 xLR = inputLRx.read_u16(); 00110 yLR = inputLRy.read_u16(); 00111 zLR = inputLRz.read_u16(); 00112 sumX += xLR; 00113 sumY += yLR; 00114 sumZ += zLR; 00115 } 00116 00117 xLR1 = sumX/10; 00118 yLR1 = sumY/10; 00119 zLR1 = sumZ/10; 00120 00121 USBKeyboard keyboard; 00122 while (true) { 00123 // Up/Down Read 00124 xUD = inputUDx.read_u16()-xUD1; 00125 yUD = inputUDy.read_u16()-yUD1; 00126 zUD = inputUDz.read_u16()-zUD1; 00127 00128 // Left/Right Read 00129 xLR = inputLRx.read_u16()-xLR1; 00130 yLR = inputLRy.read_u16()-yLR1; 00131 zLR = inputLRz.read_u16()-zLR1; 00132 00133 00134 //Establish Up 00135 if (xUD > -800 && yUD < 1500 && up == 0) { 00136 keyboard.keyCode(UP_ARROW); 00137 led1 = 1; 00138 led2 = 0.9; 00139 led3 = 1; 00140 up = 1; 00141 debounceUp.attach(&DBDown, 0.100); 00142 } 00143 00144 //Establish Down 00145 if(xUD < -3500 && yUD > 5000 && dwn == 0) { 00146 keyboard.keyCode(DOWN_ARROW); 00147 led1 = 1; 00148 led2 = 1; 00149 led3 = 0.9; 00150 dwn = 1; 00151 debounceDown.attach(&DBUp, 0.100); 00152 } 00153 00154 //Establish Left 00155 if (xLR > -800 && yLR < 1500 && lft == 0) { 00156 keyboard.keyCode(LEFT_ARROW); 00157 led1 = 1; 00158 led2 = 0.9; 00159 led3 = 1; 00160 lft = 1; 00161 debounceRight.attach(&DBRight, 0.100); 00162 } 00163 00164 //Establish Right 00165 if(xLR < -3500 && yLR > 5000 && rht == 0) { 00166 keyboard.keyCode(RIGHT_ARROW); 00167 led1 = 1; 00168 led2 = 1; 00169 led3 = 0.9; 00170 rht = 1; 00171 debounceLeft.attach(&DBLeft, 0.100); 00172 } 00173 //When not moving 00174 if (xUD < -2000 && xUD > -3000 && yUD < 4000 && yUD > 3000 && mFUp == 1){ 00175 up = 0; 00176 mFUp = 0; 00177 middleUp.attach(&midUp,0.50); 00178 led1 = 0.9; 00179 led2 = 1; 00180 led3 = 1; 00181 } // end if 00182 00183 if (xUD < -1000 && xUD > -2800 && yUD < 3000 && yUD > 2000 && mFDown == 1){ 00184 dwn = 0; 00185 mFDown = 0; 00186 middleDown.attach(&midDown,0.50); 00187 led1 = 0.9; 00188 led2 = 0.9; 00189 led3 = 1; 00190 } // end if 00191 00192 if (xLR < -2000 && xLR > -3000 && yLR < 4000 && yLR > 3000 && mFLeft == 1){ 00193 lft = 0; 00194 mFLeft = 0; 00195 middleLeft.attach(&midLeft,0.50); 00196 led1 = 0.9; 00197 led2 = 1; 00198 led3 = 1; 00199 } // end if 00200 00201 if (xLR < -1000 && xLR > -2800 && yLR < 3000 && yLR > 2000 && mFRight == 1){ 00202 rht = 0; 00203 mFRight = 0; 00204 middleRight.attach(&midRight,0.50); 00205 led1 = 0.9; 00206 led2 = 0.9; 00207 led3 = 1; 00208 } // end if 00209 00210 wait_ms(50); 00211 } // end while 00212 } // end main
Generated on Wed Jul 13 2022 07:16:04 by
1.7.2