Changeset 77a80d3 in pyramid
- Timestamp:
- 10/16/13 19:39:18 (7 years ago)
- Branches:
- master
- Children:
- dbfae54
- Parents:
- f5d44ec
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
firmware/Puzzlebox_Pyramid/Puzzlebox_Pyramid.ino
rf5d44ec r77a80d3 38 38 39 39 // ADK 40 //#include <Max3421e.h>41 //#include <Usb.h>42 //#include <AndroidAccessory.h>40 #include <Max3421e.h> 41 #include <Usb.h> 42 #include <AndroidAccessory.h> 43 43 44 44 … … 77 77 boolean modeSerial = false; 78 78 boolean modeADK = false; 79 80 // Android ADK 81 // AndroidAccessory acc( 82 // "puzzlebox", 83 // "PuzzleboxPyramid", 84 // "Puzzlebox Pyramid", 85 // "1.2", 86 // "http://pyramid.puzzlebox.info", 87 // "0000000012345678" 88 // ); 89 90 AndroidAccessory acc( 91 "puzzlebox", 92 "TriggerRemote", 93 "DemoKit Arduino Board", 94 "1.0", 95 "http://www.android.com", 96 "0000000012345678" 97 ); 98 99 byte msg[2]; 100 byte led; 79 101 80 102 // MindWave Mobile Protocol … … 134 156 pinMode(13, OUTPUT); 135 157 Serial.begin(115200); 136 158 137 159 // Android ADK 138 160 // delay(500); 139 161 // acc.powerOn(); 140 162 141 163 142 164 ////////Orbit Flight/////////// 143 165 // initialize all timers except for 0, to save time keeping functions 144 166 InitTimersSafe(); 145 167 146 168 // sets the IR_frequency for the specified pin 147 169 bool success = SetPinFrequencySafe(IR, IR_frequency); 148 170 149 171 // if the pin IR_frequency was set successfully, turn pin 13 on 150 172 if(success) { 151 173 Serial.println("INFO: PWM pin frequency set"); 152 174 } 153 175 154 176 /////////////RGB//////////////// 155 177 Wire.begin(); // join i2c bus as master (address optional for master) 156 178 157 179 158 180 setStartScreen(); 181 delay(1000); // Take a second after startup to receive any USB Serial input 159 182 160 183 … … 168 191 //////////Bluetooth/////////// 169 192 170 if ( ! modeADK) {193 if ( (! modeADK) & (! Serial.available()) ) { 171 194 172 195 // delay(500); 173 196 174 197 setupBlueToothConnection(); 175 // wait 1s and flush the serial buffer 198 199 // wait 1s and flush the Bluetooth serial buffer 176 200 delay(1000); 177 // Serial.flush(); //delete?178 201 Serial1.flush(); 179 202 … … 211 234 } 212 235 else { 213 RGB_Panel[11][0]=0;214 RGB_Panel[11][1]=0;215 RGB_Panel[11][2]=0;236 // RGB_Panel[11][0]=0; 237 // RGB_Panel[11][1]=0; 238 // RGB_Panel[11][2]=0; 216 239 digitalWrite(13,LOW); 217 240 } … … 362 385 363 386 364 if (poorQuality == 200) 387 // if (poorQuality == 200) 388 if (poorQuality != 0) 365 389 setColorWheel(0,255,0); // Green 390 // return; 366 391 367 392 368 393 // Update RGB Panel when received data from MindWave Mobile 369 394 int att, med; 370 if(attention==0) att=0; 371 else att=(attention-1)/20+1; 372 if(meditation==0) med=0; 373 else med=(meditation-1)/20+1; 395 if (attention==0) 396 att=0; 397 else 398 att=(attention-1)/20+1; 399 400 if (meditation==0) 401 med=0; 402 else 403 med=(meditation-1)/20+1; 374 404 375 405 att_buff=attention; … … 380 410 RGB_Panel[11][2]=128; 381 411 } 382 else{ 383 RGB_Panel[11][0]=0; 384 RGB_Panel[11][1]=0; 385 RGB_Panel[11][2]=0; 412 else { 413 if (poorQuality == 0) { 414 RGB_Panel[11][0]=0; 415 RGB_Panel[11][1]=0; 416 RGB_Panel[11][2]=0; 417 } 386 418 } 387 419 … … 529 561 Serial1.print("\r\n+STAUTO=1\r\n");// Permit Auto-connection 530 562 //update bottom RGB LED to blue 563 setColorWheel(0,0,0); // Black 531 564 RGB_Panel[5][0]=0; 532 565 RGB_Panel[5][1]=0; … … 539 572 //If Bluetooth connection is established, top LED blue. 540 573 // Otherwise top LED red. 541 if(digitalRead(5)){//D5 for Pyramid Shield, A1 for testing542 RGB_Panel[11][0]=0;543 RGB_Panel[11][1]=0;544 RGB_Panel[11][2]=255;545 sendFrame(0);546 }547 else{548 RGB_Panel[11][0]=255;549 RGB_Panel[11][1]=0;550 RGB_Panel[11][2]=0;551 sendFrame(0);552 }574 // if(digitalRead(5)){//D5 for Pyramid Shield, A1 for testing 575 // RGB_Panel[11][0]=0; 576 // RGB_Panel[11][1]=0; 577 // RGB_Panel[11][2]=255; 578 // sendFrame(0); 579 // } 580 // else{ 581 // RGB_Panel[11][0]=255; 582 // RGB_Panel[11][1]=0; 583 // RGB_Panel[11][2]=0; 584 // sendFrame(0); 585 // } 553 586 554 587 } … … 574 607 RGB_Panel[5][1]=255; 575 608 RGB_Panel[5][2]=0; 576 } 577 else { 578 RGB_Panel[5][0]=0; 579 RGB_Panel[5][1]=0; 580 RGB_Panel[5][2]=0; 581 582 //Make top LED green, indicating valid bluetooth connection 583 RGB_Panel[11][0]=0; 584 RGB_Panel[11][1]=255; 585 RGB_Panel[11][2]=0; 586 } 609 } else 610 return; 611 // else { 612 // RGB_Panel[5][0]=0; 613 // RGB_Panel[5][1]=0; 614 // RGB_Panel[5][2]=0; 615 // 616 // //Make top LED green, indicating valid bluetooth connection 617 // RGB_Panel[11][0]=0; 618 // RGB_Panel[11][1]=255; 619 // RGB_Panel[11][2]=0; 620 // } 587 621 588 622 … … 703 737 bitWrite(mainCode,15,0); //this digit equals 0 in channel A or B 704 738 705 for (n=6; n>=0; n--) bitWrite(mainCode,8+n,bitRead(yaw,n));//yaw 739 for (n=6; n>=0; n--) 740 bitWrite(mainCode,8+n,bitRead(yaw,n));//yaw 706 741 707 742 //channel selection second half … … 726 761 727 762 bitWrite(mainCode,0,1); // finish code 763 728 764 return mainCode; // mainCode is a 29 bit binary number 765 729 766 } // formCode 730 767 … … 994 1031 } 995 1032 1033 // sendCode(formCode(_throttle,_yaw,_pitch)); 1034 // delay(80); // originally 36, 51, 66, 80 1035 1036 } // parseSerialInput() 1037 1038 1039 // ################################################################ 1040 1041 int calculateMeter(int value) { 1042 1043 int result = 0; 1044 1045 if (value == 0) 1046 result = 0; 1047 else if (value <= 20) 1048 result = 1; 1049 else if (value <= 40) 1050 result = 2; 1051 else if (value <= 60) 1052 result = 3; 1053 else if (value <= 80) 1054 result = 4; 1055 else if (value <= 100) 1056 result = 5; 1057 1058 return(result); 1059 1060 } // calculateMeter 1061 1062 1063 // ################################################################ 1064 1065 void updateFrameADK() { 1066 1067 // update RGB_Panel[][] here to set next Frame you want to send 1068 // For Example: 1069 // RGB_Panel[0][0]=0; 1070 // RGB_Panel[0][1]=0; 1071 // RGB_Panel[0][2]=255; 1072 // will update the LED on 1 o'clock position to be blue. 1073 1074 // This following code simulates when you wear a headset. 1075 // RGB_Panel[5][0]=0; 1076 // RGB_Panel[5][1]=255; 1077 // RGB_Panel[5][2]=0; 1078 1079 // int attention=random(0,7); //generates 0~6 1080 // int meditation=random(0,7); 1081 1082 1083 int attention = calculateMeter(eegAttention); 1084 int meditation = calculateMeter(eegMeditation); 1085 1086 1087 for(int i=6; i<=6+attention; i++){ 1088 RGB_Panel[i][0]=255; 1089 RGB_Panel[i][1]=0; 1090 RGB_Panel[i][2]=0; 1091 } 1092 1093 if (meditation==6){ 1094 RGB_Panel[11][0]=0; 1095 RGB_Panel[11][1]=0; 1096 RGB_Panel[11][2]=255; 1097 } 1098 1099 for(int i=4; i>=4-meditation; i--){ 1100 RGB_Panel[i][0]=0; 1101 RGB_Panel[i][1]=0; 1102 RGB_Panel[i][2]=255; 1103 } 1104 1105 if (attention==6) 1106 RGB_Panel[11][0]=255; 1107 1108 for(int i=6+attention; i<=11; i++){ 1109 RGB_Panel[i][0]=0; 1110 RGB_Panel[i][1]=0; 1111 RGB_Panel[i][2]=0; 1112 } 1113 1114 for(int i=4-meditation; i>=0; i--){ 1115 RGB_Panel[i][0]=0; 1116 RGB_Panel[i][1]=0; 1117 RGB_Panel[i][2]=0; 1118 } 1119 1120 1121 } // updateFrame 1122 1123 1124 // ################################################################ 1125 1126 void setOrbitThrottle(int throttle) { 1127 1128 setColorWheel(255,255,0); // Yellow 1129 1130 _throttle=throttle; 1131 setThrottle(); 996 1132 sendCode(formCode(_throttle,_yaw,_pitch)); 997 1133 delay(80); // originally 36, 51, 66, 80 998 999 } // parseSerialInput() 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1134 } // setOrbitThrottle 1135 1136 1137 // ################################################################ 1138 1139 void parseADK() { 1140 1141 // Android ADK 1142 1143 if (acc.isConnected()) { 1144 1145 int len = acc.read(msg, sizeof(msg), 1); 1146 1147 if (! modeADK) { 1148 Serial.println("INFO: ADK connection detected, setting modeADK"); 1149 modeADK = true; 1150 } 1151 1152 // Action taken by Arduino is tied to the message it receives from Android 1153 1154 if (len > 0) { 1155 1156 setColorWheel(255,255,255); // White 1157 1158 if (msg[0] == 0x1) { 1159 // Reserved 1160 } 1161 else if(msg[0] == 0x2) { 1162 // Reserved 1163 } 1164 else if (msg[0] == 0x3) { 1165 // Reserved 1166 } 1167 1168 // EEG Signal 1169 else if (msg[0] == 0x4) { 1170 eegSignal = 0; 1171 if (msg[1] == 0x1) { 1172 eegSignal = 10; 1173 } else if (msg[1] == 0x2) { 1174 eegSignal = 20; 1175 } else if (msg[1] == 0x3) { 1176 eegSignal = 30; 1177 } else if (msg[1] == 0x4) { 1178 eegSignal = 40; 1179 } else if (msg[1] == 0x5) { 1180 eegSignal = 50; 1181 } else if (msg[1] == 0x6) { 1182 eegSignal = 60; 1183 } else if (msg[1] == 0x7) { 1184 eegSignal = 70; 1185 } else if (msg[1] == 0x8) { 1186 eegSignal = 80; 1187 } else if (msg[1] == 0x9) { 1188 eegSignal = 90; 1189 } else { 1190 eegSignal = 100; 1191 } 1192 } 1193 1194 1195 // EEG Attention 1196 else if (msg[0] == 0x5) { 1197 eegAttention = 0; 1198 if (msg[1] == 0x1) { 1199 eegAttention = 10; 1200 } else if (msg[1] == 0x2) { 1201 eegAttention = 20; 1202 } else if (msg[1] == 0x3) { 1203 eegAttention = 30; 1204 } else if (msg[1] == 0x4) { 1205 eegAttention = 40; 1206 } else if (msg[1] == 0x5) { 1207 eegAttention = 50; 1208 } else if (msg[1] == 0x6) { 1209 eegAttention = 60; 1210 } else if (msg[1] == 0x7) { 1211 eegAttention = 70; 1212 } else if (msg[1] == 0x8) { 1213 eegAttention = 80; 1214 } else if (msg[1] == 0x9) { 1215 eegAttention = 90; 1216 } else { 1217 eegAttention = 100; 1218 } 1219 } 1220 1221 1222 // EEG Meditation 1223 else if (msg[0] == 0x6) { 1224 eegMeditation = 0; 1225 if (msg[1] == 0x1) { 1226 eegMeditation = 10; 1227 } else if (msg[1] == 0x2) { 1228 eegMeditation = 20; 1229 } else if (msg[1] == 0x3) { 1230 eegMeditation = 30; 1231 } else if (msg[1] == 0x4) { 1232 eegMeditation = 40; 1233 } else if (msg[1] == 0x5) { 1234 eegMeditation = 50; 1235 } else if (msg[1] == 0x6) { 1236 eegMeditation = 60; 1237 } else if (msg[1] == 0x7) { 1238 eegMeditation = 70; 1239 } else if (msg[1] == 0x8) { 1240 eegMeditation = 80; 1241 } else if (msg[1] == 0x9) { 1242 eegMeditation = 90; 1243 } else { 1244 eegMeditation = 100; 1245 } 1246 } 1247 1248 1249 // updateFrame(eegAttention, eegMeditation, eegSignal); 1250 updateFrameADK(); 1251 1252 1253 // Orbit Throttle 1254 if (msg[0] == 0x7) { 1255 1256 Serial.println(" (msg[0] == 0x7) "); 1257 1258 int throttle = 0; 1259 1260 if (msg[1] == 0x1) { 1261 throttle = _throttle_hover; 1262 Serial.println(" (msg[1] == 0x1) "); 1263 } 1264 else if (msg[1] == 0x0) 1265 Serial.println(" (msg[1] == 0x0) "); 1266 1267 setOrbitThrottle(throttle); 1268 1269 } else { 1270 1271 // sendFrame(1000); 1272 sendFrame(0); 1273 sendCode(formCode(_throttle,_yaw,_pitch)); 1274 delay(80); //cycle(); 1275 } 1276 1277 setColorWheel(255,0,0); // Red 1278 1279 } // len 1280 else { 1281 // setColorWheel(255, 0, 0); // red 1282 delay(80); //cycle(); 1283 } 1284 } // if acc.isConnected() 1285 1286 1287 } // parseADK 1288 1289
Note: See TracChangeset
for help on using the changeset viewer.