Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. After checking main If condition unable to reach if and while
Qt 6.12 LTS is out! See what's new in the release blog

After checking main If condition unable to reach if and while

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
2 Posts 2 Posters 616 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hardware reading from scope Thread stored in beforeRightValue. Before that is coming inside void Motion::_Grid() car will stop 2 seconds. Sometimes values will not match so that time I want to calculate error value then I want to make repositioning.

      float degree_check; // calculating error value will store here.
    

    I have created 4 conditions. All condition will check first stored value from hardware (firstdataValue) and current value(beforeRightValue). If any difference happened that will calculate the error value (degree_check), then the car need to move same first degree depends upon the error degree.

    That is working fine. That is coming inside if condition. For example condition 1 if, condition 4 if printing. If that is coming inside of main if condition, this condition also true only, but that is not coming inside of this if condition, not inside of the while. I was unable to understand the mistake.

    void Motion::_Grid()
    {
      float beforeRightValue = ScopeThread::data; 
    
      if((int)beforeRightValue != (int)firstdataValue) 
        {
          float degree_check; // calculating error value will store here.
    
            if(firstdataValue > 0 && beforeRightValue > 0) 
            {
              degree_check = (firstdataValue)-(beforeRightValue); 
    
              if(degree_check > 0)
                {
                  cout<<"condition 1 if : "<<degree_check<<endl;
    
                  if(!beforeRightValue == firstYawValue) 
                    {
                        softPwmWrite(Motr2, 0);
                        softPwmWrite(Motr1, 0);
                        delay(200);
    
                        softPwmWrite(Motr1,5);
                        softPwmWrite(Motr2,0);
    
                       while(true)
                        {
    		        cout<<"2nd while : "<<firstdataValue<<endl;
    
                          if(int(ScopeThread::data) == firstdataValue)
                            {
                                softPwmWrite(Motr2, 0);
                                softPwmWrite(Motr1, 0);
                                delay(2000);
                                break;
                            }
                          break;
                        }
                    }
                }
    
              else if(degree_check < 0)
                {
                   cout<<"condition 1 else : "<<degree_check<<endl;
    
                    if(int(ScopeThread::data) != (int)firstdataValue)
                    {
                        softPwmWrite(Motr2, 0);
                        softPwmWrite(Motr1, 0);
                        delay(200);
    
                        softPwmWrite(Motr1,0);
                        softPwmWrite(Motr2,5);
    
                       while(true)
                        {
                          if(int(ScopeThread::data) == firstdataValue)
                            {
                                cout<<"2nd while : "<<firstdataValue<<endl;
    
                                softPwmWrite(Motr2, 0);
                                softPwmWrite(Motr1, 0);
                                delay(2000);
                                break;
                            }
                          break;
                        }
                    }
                }
            }
    
            else if(firstdataValue < 0 && beforeRightValue < 0)
            {
              degree_check = (firstdataValue) - (beforeRightValue);
    
              if(degree_check < 0)
                {
                  cout<<"condition 2 if : "<<degree_check<<endl;
    
                    if(int(ScopeThread::data) != (int)firstdataValue)
                    {
                        softPwmWrite(Motr2, 0);
                        softPwmWrite(Motr1, 0);
                        delay(200);
    
                        softPwmWrite(Motr1,5);
                        softPwmWrite(Motr2,0);
    
                       while(true)
                        {
                          if(int(ScopeThread::data) == firstdataValue)
                            {
                                cout<<"3rd while : "<<firstdataValue<<endl;
    
                                softPwmWrite(Motr2, 0);
                                softPwmWrite(Motr1, 0);
                                delay(2000);
                                break;
                            }
                          break;
                        }
                    }
                }
    
              else if(degree_check > 0)
                {
                    cout<<"condition 2 else : "<<degree_check<<endl;
    
                    if(int(ScopeThread::data) != (int)firstdataValue)
                    {
                        softPwmWrite(Motr2, 0);
                        softPwmWrite(Motr1, 0);
                        delay(200);
    
                        softPwmWrite(Motr1,0);
                        softPwmWrite(Motr2,5);
    
                       while(true)
                        {
                          if(int(ScopeThread::data) == firstdataValue)
                            {
                                cout<<"4th while : "<<firstdataValue<<endl;
    
                                softPwmWrite(Motr2, 0);
                                softPwmWrite(Motr1, 0);
                                delay(2000);
                                break;
                            }
                          break;
                        }
                    }
                }
            }
    
            else if(firstdataValue > 0 && beforeRightValue < 0)
            {
              degree_check = (firstdataValue) - (beforeRightValue);
    
              if(beforeRightValue < -90)
                {
                  cout<<"condition 3 if : "<<degree_check<<endl;
    
                    if(int(ScopeThread::data) != (int)firstdataValue)
                    {
                        softPwmWrite(Motr2, 0);
                        softPwmWrite(Motr1, 0);
                        delay(200);
    
                        softPwmWrite(Motr1,5);
                        softPwmWrite(Motr2,0);
    
                       while(true)
                        {
                          if(int(ScopeThread::data) == firstdataValue)
                            {
                                cout<<"5th while : "<<firstdataValue<<endl;
    
                                softPwmWrite(Motr2, 0);
                                softPwmWrite(Motr1, 0);
                                delay(2000);
                                break;
                            }
                          break;
                        }
                    }
                }
    
              else if(beforeRightValue > -90 && beforeRightValue <= 0)
                {
                    cout<<"condition 3 else : "<<degree_check<<endl;
    
                    if(int(ScopeThread::data) != (int)firstdataValue)
                    {
                        softPwmWrite(Motr2, 0);
                        softPwmWrite(Motr1, 0);
                        delay(200);
    
                        softPwmWrite(Motr1,0);
                        softPwmWrite(Motr2,5);
    
                       while(true)
                        {
                          if(int(ScopeThread::data) == firstdataValue)
                            {
                                cout<<"6th while : "<<firstdataValue<<endl;
    
                                softPwmWrite(Motr2, 0);
                                softPwmWrite(Motr1, 0);
                                delay(2000);
                                break;
                            }
                          break;
                        }
                    }
                }
            }
    
            else if(firstdataValue < 0 && beforeRightValue > 0)
            {
              degree_check = (firstdataValue) - (beforeRightValue);
    
              if(beforeRightValue > 90)
                {
                    cout<<"condition 4 if : "<<degree_check<<endl;
    
                    if(int(ScopeThread::data) != (int)firstdataValue)
                    {
                        softPwmWrite(Motr2, 0);
                        softPwmWrite(Motr1, 0);
                        delay(200);
    
                        softPwmWrite(Motr1,0);
                        softPwmWrite(Motr2,5);
    
                       while(true)
                        {
                          if(int(ScopeThread::data) == firstdataValue)
                            {
                                cout<<"7th while : "<<firstdataValue<<endl;
    
                                softPwmWrite(Motr2, 0);
                                softPwmWrite(Motr1, 0);
                                delay(2000);
                                break;
                            }
                          break;
                        }
                    }
                }
    
              else if(beforeRightValue < 90 && beforeRightValue >= 0)
                {
                  cout<<"condition 3 if : "<<degree_check<<endl;
    
                    if(int(ScopeThread::data) != (int)firstdataValue)
                    {
                        softPwmWrite(Motr2, 0);
                        softPwmWrite(Motr1, 0);
                        delay(200);
    
                        softPwmWrite(Motr1,5);
                        softPwmWrite(Motr2,0);
    
                       while(true)
                        {
                          if(int(ScopeThread::data) == firstdataValue)
                            {
                                cout<<"8th while : "<<firstdataValue<<endl;
    
                                softPwmWrite(Motr2, 0);
                                softPwmWrite(Motr1, 0);
                                delay(2000);
                                break;
                            }
                          break;
                        }
                    }
                }
            }
        }
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      I'd recommend splitting your function in smaller pieces. That will make it easier to understand. Your use of ScopeThread::data also isn't really clear.

      Since you are controlling hardware, did you consider using the state machine framework ? That would allow to more cleanly model your car behaviour.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt
      • Unsolved