UK
Offline
Newbie
Karma: 1
Posts: 22
|
 |
« on: October 06, 2012, 02:39:10 pm » |
I'm playing with the example sketchs using an Uno board and 1.01 and was looking at endsWith to read a string. But the example doesn't work for me. startsWith seems OK though.
Can anyone else confirm? I get the following results regardless of whether the sensor ends with 0.
HTTP/1.1 200 OK Server's using http version 1.1 Got an OK from the server sensor = 10. This reading is not divisible by ten
String startsWith() and endsWith(): HTTP/1.1 200 OK Server's using http version 1.1 Got an OK from the server sensor = 28. This reading is not divisible by ten
String startsWith() and endsWith(): HTTP/1.1 200 OK Server's using http version 1.1 Got an OK from the server sensor = 4. This reading is not divisible by ten
String startsWith() and endsWith(): HTTP/1.1 200 OK Server's using http version 1.1 Got an OK from the server sensor = 0. This reading is not divisible by ten
The actual code from the demo.
Can anyone else confirm if I'm going mad.
/* String startWith() and endsWith() Examples of how to use startsWith() and endsWith() in a String created 27 July 2010 by Tom Igoe This example code is in the public domain. */
void setup() { Serial.begin(9600); Serial.println("\n\nString startsWith() and endsWith():");
}
void loop() { // startsWith() checks to see if a String starts with a particular substring: String stringOne = "HTTP/1.1 200 OK"; Serial.println(stringOne); if (stringOne.startsWith("HTTP/1.1")) { Serial.println("Server's using http version 1.1"); } // you can also look for startsWith() at an offset position in the string: stringOne = "HTTP/1.1 200 OK"; if (stringOne.startsWith("200 OK", 9)) { Serial.println("Got an OK from the server"); } // endsWith() checks to see if a String ends with a particular character: String sensorReading = "sensor = "; sensorReading += analogRead(0); Serial.print (sensorReading); if (sensorReading.endsWith(0)) { Serial.println(". This reading is divisible by ten"); } else { Serial.println(". This reading is not divisible by ten");
}
// do nothing while true: while(true); }
|
|
|
|
|
Logged
|
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 96
Posts: 6376
|
 |
« Reply #1 on: October 06, 2012, 02:45:33 pm » |
if (sensorReading.endsWith(0)) {
Perhaps you meant: if (sensorReading.endsWith("0")) {
or maybe if (sensorReading.endsWith('0')) {
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #2 on: October 06, 2012, 02:47:32 pm » |
Is it just me, but this seems over complicated, when a simple % operator would do the job?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
UK
Offline
Newbie
Karma: 1
Posts: 22
|
 |
« Reply #3 on: October 06, 2012, 02:52:12 pm » |
John
Thanks for the quick reply. This is the example sketch that came with the Arduino not code I have written
"0" doesn't compile StringStartsWithEndsWith.cpp: In function 'void loop()': StringStartsWithEndsWith:33: error: invalid conversion from 'const char*' to 'uint8_t' StringStartsWithEndsWith:33: error: initializing argument 1 of 'int analogRead(uint8_t)'
and '0' doesn't work. It is possible you could try it and see if the function is actually working.
|
|
|
|
|
Logged
|
|
|
|
|
Austin, TX
Offline
Faraday Member
Karma: 41
Posts: 5171
CMiYC
|
 |
« Reply #4 on: October 06, 2012, 04:11:11 pm » |
"0" doesn't compile StringStartsWithEndsWith.cpp: In function 'void loop()': StringStartsWithEndsWith:33: error: invalid conversion from 'const char*' to 'uint8_t' StringStartsWithEndsWith:33: error: initializing argument 1 of 'int analogRead(uint8_t)'
Compiled and work fine for me in this code: void setup() { Serial.begin(9600); }
void loop() { String sensorReading = "sensor = "; sensorReading += analogRead(A0); Serial.print (sensorReading); if (sensorReading.endsWith("0")) { Serial.println(". This reading is divisible by ten"); } else { Serial.println(". This reading is not divisible by ten"); } delay(250); }
As I expected sensorReading.endsWith(0) did not work.
|
|
|
|
|
Logged
|
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 96
Posts: 6376
|
 |
« Reply #5 on: October 06, 2012, 04:56:22 pm » |
The argument to endsWith() is a "const String reference". I think it tries to take the argument, in this case a constant 0, and construct a String out of it. In theory the constant should be treated as an 'int' and should thus be converted to a decimal String "0". I think that the constant 0 may be taken as a 'char'. In that case the a two-byte buffer would be constructed with the character (null) and a null terminator. The resulting string would be empty. I suspect that is what is happening and why the "endsWith(0)" always return false.
Perhaps (int)0 would work.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #6 on: October 06, 2012, 04:58:08 pm » |
I still think "% 10" is a much simpler solution.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
UK
Offline
Newbie
Karma: 1
Posts: 22
|
 |
« Reply #7 on: October 07, 2012, 02:31:08 am » |
John
Sorry I changed the wrong 0 in my rush to test and get out of the house last night and as you say it compiles and work fine if I use "0" As this is the distributed example is it worth letting Arduino know?
AWOL
Could you explain %10 as I was unable to find any reference to it. I assume it's means if it's divisible by 10 but I can't work out the syntax
Rob
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #8 on: October 07, 2012, 03:26:41 am » |
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
UK
Offline
Newbie
Karma: 1
Posts: 22
|
 |
« Reply #9 on: October 07, 2012, 04:51:26 am » |
AWOL
Thanks for the pointer I couldn't get a result searching for % on the web or board. I will go and have a play.
Rob
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 71
Posts: 6625
Arduino rocks
|
 |
« Reply #10 on: October 07, 2012, 04:57:26 am » |
The argument to endsWith() is a "const String reference". I think it tries to take the argument, in this case a constant 0, and construct a String out of it. In theory the constant should be treated as an 'int' and should thus be converted to a decimal String "0". I think that the constant 0 may be taken as a 'char'. In that case the a two-byte buffer would be constructed with the character (null) and a null terminator. The resulting string would be empty. I suspect that is what is happening and why the "endsWith(0)" always return false.
Perhaps (int)0 would work.
In C and C++ anywhere a pointer or reference is accepted the null pointer is also accepted. The null pointer is notated as 0.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 28
|
 |
« Reply #11 on: October 07, 2012, 12:56:42 pm » |
In C and C++ anywhere a pointer or reference is accepted the null pointer is also accepted. The null pointer is notated as 0. Pointers can be NULL (or 0) but references can not. In this case I believe the compiler decided it could take your 0 and turn it into a string. However, looking at the reference for String, this should work.
|
|
|
|
|
Logged
|
|
|
|
|
|