String comparison

Hopefully I am just doing something really stupid but I can't get the string comparison to work. I have tried with const, define, string with and without the extra space at the end but I just can't get the first or second if to run

#include <String.h>
#include <NewSoftSerial.h>

#define enable_code "3E0018105F69 "  //change this ID with your own card TAG
#define disable_code "3E00187E97CF "

NewSoftSerial RFID(2, 3);
String msg = String(60);
String ID = String(60); //string to store allowed cards
char c;

void setup()  
{
  Serial.begin(9600);
  Serial.println("Serial Ready");

  RFID.begin(9600);
  Serial.println("RFID Ready");
}

void loop()
{
  while(RFID.available()>0)
  {
    c=RFID.read(); 
    msg += c;
    Serial.println(msg);  //Uncomment to view your tag ID
  }
  if(msg == enable_code) add();
  if(msg == disable_code) del();  
  if(msg.length()>10) verifica();
  msg="";
}

void add()
{
  Serial.print("What TAG do you wanna grant access?: ");
  msg="";
  while(msg.length()<13)
  {
    while(RFID.available()>0)
    {
      c=RFID.read(); 
      msg += c;
    }
  }
  if(ID == msg)
  {
    Serial.println("\nAccess already granted for this card.");
    msg="";
  }
  else
  {
    Serial.print("Card: ");
    Serial.println(msg); 
    ID += msg;
    ID += ",";
    Serial.print("ID: ");
    Serial.println(ID);
    msg="";
    Serial.println("Access granted for this card.");
  }
}

void del()
{
  msg="";
  Serial.print("What TAG do you wanna deny access?: ");
  while(msg.length()<13)
  {
    while(RFID.available()>0)
    {
      c=RFID.read(); 
      msg += c;
    }
  }
  if(ID == msg)
  {
    Serial.println(msg);
    Serial.println("TAG found. Access for this card denied.");
    //ID.replace(card,"");
    int pos=ID.indexOf(msg);
    msg="";
    msg += (ID.substring(0,pos));
    msg += (ID.substring(pos+15,ID.length()));
    ID="";
    ID += msg;
    //Serial.print("ID: ");
    //Serial.println(ID);
  } else Serial.println("\nTAG not found or already denied");
  msg="";
}

void verifica()
{
    if(ID == msg) Serial.println("Access granted.");
    else Serial.println("Access denied.");
}

I am guessing i am doing something really dumb but i have searched and searched and can't find an answer.

Hi,

I may be completely wrong about this, but it looks all wrong, and somewhat Java-esque.

C is an old language, and you cannot just compare strings using ==. The == will just see if both strings start at the same memory address. You need to use strcmp.

But, you seem to be creating strings using +=, does this compile? As far as I am aware, you have to do all that kind of thing the hard way, writing things into buffers. I wouldn't even try to do any dynamic memory allocation on an Arduino. You can't afford memory leaks when you only have 1K :slight_smile:

When you install WString in your IDE you will get 8 examples with it - do look at them!

It compiles fine with arduino 19 (strings is included in 19) and the string contains the correct data it is the first 2 if statements that do not seem to come back true but the last one does and prints the access denied error.

The info I got for the string.h is from the arduino website I do not have the wstring

The info I got for the string.h is from the arduino website I do not have the wstring

http://www.arduino.cc/en/Tutorial/TextString

Still doesn't match on it.

#include <String.h>
#include <NewSoftSerial.h>

#define enable_code "3E0018105F69 "  //change this ID with your own card TAG
#define disable_code "3E00187E97CF "

NewSoftSerial RFID(2, 3);
String msg = String(60);
String ID = String(60); //string to store allowed cards
char c;

void setup()  
{
  Serial.begin(9600);
  Serial.println("Serial Ready");

  RFID.begin(9600);
  Serial.println("RFID Ready");
}

void loop()
{
  while(RFID.available()>0)
  {
    c=RFID.read(); 
    msg += c;
    Serial.println(msg);  //Uncomment to view your tag ID
  }
  if(msg.equals("3E0018105F69")) {add();}
  if(msg == disable_code) del();  
  if(msg.length()>10) verifica();
  msg="";
}

void add()
{
  Serial.print("What TAG do you wanna grant access?: ");
  msg="";
  while(msg.length()<13)
  {
    while(RFID.available()>0)
    {
      c=RFID.read(); 
      msg += c;
    }
  }
  if(ID == msg)
  {
    Serial.println("\nAccess already granted for this card.");
    msg="";
  }
  else
  {
    Serial.print("Card: ");
    Serial.println(msg); 
    ID += msg;
    ID += ",";
    Serial.print("ID: ");
    Serial.println(ID);
    msg="";
    Serial.println("Access granted for this card.");
  }
}

void del()
{
  msg="";
  Serial.print("What TAG do you wanna deny access?: ");
  while(msg.length()<13)
  {
    while(RFID.available()>0)
    {
      c=RFID.read(); 
      msg += c;
    }
  }
  if(ID == msg)
  {
    Serial.println(msg);
    Serial.println("TAG found. Access for this card denied.");
    //ID.replace(card,"");
    int pos=ID.indexOf(msg);
    msg="";
    msg += (ID.substring(0,pos));
    msg += (ID.substring(pos+15,ID.length()));
    ID="";
    ID += msg;
    //Serial.print("ID: ");
    //Serial.println(ID);
  } else Serial.println("\nTAG not found or already denied");
  msg="";
}

void verifica()
{
    if(ID == msg) Serial.println("Access granted.");
    else Serial.println("Access denied.");
}

and the serial output

3

3E

3E0

3E00

3E001

3E0018

3E00181

3E001810

3E0018105

3E0018105F

3E0018105F6

3E0018105F69

3E0018105F69
Access denied.

adding string from String() - Arduino Reference then adding contains instead of equals as below

if(msg.contains("3E0018105F69")) 
{
add();
}

as per the example code in the string folder gives the following error when trying to compile

RFID.cpp: In function 'void loop()':
rfid:28: error: 'class String' has no member named 'contains'

Trying to run the example code from the libraries/String/examples/StringContains

gives the following errors

C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:34: error: prototype for 'String::String(int)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:39: error: candidates are: String::String(long unsigned int, int)
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:38: error: String::String(long int, int)
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:37: error: String::String(unsigned int, int)
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:36: error: String::String(int, int)
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:35: error: String::String(unsigned char)
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:34: error: String::String(char)
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:33: error: String::String(const String&)
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:32: error: String::String(const char*)
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp: In constructor 'String::String(const char*)':
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:50: error: '_array' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:52: error: 'setArray' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp: In copy constructor 'String::String(const String&)':
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:60: error: '_array' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:61: error: 'clear' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:62: error: 'const class String' has no member named '_array'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:62: error: 'setArray' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp: In member function 'const String& String::operator=(const String&)':
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:77: error: '_array' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:83: error: 'clear' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:84: error: 'const class String' has no member named '_array'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:84: error: 'setArray' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp: At global scope:
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:90: error: prototype for 'const String& String::operator=(const char*)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:70: error: candidate is: const String& String::operator=(const String&)
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:107: error: prototype for 'const String& String::operator+=(const char*)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:44: error: candidate is: const String& String::operator+=(const String&)
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:127: error: prototype for 'const String& String::operator+=(char)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:44: error: candidate is: const String& String::operator+=(const String&)
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:145: error: prototype for 'const String& String::operator+=(int)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:44: error: candidate is: const String& String::operator+=(const String&)
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:159: error: prototype for 'const String& String::operator+=(long int)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:44: error: candidate is: const String& String::operator+=(const String&)
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp: In member function 'const String& String::operator+=(const String&)':
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:176: error: '_array' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:179: error: 'setArray' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:183: error: '_array' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:183: error: 'const class String' has no member named '_array'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp: At global scope:
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:191: error: no 'const String& String::append(char)' member function declared in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:196: error: no 'const String& String::append(char*)' member function declared in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:201: error: no 'const String& String::append(const String&)' member function declared in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:206: error: no 'const String& String::append(int, int)' member function declared in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:217: error: no 'const String& String::append(long int, int)' member function declared in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:226: error: no 'const String& String::append(int)' member function declared in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:234: error: no 'const String& String::append(long int)' member function declared in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:323: error: prototype for 'char String::charAt(int)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:57: error: candidate is: char String::charAt(unsigned int) const
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:339: error: prototype for 'void String::setCharAt(int, char)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:71: error: candidate is: void String::setCharAt(unsigned int, char)
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:355: error: prototype for 'boolean String::equals(char*)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:60: error: candidate is: unsigned char String::equals(const String&) const
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:366: error: prototype for 'boolean String::equals(const String&)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:60: error: candidate is: unsigned char String::equals(const String&) const
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:372: error: no 'boolean String::contains(char*)' member function declared in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:380: error: prototype for 'byte* String::getBytes()' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:79: error: candidate is: void String::getBytes(unsigned char*, unsigned int)
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:386: error: no 'void String::setArray(const char*)' member function declared in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:396: error: prototype for 'int String::indexOf(int)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:65: error: candidates are: int String::indexOf(const String&, unsigned int) const
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:64: error: int String::indexOf(const String&) const
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:63: error: int String::indexOf(char, unsigned int) const
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:62: error: int String::indexOf(char) const
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:402: error: prototype for 'int String::indexOf(const String&)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:65: error: candidates are: int String::indexOf(const String&, unsigned int) const
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:64: error: int String::indexOf(const String&) const
C:\P

Below is some code I made for testing. I added the if contains function to find if a string contains "woohoo". You can send strings containing woohoo using the serial monitor to see if it is found in the string.

//zoomkat 9-9-10 simple delimited ',' string parce 
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter
// http://www.arduino.cc/en/Tutorial/TextString for WString.h

#include <WString.h> //provides easy string handling
String readString = String(100);

void setup() {
      Serial.begin(9600);
        }

void loop() {

        //expect a string like wer,qwe rty,123 456,hyre kjhg,
        //or like hello world,who are you?,bye!,
        while (Serial.available()) {
        delay(10);  //small delay to allow input buffer to fill
          if (Serial.available() >0) {
        char c = Serial.read();  //gets one byte from serial buffer
        if (c == ',') {break;}  //breaks out of capture loop to print readstring
        readString.append(c); } //makes the string readString
        }
      
      if (readString.length() >0) {
      Serial.println(readString); //prints string to serial port out
      
         if(readString.contains("woohoo")) {
         Serial.println("I found woohoo!"); }
      
      readString=""; //clears variable for new input
      }
   }

Where do you get wstring. the textstring page now contains string not wstring.

Where do you get wstring. the textstring page now contains string not wstring.

Wstring is inside the string.zip file you download from the textstring page.

I download string.zip again and it contains a folder called string with the wstring.h and wstring.cpp in it. If I include wstring.h as per your example it errors out with

C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:34: error: prototype for 'String::String(int)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:39: error: candidates are: String::String(long unsigned int, int)
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:38: error: String::String(long int, int)
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:37: error: String::String(unsigned int, int)
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:36: error: String::String(int, int)
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:35: error: String::String(unsigned char)
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:34: error: String::String(char)
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:33: error: String::String(const String&)
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:32: error: String::String(const char*)
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp: In constructor 'String::String(const char*)':
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:50: error: '_array' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:52: error: 'setArray' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp: In copy constructor 'String::String(const String&)':
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:60: error: '_array' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:61: error: 'clear' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:62: error: 'const class String' has no member named '_array'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:62: error: 'setArray' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp: In member function 'const String& String::operator=(const String&)':
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:77: error: '_array' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:83: error: 'clear' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:84: error: 'const class String' has no member named '_array'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:84: error: 'setArray' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp: At global scope:
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:90: error: prototype for 'const String& String::operator=(const char*)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:70: error: candidate is: const String& String::operator=(const String&)
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:107: error: prototype for 'const String& String::operator+=(const char*)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:44: error: candidate is: const String& String::operator+=(const String&)
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:127: error: prototype for 'const String& String::operator+=(char)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:44: error: candidate is: const String& String::operator+=(const String&)
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:145: error: prototype for 'const String& String::operator+=(int)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:44: error: candidate is: const String& String::operator+=(const String&)
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:159: error: prototype for 'const String& String::operator+=(long int)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:44: error: candidate is: const String& String::operator+=(const String&)
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp: In member function 'const String& String::operator+=(const String&)':
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:176: error: '_array' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:179: error: 'setArray' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:183: error: '_array' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:183: error: 'const class String' has no member named '_array'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp: At global scope:
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:191: error: no 'const String& String::append(char)' member function declared in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:196: error: no 'const String& String::append(char*)' member function declared in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:201: error: no 'const String& String::append(const String&)' member function declared in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:206: error: no 'const String& String::append(int, int)' member function declared in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:217: error: no 'const String& String::append(long int, int)' member function declared in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:226: error: no 'const String& String::append(int)' member function declared in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:234: error: no 'const String& String::append(long int)' member function declared in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:323: error: prototype for 'char String::charAt(int)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:57: error: candidate is: char String::charAt(unsigned int) const
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:339: error: prototype for 'void String::setCharAt(int, char)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:71: error: candidate is: void String::setCharAt(unsigned int, char)
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:355: error: prototype for 'boolean String::equals(char*)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:60: error: candidate is: unsigned char String::equals(const String&) const
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:366: error: prototype for 'boolean String::equals(const String&)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:60: error: candidate is: unsigned char String::equals(const String&) const
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:372: error: no 'boolean String::contains(char*)' member function declared in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:380: error: prototype for 'byte* String::getBytes()' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:79: error: candidate is: void String::getBytes(unsigned char*, unsigned int)
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:386: error: no 'void String::setArray(const char*)' member function declared in class 'String'
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:396: error: prototype for 'int String::indexOf(int)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:65: error: candidates are: int String::indexOf(const String&, unsigned int) const
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:64: error: int String::indexOf(const String&) const
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:63: error: int String::indexOf(char, unsigned int) const
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:62: error: int String::indexOf(char) const
C:\Program Files (x86)\arduino-0019\libraries\String\WString.cpp:402: error: prototype for 'int String::indexOf(const String&)' does not match any in class 'String'
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:65: error: candidates are: int String::indexOf(const String&, unsigned int) const
C:\Program Files (x86)\arduino-0019\hardware\arduino\cores\arduino/WString.h:64: error:

wstring seems to work in arduino 18 but not arduino 19

#include <WString.h>
#include <NewSoftSerial.h>

//#define enable_code "3E0018105F69 "  //change this ID with your own card TAG
//#define disable_code "3E00187E97CF "

NewSoftSerial RFID(2, 3);
String msg = String(60);
String ID = String(60); //string to store allowed cards
char c;

void setup()  
{
  Serial.begin(9600);
  Serial.println("Serial Ready");

  RFID.begin(9600);
  Serial.println("RFID Ready");
}

void loop()
{
  while(RFID.available()>0)
  {
    c=RFID.read(); 
    msg += c;
    Serial.println(msg);  //Uncomment to view your tag ID
  }
  if(msg.contains("E0018105F69")) add();
  if(msg.contains("E00187E97CF")) del();  
  if(msg.length()>10) verifica();
  msg="";
}

void add()
{
  Serial.print("What TAG do you wanna grant access?: ");
  msg="";
  while(msg.length()<13)
  {
    while(RFID.available()>0)
    {
      c=RFID.read(); 
      msg += c;
    }
  }
  if(ID == msg)
  {
    Serial.println("\nAccess already granted for this card.");
    msg="";
  }
  else
  {
    Serial.print("Card: ");
    Serial.println(msg); 
    ID += msg;
    ID += ",";
    Serial.print("ID: ");
    Serial.println(ID);
    msg="";
    Serial.println("Access granted for this card.");
  }
}

void del()
{
  msg="";
  Serial.print("What TAG do you wanna deny access?: ");
  while(msg.length()<13)
  {
    while(RFID.available()>0)
    {
      c=RFID.read(); 
      msg += c;
    }
  }
  if(ID == msg)
  {
    Serial.println(msg);
    Serial.println("TAG found. Access for this card denied.");
    //ID.replace(card,"");
    int pos=ID.indexOf(msg);
    msg="";
    msg += (ID.substring(0,pos));
    msg += (ID.substring(pos+15,ID.length()));
    ID="";
    ID += msg;
    //Serial.print("ID: ");
    //Serial.println(ID);
  } else Serial.println("\nTAG not found or already denied");
  msg="";
}

void verifica()
{
    if(ID == msg) Serial.println("Access granted.");
    else Serial.println("Access denied.");
}

Works in arduino 18. I guess I'll carry on looking at 19 and try and figure out why the wstring library doesn't work although I see they added the string class in 19

wstring seems to work in arduino 18 but not arduino 19

From what I've read, version 19 has string handling, but good code examples aren't really available yet. 19 also apparently changes something such that an additional library is needed when using the ethernet library, which just increases some overhead for me.

I can cofirm string is in 19. I can not however find any mention of the contains string operation and the equals is not working correctly for me. (at least not with my crappy code lol)

I think equals must be a numeric only operator hence the if never setting to 'true'. If this is the case and the string class in arduino 19 doesn't have a 'contains' operator can anyone think of another string operator that I could use in the place of the equals/contains?

As mentioned in a previous post; when comparing strings in C you can't use == or conditional operators like you would with numeric values. The string library may have a built-in function to compare it's string to another, not certain of that, but you may want to take a look here: avr-libc: <string.h>: Strings

The function you want to use, in your case, is "strcmp" which is string compare.

Regards,

Eric

With the new String class in arduino 19 you can use indexof to compare. I have this working now.

Works in arduino 18. ...I see they added the string class in 19

To use String class in '19, you may need to remove your old libraries/String directory. (See Footnote.)

Then open Arduino and look at the examples menu.

You see some numbered examples


1.  Basics
2.  Digital
.
.
.
8. Strings

Try the examples in that Strings library. For example, look at the StringComparisonOperators sketch. The overloaded '==' and '!=' operators work just fine (as do '<', '>', '<=' , '>=).
Note that the operators work correctly, but the part of the example code that uses the compareTo() function has the logic backwards. The function works the way it is described in the reference (it just uses the standard library function strcmp() on the String buffers), but the sketch reports the results incorrectly.

On the other hand...

Some of the old functions are no longer there. Depending on your spirit of adventure (and, maybe, depending on permission of some IT administrator/weenie for some working environments), you may have the option of adding them.

For example I find the old cstr() function valuable to feed a String object's value to a function like SdFat file.open() that needs a const pointer to char, but, alas, cstr() is no longer part of the String class. I don't know whether it was a deliberate design decision to omit this (and, if so, I have to ask: why?), or an oversight. Anyhow, I added such a function with a single line in the public section of the new WString.h.

    const char * cstr() const {return _buffer;}

Or you may simply have to live with the new stuff or, maybe, delay upgrading to 0019 until enough people have contributed "fixes" to make it worthwhile (and maybe less painful) to convert your legacy code.

That's not a hardship for me since I don't have any legacy code that uses the old String library. The first two things that I tried didn't work and when I started looking at the code I found lots of things not to like. (Hacked up code that apparently was designed to fix previous problems with memory a memory leak. Stuff like that.)

Fundamentally I just don't like writing code for limited-resources CPUs (like the '328p) that depends on dynamic memory allocation, even if the library itself were perfect. It's too easy to get into the habit of writing as if there were no limits and letting the compiler take care of things that the embedded systems programmer should really keep track of. (But maybe that's just me. I'm funny that way.)

Regards,

Dave

Footnote:
If your libraries/String directory is local to your sketchbook, don't just rename the directory. Move it to somewhere else while you are running arduino-0019 so that the brilliantly helpful Arduino startup stuff won't find it and get confused. If you want to be able to revert to arduino-0018, then you can move the old Strings directory to the arduino-0018/libraries directory so that -0019 won't find it.