Pages: [1] 2   Go Down
Author Topic: String Comparison - Not Equal To problem in While Loop  (Read 616 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've looked, scratched my head and referenced but can't find the answer which I'm sure is easy (please be gentle with me!):

I am trying to increment a value when a button is pressed.  buttonPress () returns a String value of: Left, Right, Up, Down, Accept or Reject and the below code quite happily increases or decreases the pulseCount variable and displays it as appropriate.  I am struggling to break out of the While loop when Accept or Reject is pressed, Serial Monitor shows the correct string is being passed from buttonPress ().  This means that my function wont break out once I have set number I need and changing the While has indicated that this is the halt.

Syntax/Capability escapes me here, any gentle pointers would be greatly appreciated  smiley-red  Code section below.

Code:
              clearScreen ();
                       lcd.print("Setup Menu - Active");
                       lcd.setCursor(0, 2);
                       lcd.print("Pulse Count: ");
                       lcd.setCursor(13, 2);
                       lcd.print(pulseCount);
                       do
                       {
                         increment(pulseCount,1);
                         lcd.setCursor(13, 2);
                         lcd.print(pulseCount);
                         Serial.print(button);  //Debug Code
                       }
                       while (button != "Accept" || button != "Reject");
                       if (button == "Reject")
                         {
                         menuSetup1();
                         }
                       lcd.setCursor(0,3);
                       lcd.print("Trigger Set to: ");
                       lcd.setCursor(16,3);
                       lcd.print(pulseCount);
                      buttonPress ();
                      if (button == "Reject"){menuTop();}                       
}   

int increment (int startValue, int value){
                               
                buttonPress ();
                if (button == "Up")
                  {
                  pulseCount = pulseCount + value;
                  }
                  else if(button =="Down")
                        {
                        pulseCount = pulseCount - value;
                        }
                        else{
                              return pulseCount;
                            }

}
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

'C' string comparisons are NOT done with the '==' comparsion operator.

Use the 'C' library function 'strcmp' instead.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46107
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Snippets-r-us is down the road a ways. Here, we need to see ALL of your code, no matter how poorly formatted.

Of course, if you used Tools + Auto Format first, that would be even better.
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As PaulS stated in the future please post the complete sketch after applying formatting.  But assuming that 'button' is a global declared as …

Code:
char*   buuton;

… your code should be …

Code:
    clearScreen ();
    lcd.print("Setup Menu - Active");
    lcd.setCursor(0, 2);
    lcd.print("Pulse Count: ");
    lcd.setCursor(13, 2);
    lcd.print(pulseCount);

    do
    {
        increment(pulseCount, 1);

        lcd.setCursor(13, 2);
        lcd.print(pulseCount);

        Serial.print(button);  //Debug Code
    } while (button != "Accept" || button != "Reject");
   
   
    if ( 0 == strcmp(button, "Reject") )
    {
        menuSetup1();
    }

    lcd.setCursor(0, 3);
    lcd.print("Trigger Set to: ");
    lcd.setCursor(16, 3);
    lcd.print(pulseCount);

    buttonPress();
    if ( 0 == strcmp(button, "Reject") )
    {
        menuTop();
    }
}   

int increment(int startValue, int value)
{
    buttonPress();
    if ( 0 == strcmp(button, "Up") )
    {
        pulseCount = pulseCount + value;
    }
    else if ( 0 == strcmp(button, "Down") )
    {
        pulseCount = pulseCount - value;
    }
    else
    {
        return pulseCount;
    }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

lloyddean,

Many thanks for the constructive pointer.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Apologies for not putting the entire code in, I was not able to as it's larger than 9500 characters and therefore wont post  smiley-cry  In future, do I need to break the code down into more than one post to get it onto the forum?

Thanks again for the pointer.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46107
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
In future, do I need to break the code down into more than one post to get it onto the forum?
No. Just below the text window, there is a link called Additional Options... Select that, and one of the things you can do is attach one or more files that are larger than the 9500 character limit.

Even better, though, is to create a smaller sketch that illustrates the problem.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Again, many thanks.

For completeness I have attached the entire code , formatted using Auto Format, to close out the thread.

* FlashTrigger02c.ino (14.01 KB - downloaded 6 times.)
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it solved? You need, for example:

Code:
    } while (strcmp (button, "Accept") != 0 || strcmp (button, "Reject") != 0);
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12462
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Note there also exist the  stricmp(stringA, stringB)   that ignores the case of the char arrays.

on this site you see many more libs/functions to use on Arduino - http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html -
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it solved? You need, for example:

Code:
   } while (strcmp (button, "Accept") != 0 || strcmp (button, "Reject") != 0);

He gave a code fragment and I wanted him to extrapolate to the rest of his code …
« Last Edit: October 30, 2012, 04:51:05 pm by lloyddean » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, but the part I saw without scrolling my code box had the non-example code. Your example was further down.
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looking at his posted project he's going to need a little, OK a LOT, more help then my simple posting provides.

The Arduino 1.x (and presumably later) 'String' class ought to be taken out back and buried.  It's constantly being used and abused by those who don't know any better.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh, I see. He's using String. That's what comes from posting snippets.

Please note that, at present, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11173
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Syntax/Capability escapes me here, any gentle pointers would be greatly appreciated  smiley-red  Code section below.

If the button is being specified elsewhere in your code then return it as an enumerated value instead of as a string or String. Then you can use a plain old numeric comparison (or use it in switch cases etc) without having to do string comparisons everywhere.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1] 2   Go Up
Jump to: