View unanswered posts | View active topics It is currently Sun Dec 21, 2014 4:52 am






Reply to topic  [ 3 posts ] 
Problem with bluetooth commands 
Author Message
Rookie

Joined: Tue Jul 12, 2011 10:24 am
Posts: 20
Post Problem with bluetooth commands
Here is the source code on the NXT

Code:
#pragma config(Sensor, S1,     S1,                  sensorTouch)
#pragma config(Sensor, S2,     S2,                  sensorTouch)
#pragma config(Sensor, S4,     S4,                  sensorLightActive)
#pragma config(Motor,  motorA,          motorA,        tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          motorB,        tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorC,          motorC,        tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

const int SENSOR_ERR = 5;                     //Constant for the error range acceptable from the sensor


bool isAround(int sensorVal, int arrayVal){   // function to determine if the sensor value is around the given array value
  return abs(sensorVal - arrayVal) < SENSOR_ERR;
}

void movements1(){                            //code if the light sensor recognizes a value of colorArr[0]
      motor[motorA] = 0;                      //Motor A is stopped
      motor[motorB] = 0;                      //Motor B is stopped
      motor[motorC] = 0;                      //Motor C is stopped

}

void movements2(){                            //if the light sensor recognizes a value of colorArr[1]
    if (SensorValue[S1]==1){                  //if Sensor Touch S1 is pressed these movements
      bMotorFlippedMode[motorB] = 0;          //Motor B forward direction
       motor[motorB] = 40;                     //Motor B power 40
       while(nMotorEncoder[motorB] < 50);      //Motor B 50 degrees target
       motor[motorB] = 0;                      //Motor B is stopped
       bMotorFlippedMode[motorA] = 0;          //Motor A forward direction
       motor[motorA] = 20;                     //Motor A power 20
       while(nMotorEncoder[motorA] < 90);      //Motor A 90 degrees target
       motor[motorA] = 0;                      //Motor A is stopped
       wait1Msec(2000);                        //Wait 2 sec
       bMotorFlippedMode[motorA] = 1;          //Motor A reverse direction
       motor[motorA] = 10;                     //Motor A power 10
       while(nMotorEncoder[motorA] < 0);       //Motor A 0 degrees target
       motor[motorA] = 0;                      //Motor A is stopped
       bMotorFlippedMode[motorB] = 1;          //Motor B reverse direction
       motor[motorB] = 20;                     //Motor B power 20
       bMotorFlippedMode[motorB] = 0;          //Motor B forward direction
       while(nMotorEncoder[motorB] > 0);       //Motor B 0 degrees target
       motor[motorB] = 0;                      //Motor B is stopped
     }

}

void movements3(){                            //if the light sensor recognizes a value of colorArr[2]
     if (SensorValue[S2]==1){                 //if Sensor Touch S2 is pressed these movements
      bMotorFlippedMode[motorB] = 1;          //Motor B reverse direction
      motor[motorB] = 40;                     //Motor B power 40
      while(nMotorEncoder[motorB] < 50);      //Motor B 50 degrees target
      motor[motorB] = 0;                      //Motor B is stopped
      bMotorFlippedMode[motorA] = 0;          //Motor A forward direction
      motor[motorA] = 20;                     //Motor A power 20
      while(nMotorEncoder[motorA] < 90);      //Motor A 90 degrees target
      motor[motorA] = 0;                      //Motor A is stopped
      wait1Msec(3000);                        //Wait 3 sec
      bMotorFlippedMode[motorC] = 0;          //Motor C forward direction
       motor[motorC] = 20;                     //Motor C power 20
       while(nMotorEncoder[motorC] < 95);      //Motor A 95 degrees target
       motor[motorC] = 0;                      //Motor C is stopped
       wait1Msec(1000);                        //Wait 1 sec
       bMotorFlippedMode[motorC] = 1;          //Motor C reverse direction
       motor[motorC] = 20;                     //Motor C power 20
       while(nMotorEncoder[motorC] < 0);       //Motor C 0 degrees target
       motor[motorC] = 0;                      //Motor C is stopped
      bMotorFlippedMode[motorA] = 1;          //Motor A reverse direction
      motor[motorA] = 10;                     //Motor A power 10
      while(nMotorEncoder[motorA] < 0);       //Motor A 0 degrees target
      motor[motorA] = 0;                      //Motor A is stopped
      bMotorFlippedMode[motorB] = 0;          //Motor B forward direction
      motor[motorB] = 20;                     //Motor B power 20
      bMotorFlippedMode[motorB] = 1;          //Motor B reverse direction
      while(nMotorEncoder[motorB] > 0);       //Motor B 0 degrees target
      motor[motorB] = 0;                      //Motor B is stopped
    }
}

void movements4(){                            //if the light sensor recognizes a value of colorArr[3]
      bMotorFlippedMode[motorA] = 0;          //Motor A forward direction
       motor[motorA] = 20;                     //Motor A power 20
       while(nMotorEncoder[motorA] < 90);      //Motor A 90 degrees target
       motor[motorA] = 0;                      //Motor A is stopped
       wait1Msec(2000);                        //Wait 2 sec
       bMotorFlippedMode[motorA] = 1;          //Motor A reverse direction
       motor[motorA] = 10;                     //Motor A is stopped
       while(nMotorEncoder[motorA] < 0);       //Motor A 0 degrees target
       motor[motorA] = 0;                      //Motor A is stopped


}

task main()                                   //Here start the main program
{
  int msgSize;
  byte msg[58];

  nxtDisplayTextLine(0, "   Calibration");    //Displaying text "Calibration"
   nxtDisplayTextLine(2, "Press the orange");  //Displaying text "Press the orange"
   nxtDisplayTextLine(3, "button on the NXT"); //Displaying text "button on the NXT"
   nxtDisplayTextLine(4, "to set the values"); //Displaying text "to set the values"
   while(nNxtButtonPressed == -1);             //take control over the NXT buttons
   while(nNxtButtonPressed == 3);              //press button 3 (the orange button)
   eraseDisplay();                             //Erase the Display

   int colorArr[4];                            //Create and initialize variable colorArr with 4 values

   colorArr[0] = 0;                            //Firs value on colorArr
   colorArr[1] = 0;                            //Second value on colorArr
   colorArr[2] = 0;                            //Third value on colorArr
   colorArr[3] = 0;                            //Fourth value on colorArr

  nxtDisplayTextLine(0, "   Calibration");    //Displaying text "Calibration"
  nxtDisplayTextLine(2, "Press the orange");  //Displaying text "Press the orange"
  nxtDisplayTextLine(3, "button on the NXT"); //Displaying text "button on the NXT"
  nxtDisplayTextLine(4, "to set the value");  //Displaying text "to set the values"
  nxtDisplayTextLine(5, "when there is");     //Displaying text "when there is"
  nxtDisplayTextLine(6, "nothing");           //Displaying text "nothing"
  while(nNxtButtonPressed == -1);             //take control over the NXT buttons
  while(nNxtButtonPressed == 3);              //press button 3 (the orange button)
  colorArr[0] = SensorValue[S4];              //when we press the orange button we set value of colorArr[0] from the Light Sensor S4

  eraseDisplay();                             //Erase the Display
  nxtDisplayTextLine(0, "   Calibration");    //Displaying text "Calibration"
  nxtDisplayTextLine(2, "Press the orange");  //Displaying text "Press the orange"
  nxtDisplayTextLine(3, "button on the NXT"); //Displaying text "button on the NXT"
  nxtDisplayTextLine(4, "to set the value");  //Displaying text "to set the value"
  nxtDisplayTextLine(5, "of the first");      //Displaying text "of the first"
  nxtDisplayTextLine(6, "color");             //Displaying text "color"
  while(nNxtButtonPressed == -1);             //take control over the NXT buttons
  while(nNxtButtonPressed == 3);              //press button 3 (the orange button)
  colorArr[1] = SensorValue[S4];              //when we press the orange button we set value of colorArr[1] from the Light Sensor S4

  eraseDisplay();                             //Erase the Display
  nxtDisplayTextLine(0, "   Calibration");    //Displaying text "Calibration"
  nxtDisplayTextLine(2, "Press the orange");  //Displaying text "Press the orange"
  nxtDisplayTextLine(3, "button on the NXT"); //Displaying text "button on the NXT"
  nxtDisplayTextLine(4, "to set the value");  //Displaying text "to set the value"
  nxtDisplayTextLine(5, "of the second");     //Displaying text "of the second"
  nxtDisplayTextLine(6, "color");             //Displaying text "color"
  while(nNxtButtonPressed == -1);             //take control over the NXT buttons
  while(nNxtButtonPressed == 3);              //press button 3 (the orange button)
  colorArr[2] = SensorValue[S4];              //when we press the orange button we set value of colorArr[2] from the Light Sensor S4

  eraseDisplay();                             //Erase the Display
  nxtDisplayTextLine(0, "   Calibration");    //Displaying text "Calibration"
  nxtDisplayTextLine(2, "Press the orange");  //Displaying text "Press the orange"
  nxtDisplayTextLine(3, "button on the NXT"); //Displaying text "button on the NXT"
  nxtDisplayTextLine(4, "to set the value");  //Displaying text "to set the value"
  nxtDisplayTextLine(5, "of the third");      //Displaying text "of the third"
  nxtDisplayTextLine(6, "color");             //Displaying text "color"
  while(nNxtButtonPressed == -1);             //take control over the NXT buttons
  while(nNxtButtonPressed == 3);              //press button 3 (the orange button)
  colorArr[3] = SensorValue[S4];              //when we press the orange button we set value of colorArr[3] from the Light Sensor S4

  eraseDisplay();                             //Erase the Display
  string s;                                   //make string s
  sprintf(s, "%d %d %d %d", colorArr[0], colorArr[1], colorArr[2], colorArr[3]);  //remember the Value of colorArr[0], Value of colorArr[1], Value of colorArr[2], Value of colorArr[3]
  nxtDisplayTextLine(2, "       Now");        //Displaying text "Now"
  nxtDisplayTextLine(3, "   Robot Sorter");   //Displaying text "Robot Sorter"
  nxtDisplayTextLine(4, "     wait for");     //Displaying text "wait for"
  nxtDisplayTextLine(5, "    details...");    //Displaying text "details..."
  nxtDisplayTextLine(6, s);                   //Displaying text "Value of colorArr[0], Value of colorArr[1], Value of colorArr[2], Value of colorArr[3]"
  while(nNxtButtonPressed == -1);             //take control over the NXT buttons
  while(nNxtButtonPressed == 3);              //press button 3 (the orange button)
  eraseDisplay();                             //Erase the Display

  while(nNxtButtonPressed == -1){             //take control over the NXT buttons
    int sensor = SensorValue[S4];             //Create and initialize variable sensor = SensorValue[S4]
    sprintf(s, "Sensor: %d", sensor);         //remember the Value of SensorValue[S4]
    nxtDisplayTextLine(2, s);                 //Displaying text "Value of SensorValue[S4]"
    int color = -1;                           //Create and initialize variable color = -1
      for(int i = 0; i < 4; i++){               //a for loop that goes from 0 - 3
      if(isAround(sensor, colorArr[i]))       //check if the sensor value is around the value of colorArr[i]
        color = i;                            //initialize color az i
    }

    sprintf(s, "Color: %d", color);           //remember the values of string "color"
    nxtDisplayTextLine(3, s);                 //Displaying text value of the detail which is there

    if (color >= 0){                          //Decide which of the functions to execute
      switch(color){
       switch (cCmdBTCheckStatus(0))
      {
        case 0 :
          movements1();
          break;

        case 1 :
          movements2();
          break;

        case 2 :
          movements3();
          break;

        case 3 :
          movements4();
          break;

         case(ioRsltSuccess):
       // connected;
       nxtDisplayTextLine(2,"   Connected");
       // check for msg in mailbox1;
       msgSize=cCmdMessageGetSize(mailbox1);
       if(msgSize>0)
       {
         cCmdMessageRead(msg, msgSize, mailbox1);
         [color=#FF0000] if(msg[0]==115 && msg[1]==116 && msg[2]==111 && msg[3]==112)[/color] //////////////////////// <- ASCII values of (stop)
         {
            StopAllTasks();
          }
       }
       break;
     case(ioRsltCommChannelNotReady):
       // not connected;
       nxtDisplayTextLine(2,"Disconnected");
       break;
     default:

        default : break;
       }
    }
  }
  {
  while (true)
   {
     nxtDisplayTextLine(1, "%3d, %3d",cCmdBTCheckStatus(0));
     // check for a BT connection

     {

       // Other condition;
       nxtDisplayTextLine(2, "%3d, %3d",cCmdBTCheckStatus(0));
       break;
     }
     wait1Msec(10);
   }
 }
}


the red means when I send command from my PC application "stop" - everything must be stop... BUT when I finish with the calibration and everything, when I send command stop it doesen't stop... where actually is the problem?!


Fri Jul 29, 2011 5:39 am
Profile
Rookie
User avatar

Joined: Tue Nov 01, 2011 9:18 pm
Posts: 49
Location: Davenport, Iowa
Post Re: Problem with bluetooth commands
:shock: I think that the problem may be that your program is way too long or complicated. I'm not entirely sure what you're trying to program it to do, but I'm thinking that you managed to completely confuse me, a veteran programmer! :eek: You should try to simplify it if possible, and check for spelling errors in the programming. That may help a lot!


Thu Nov 17, 2011 11:14 am
Profile WWW
Rookie
User avatar

Joined: Tue Nov 01, 2011 9:18 pm
Posts: 49
Location: Davenport, Iowa
Post Re: Problem with bluetooth commands
Maybe I should read the complete program before I post. :idea:

So, you are trying to calibrate the sensors on the NXT? I really should have looked at it better before I posted... I'm sorry, but I have very limited experience with what you are trying to do here. I shouldn't even have posted! :?


Thu Nov 17, 2011 1:20 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.