bool checkBTLinkConnected2() { if (nBTCurrentStreamIndex >= 0) return(true); // An existing Bluetooth connection is present. nxtDisplayCenteredTextLine(3, "Attempting"); nxtDisplayCenteredTextLine(4, "To Connect"); string pinnumber = "1234"; setDefaultPIN(pinnumber); // setSessionPIN(pinnumber); bBTSkipPswdPrompt = true; // connect to GPS, here... btConnect(1, "PETER-PC"); while (bBTBusy) { wait1Msec(1); } if (nBTCurrentStreamIndex >= 0) return(true); // An existing Bluetooth connection is present. nxtDisplayCenteredTextLine(3, "PETER-PC not"); nxtDisplayCenteredTextLine(4, "Connected"); wait1Msec(3000); // StopAllTasks(); return(false); }
// // checkLink() // -- check if there is at least one connected // -- if not, try 3times to reconnnect // -- if it still fails after 3 attempts, stop all tasks // -- return to caller // -- if it is successful or already have one queue connected to a remote unit // -- void checkLink() { int nTries=0; setFriendlyName(MASTER); while (nTries<3 && nBTCurrentStreamIndex < 1) { PlaySound(soundLowBuzz); PlaySound(soundLowBuzz); eraseDisplay(); nxtDisplayCenteredTextLine(3, "BT Not Connected!"); nxtDisplayCenteredTextLine(4, "Connecting!"); btConnect(1, ENGINE); wait1Msec(1000); ++nTries; } if (nBTCurrentStreamIndex < 1)// -- check if there is at least one connected StopAllTasks(); // exit the entire process while (bBTBusy) { wait1Msec(100); } nxtDisplayCenteredTextLine(3, "StreamIndex=%d", nBTCurrentStreamIndex); nxtDisplayCenteredTextLine(4, "Connect to Q %d", Queue1); wait1Msec(1000); return; }
void init() { NLINES = 8; *sensors = { .TLEFT_P = EV3_PORT_1, .COLOR_P = EV3_PORT_2, .ULTRA_P = EV3_PORT_3, .TRIGHT_P = EV3_PORT_4, .LEFT_P = EV3_PORT_A, .RIGHT_P = EV3_PORT_D }; shared_memory->yellow = 0; shared_memory->blue = 0; shared_memory->red = 0; set_font(EV3_FONT_MEDIUM); // Motor init ev3_motor_config(sensors->LEFT_P, LARGE_MOTOR); ev3_motor_config(sensors->RIGHT_P, LARGE_MOTOR); // Sensor init ev3_sensor_config(sensors->ULTRA_P, ULTRASONIC_SENSOR); ev3_sensor_config(sensors->COLOR_P, COLOR_SENSOR); ev3_sensor_config(sensors->TLEFT_P, TOUCH_SENSOR); ev3_sensor_config(sensors->TRIGHT_P, TOUCH_SENSOR); btConnect(); }
task main() { char inValue = 0; char inLastValue = 0; string PIN = 1234; char nIndex = 0; char oIndex = 0; int touch01 = 0; int touch02 = 0; int sonar01 = 0; const int kHexDigitsPerLine = 2; ubyte MarkerRead[kHexDigitsPerLine]; ubyte SensorRead[kHexDigitsPerLine]; int usDistance = 0; //Establish arduino connection btConnect(3, "SeeedBTSlave"); wait1Msec(1000); setSessionPIN(PIN); nxtDisplayCenteredBigTextLine(2, "Testing!"); wait1Msec(5000); //Check that the connection is live checkBTLinkConnected(); //Push bluecore into RAW mode nxtDisplayCenteredTextLine(4, "Before RAW"); setBluetoothRawDataMode(); while (!bBTRawMode) { nxtDisplayCenteredTextLine(4, "Inside RAW"); wait1Msec(1); } nxtDisplayCenteredTextLine(4, "After RAW"); //Send some data to the connected arduino ubyte count[3]; ubyte count2[3]; count[0]=70; count[1]= 65; count[2]= 66; count2[0]=65; count2[1]=70; count2[2]=71; nxtWriteRawBluetooth(count, sizeof(count)); wait1Msec(1000); nxtWriteRawBluetooth(count2, sizeof(count2)); wait1Msec(1000); eraseDisplay(); while(true) { nxtDisplayCenteredTextLine(1, "Reading"); string str = ""; nxtReadRawBluetooth(&MarkerRead[1], 2); inValue = MarkerRead[1]; if (inValue == 63) { nxtReadRawBluetooth(&SensorRead[1], 2); nIndex = SensorRead[1]; if ( ( nIndex >= 65 ) && ( nIndex <= 70 ) ) { if (nIndex == 65 ){touch01 = 1;nxtDisplayTextLine(2, "One is ON: %d", touch01);} else if (nIndex == 66 ){touch01 = 0;nxtDisplayTextLine(2, "One is OFF: %d", touch01);} else if (nIndex == 67 ){touch02 = 1;nxtDisplayTextLine(3, "Two is ON: %d", touch02);} else if (nIndex == 68 ){touch02 = 0;nxtDisplayTextLine(3, "Two is OFF: %d", touch02);} else if (nIndex == 69 ){sonar01 = 0;nxtDisplayTextLine(4, "Sonar1 OUT: %d", sonar01);} else if (nIndex == 70 ){sonar01 = 1;nxtDisplayTextLine(4, "Sonar1 IN: %d", sonar01);} nIndex = oIndex; } } } }
task main(){ btConnect(1, "1-DaRWIn"); wait1Msec(3000); btConnect(3, "3-DaRWIn"); wait1Msec(7000); }