contains() and getChars() don´t work fine

Not because It Works "contains" and "getChars ()" does not work well, what fault is this code? Thanks

int LED = 13;   // select the pin for the LED

char mystring[32]= "hola";

#include "WString.h"

#define maxLength 512

String buffer1 = String(maxLength);       // allocate a new String
String buffer2 = String(maxLength);       // allocate a new String

// definition of instance of GSM class


#include "SparkSoftLCD.h"

// LCD transmit pin
#define LCD_TX 12

SparkSoftLCD lcd = SparkSoftLCD(LCD_TX);


void setup() {
  pinMode(LED,OUTPUT);   // declare the LED's pin as output
 
  Serial.begin(115200);        // connect to the serial port

  
  // setup lcd

  pinMode(LCD_TX, OUTPUT);
  lcd.begin(9600);
  lcd.clear();

}

void loop () {
  
 buffer1 = "00:14:a4:8b:76:9e";
 buffer2 ="INQUIRY_PARTIAL 00:14:a4:8b:76:9e 72010c";
 
 lcd.clear();

 if( buffer2.contains(buffer1)){
 lcd.clear();
 lcd.print("MAC Not found!!!");
 delay(2000);
 }
 else{
 lcd.clear();
 lcd.print("MAC in memory");
 delay(2000);
 }

 lcd.print( buffer1.getChars());
  
  
  
  delay(2000);
  lcd.clear();
  lcd.print("OK");
  delay(2000);
  lcd.clear();
 


}

What micro are you using? You have allocated 1k memory for the Strings...

What version of the string library are you using? There are a number of bugs in earlier version that have been corrected in the latest version (1.0.9, I think).

Me, I've got 0.9 (?)

@bono96576

"contains" ... does not work well

Hmmm...

@bono96576

if( buffer2.[glow]contains[/glow](buffer1)){

lcd.clear();
lcd.print("MAC [glow]Not[/glow] found!!!");

Ummm---don't you mean something like the following?

if([glow]![/glow]buffer2.contains(buffer1)){
 lcd.clear();
 lcd.print("MAC [glow]Not[/glow] found!!!");

"getChars ()" does not work well

It seems to work for me with code similar to your sketch.

So...

Here's the drill:

  • Tell us what happens with the getChars() function in your sketch? (What does your program actually do?)

  • Tell us what you expected to happen?

  • Tell us what you do not understand about the difference.

I mean, really, how can you expect for us to be able to guess how to help you if you only tell us it "does not work well"??? Be specific!

Regards,

Dave

sorry I uploaded the code is wrong. I have it more advanced and I have drawn an example of WString.h library (version 0.9) which is on the page. This is the code:

#include "WString.h" // include the String library version 0.9

#define maxLength 512

String buffer1 = String(maxLength); // allocate a new String
String buffer2 = String(maxLength); // allocate a new String
int i=0;
int leng;

#include "SparkSoftLCD.h"

// LCD transmit pin
#define LCD_TX 12

SparkSoftLCD lcd = SparkSoftLCD(LCD_TX);

void setup() {
// open the serial port:
Serial.begin(115200);

// setup lcd

pinMode(LCD_TX, OUTPUT);
lcd.begin(9600);
lcd.clear();
}

void loop () {

buffer1 = ""; // Clear buffer1
lcd.clear();
lcd.print("Command mode OK");
delay(2000);
lcd.clear();
lcd.print("Searching device");
Serial.println("INQUIRY 3");
delay(3600);
if (Serial.available() > 0) {
while(Serial.available() > 0){
char inChar = Serial.read();
inString.append(inChar);
}

leng = buffer1.length();
lcd.clear();
lcd.print(leng); //Was INQUIRY answer send?
delay(2000);
lcd.clear();
lcd.print("inquiry 3 activated");
delay(2000);

buffer2= "c8:7e:75:20:8a:0f"; //MAC address of my mobile //The next step, get the mac address of the WT11 module
if (buffer1.contains(buffer2)) { //Check if my address is detected
lcd.clear();
lcd.print("Device detected!");
delay(2000);
buffer1 = "";
}
else{
lcd.clear();
lcd.print("Device not found");
delay(2000);
buffer1 = "";
}

}

lcd.print(buffer2); // How i can print a String on my LCD?
delay(2000);
lcd.clear();

You will see what he does is very simple.

I'm using Bluegiga WT11 module in command mode. And every so often I make a check if a phone is near. If the search response encuentor my MAC address, open the door, but sense it, I do nothing.

I'm using the Arduino Duemilanove (ATMega328), I am trying to make a lock bluetooht that when it detects the phone near a door opens. I am working with strings and I use "getChars ()" to make this statement:

lcd.print (buffer1.getChars ())

Because if I put directly:

lcd.print (buffer1)

does not work.

I'm starting and everything is new.

@bono96576

This is the code:

Well, you aren't using that code (It won't compile, since it doesn't declare or define the variable inString), and I can't see where it would be productive for us to guess at what you a really using.

Furthermore...

I don't know what it is that you would like some help with.
You originally said that contains() and getChars() don't work fine. Is that still the topic?

I pointed out the error in your use of the contains() function. (So, now, does it "work fine" or not?)

I also said that the String getChars() function works for me and asked you to illustrate exactly what does not "work fine" for you.

I mean, the lcd print() function doesn't take a String variable as an argument, but it can take a pointer to char, right? That's what the String getChars() member function is there for, right? So, as my old pal Larry King says from time to time: What's the question? Does getChars() "work fine"? Or not?

Here's a suggestion: Make a simple (small) sketch that exactly illustrates your problem and doesn't have a bunch of extra stuff that (more-or-less) works.

Then

  • Post the exact code that you are using. Make sure it compiles and executes.

  • Tell us exactly what you expect the program to do.

  • Tell us exactly what the program does (or does not do) that is different from your expectations.

  • Tell us exactly what you don't understand about the difference.

Bottom line: Be specific!

Regards,

Dave