Go Down

Topic: String Comparison - Not Equal To problem in While Loop (Read 912 times) previous topic - next topic

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: [Select]
              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;
                            }

}

lloyddean

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

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

PaulS

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.

lloyddean

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

Code: [Select]

char*   buuton;


… your code should be …

Code: [Select]

    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;
    }
}


lloyddean,

Many thanks for the constructive pointer.

Apologies for not putting the entire code in, I was not able to as it's larger than 9500 characters and therefore wont post  =(  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.

PaulS

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.

Again, many thanks.

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

Nick Gammon

Is it solved? You need, for example:

Code: [Select]

    } while (strcmp (button, "Accept") != 0 || strcmp (button, "Reject") != 0);
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

robtillaart


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 -
Rob Tillaart

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

lloyddean

#10
Oct 30, 2012, 10:40 pm Last Edit: Oct 30, 2012, 10:51 pm by lloyddean Reason: 1

Is it solved? You need, for example:

Code: [Select]

   } 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 …

Nick Gammon

Sorry, but the part I saw without scrolling my code box had the non-example code. Your example was further down.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

lloyddean

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.

Nick Gammon

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.).
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up