indexOf( int ) ????

Hi im trying to use indexof to check the incoming data against an int val. not having much luck.

int check0 = 45784;

if( msg.indexOf( check0 ) >= 0 ){
do something
}

any help would be great ?

If its done like this it works but thats a fixed value in the code how can i read the incoming data against a int value ?

if( msg.indexOf( "45784" ) >= 0 ){
do something
}

vmansmx5:
If its done like this it works but thats a fixed value in the code how can i read the incoming data against a int value ?

if( msg.indexOf( "45784" ) >= 0 ){
do something
}

I assume msg is a STRING object? If so, then you should/cannot. A string object is a container for a string of characters, a 0 or nul is a special case that is used to mark the end of the string, so having an int of 12 embedded would have two binary values embedded in the string, 0x0c 0x00. This won't work, part of the value of the int would be used as a terminator for the string.

Now, if what you are actually asking, "how can I have String.indexOf() find a group of characters that are the 'printable' representation of an integer value?"

That I can answer,
first convert the int value into an array of characters,
then pass that array of characters to indexOf() as the search parameter.

// create a 20character buffer to hold the printable representation of the int. 
//if you make the buffer smaller than needed, it can/will overwrite other variables.

char ch[20];

// convert intvalue to a decimal (base10) printable array of characters in ch. 

sprintf(ch,"%d",intvalue); 

// will return the first occurrence of the digit sequence, or -1 if not present.

int i = String.indexOf(ch);

Now realize something else, if your are looking for "12", and the string contains "9120", the indexOf() function will return 1, because it is not looking for the value 12, just an array of character '1','2'.

Chuck.

Not sure how to get this working i keep getting this ERROR
STRING_CHECK:11: error: expected primary-expression before ‘.’ token

#include <SoftwareSerial.h>
#include <String.h>
char inchar; 
char data[20];
SoftwareSerial GPRS(7, 8);
String msg = String("");
long checknumber = 0;
int Count = 0;

char ch[20];
int i = String.indexOf(ch);

void setup()
{
  GPRS.begin(19200);
  delay(10);
  Serial.begin(19200);
  delay(10);
}
void loop()
{
  
  sprintf(ch,"%d",checknumber);
  
  while(GPRS.available() >0)
  {
   
    inchar=(unsigned char)GPRS.read();
    delay(10);
    msg += String(inchar);
    if(Count == 64)break;
  }
    if( msg.indexOf( ch ) >= 0 ){

    Serial.println("its working");
    
    //////////////////////////////////////////////////////////
    }
    ClearGprsMsg();
}
void ClearGprsMsg(){
  msg = "";
  Count=0;
}

STRING_CHECK.ino: In function ‘void loop()’:
STRING_CHECK:34: error: expected primary-expression before ‘.’ token
what am i doing wrong here?

#include <SoftwareSerial.h>
#include <String.h>
char inchar; 
char data[20];
SoftwareSerial GPRS(7, 8);
String msg = String("");
long checknumber = 0;
int Count = 0;
char ch[20];
void setup()
{
  GPRS.begin(19200);
  delay(10);
  Serial.begin(19200);
  delay(10);
}
void loop()
{
  while(GPRS.available() >0)
  {
    inchar=(unsigned char)GPRS.read();
    delay(10);
    msg += String(inchar);
    if(Count == 64)break;
  }
    sprintf(ch,"%d",checknumber);
    int num = String.indexOf(ch);
    if( msg.indexOf( num ) >= 0 ){
    Serial.println("its working");
    }
    ClearGprsMsg();
}
void ClearGprsMsg(){
  msg = "";
  Count=0;
}

Hi im trying to use indexof to check the incoming data against an int val. not having much luck.

You capture the bytes that arrive in a String, then have indexOf() look for that byte string in what was received.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

//A very simple example of sending a string of characters 
//from the serial monitor, capturing the individual 
//characters into a String, then evaluating the contents 
//of the String to possibly perform an action (on/off board LED).

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if(readString.indexOf("on") >=0)
    {
      digitalWrite(ledPin, HIGH);
      Serial.println("LED ON");
    }

    if(readString.indexOf("off") >=0)
    {
      digitalWrite(ledPin, LOW);
      Serial.println("LED OFF");
    }

    readString="";
  } 
}

Yes i can do that but what im tring to do is read in the data then match it against an int value
Example

int check0 = 45784;

if( msg.indexOf( check0 ) >= 0 ){
do something
}

Yes i can do that but what im tring to do is read in the data then match it against an int value

Simple servo serial code that captures the character string in a String, then converts the captured numeric character string into an interger.

//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
  Serial.println("servo-test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    int n = readString.toInt();  //convert readString into a number
    Serial.println(n); //so you can see the integer
    myservo.write(n);
    readString="";
  } 
}

what am i doing wrong here?

The line that generates the error is:

    int num = String.indexOf(ch);

indexOf() is not a static member of the String class. It needs to be called from an instance of the String class.

    int num = msg.indexOf(ch);

thanks PaulS this works great now

#include <SoftwareSerial.h>
#include <String.h>
char inchar;
char data[20];
SoftwareSerial GPRS(7, 8);
String msg = String("");
long checknumber = 598;
int Count = 0;
char ch[20];
void setup()
{
  GPRS.begin(19200);
  delay(10);
  Serial.begin(19200);
  delay(10);
  Serial.println("on");
}
void loop()
{
  while(Serial.available() >0)
  {
    inchar=(unsigned char)Serial.read();
    delay(10);
    msg += String(inchar);
    if(Count == 64)break;
  }
    sprintf(ch,"%d",checknumber);
    
    if( msg.indexOf( ch ) >= 0 ){
    Serial.println("its working");
    }
    ClearGprsMsg();
}
void ClearGprsMsg(){
  msg = "";
  Count=0;
}

Would you explain this:

String msg = String("");

versus

  msg = "";

There really is no point in constructing a String instance, and then using the equal operator to copy the value to the msg instance, and then deleting the temporary instance, when the equal operator is capable of dealing with a string.

Problem this works only is the is up to 5 numbers long NUMBER = 12345; but if i try any larger number is when it dosnt work anymore NUMBER = 123456;
? how can i use lager numbers up to 10 NUMBER = 1234567890;

#include <String.h>
char inchar;
char data[20];
String msg = String("");
long NUMBER = 12345;
int Count = 0;
char ch[40];
void setup()
{
  Serial.begin(19200);
  delay(10);
  Serial.println("on");
}
void loop()
{
  while(Serial.available() >0)
  {
    inchar=(unsigned char)Serial.read();
    delay(10);
    msg += String(inchar);
    if(Count == 64)break;
    sprintf(ch,"%d",NUMBER);
  }   
    if( msg.indexOf( ch ) >= 0 ){
    Serial.println("its working");
    }
    ClearGprsMsg();
}
void ClearGprsMsg(){
  msg = "";
  Count=0;
}
    if(Count == 64)break;

Count will NEVER equal 64.

    sprintf(ch,"%d",NUMBER);

%d is NOT the proper format specifier for long. That is for int. %ld is for long.