Offline
Newbie
Karma: 0
Posts: 6
|
 |
« on: October 30, 2012, 12:50:48 pm » |
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  Code section below. 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
God Member
Karma: 21
Posts: 703
|
 |
« Reply #1 on: October 30, 2012, 01:03:12 pm » |
'C' string comparisons are NOT done with the '==' comparsion operator.
Use the 'C' library function 'strcmp' instead.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 314
Posts: 35507
Seattle, WA USA
|
 |
« Reply #2 on: October 30, 2012, 01:07:58 pm » |
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
God Member
Karma: 21
Posts: 703
|
 |
« Reply #3 on: October 30, 2012, 01:23:42 pm » |
As PaulS stated in the future please post the complete sketch after applying formatting. But assuming that 'button' is a global declared as … char* buuton;
… your code should be … 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
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #4 on: October 30, 2012, 01:28:05 pm » |
lloyddean,
Many thanks for the constructive pointer.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #5 on: October 30, 2012, 01:41:56 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 314
Posts: 35507
Seattle, WA USA
|
 |
« Reply #6 on: October 30, 2012, 01:43:58 pm » |
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
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #7 on: October 30, 2012, 01:53:39 pm » |
Again, many thanks.
For completeness I have attached the entire code , formatted using Auto Format, to close out the thread.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13897
Lua rocks!
|
 |
« Reply #8 on: October 30, 2012, 03:20:46 pm » |
Is it solved? You need, for example: } while (strcmp (button, "Accept") != 0 || strcmp (button, "Reject") != 0);
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9401
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #9 on: October 30, 2012, 03:56:27 pm » |
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
|
|
|
|
|
Des Moines, WA - USA
Offline
God Member
Karma: 21
Posts: 703
|
 |
« Reply #10 on: October 30, 2012, 04:40:18 pm » |
Is it solved? You need, for example: } 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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13897
Lua rocks!
|
 |
« Reply #11 on: October 30, 2012, 05:07:36 pm » |
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
God Member
Karma: 21
Posts: 703
|
 |
« Reply #12 on: October 30, 2012, 05:45:05 pm » |
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
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13897
Lua rocks!
|
 |
« Reply #13 on: October 30, 2012, 06:32:56 pm » |
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
Tesla Member
Karma: 89
Posts: 6388
-
|
 |
« Reply #14 on: October 30, 2012, 07:23:33 pm » |
Syntax/Capability escapes me here, any gentle pointers would be greatly appreciated  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
|
|
|
|
|
|