Println question

Hi there,

I'm trying to send to the Serial monitor some data like this, but without any luck...

Serial.println("Tag value: " + TagID);

The TagID is defined as:

char TagID[12];

Thank you for the help! :wink:

You are trying to concatenate Strings but TagID is an array of chars, not a String.
As you have not posted your program we don't know how TagID is created. If it is terminated with a zero (ie a C style string) then try this

Serial.print("Tag value: ");
Serial.println(TagID);

Here is my code:

#include <SoftwareSerial.h>

SoftwareSerial RFID(2, 3);

int LedOk = 12;
int LedEr = 13;
int LedDelay = 1000;

int i;
int RawRfIdData = 0;
unsigned int readData[12];
int Count = -1;
char TagID[12];
char* AuthTags[4][4];


void InitAuthTags() {
 AuthTags[0][0] = "22004C22FFB3";
 AuthTags[0][1] = "22004CD971C6";
 AuthTags[0][2] = "22004C0F4627";
 AuthTags[0][3] = "22004BDA02B1";

 AuthTags[1][0] = "Peter"; 
 AuthTags[1][1] = "Julia"; 
 AuthTags[1][2] = "Josh"; 
 AuthTags[1][3] = "Paula"; 
}

void setup() {
 RFID.begin(9600);
 Serial.begin(9600);
 pinMode(LedOk, OUTPUT);
 pinMode(LedEr, OUTPUT);
 digitalWrite(LedOk, LOW);
 digitalWrite(LedEr, LOW);
 InitAuthTags();
}

int CheckTag() {
 for (i = 0; i < 4; ++i) {
  if (strcmp(AuthTags[0][i], TagID) == 0) {
   return 1;
  }
 }
 return 0;
}


void ParseTag() {
 int x;
 for (x = 0; x < 12; ++x) {
  TagID[x] = readData[x];
 }
 TagID[12] = 0;
}

void ProcessTag() {
 ParseTag();
 //PrintTag();
 
 if (CheckTag() == 1) {
  TagSuccess();
 } else {
  TagFailed();
 }
}

void PrintTag() {
 Serial.print("Tag value: ");
 Serial.println(TagID);
}

void TagSuccess() {
 Serial.println("Tag authorized.");
 Serial.print("Welcome ");
 Serial.println(AuthTags[1][i]);
 digitalWrite(LedOk, HIGH);
 digitalWrite(LedEr, LOW);
 delay(LedDelay);
 SerialCls();
}

void TagFailed() {
 Serial.println("Unauthorized access!");
 digitalWrite(LedOk, LOW);
 digitalWrite(LedEr, HIGH);
 delay(LedDelay);
 SerialCls();
}

void SerialCls() {
 while(RFID.available() > 0)
 {
  RFID.read();
 }
 
 Serial.flush();
}

void loop() {
 digitalWrite(LedOk, LOW);
 digitalWrite(LedEr, LOW);
 
 if (RFID.available() > 0) {
	RawRfIdData = RFID.read();
		
	if (RawRfIdData == 2) {
	 Count = 0;
	} 
		
 else if (RawRfIdData == 3) {
	ProcessTag();
  SerialCls();
	Count = -1;
 }
 
 else if (Count >= 0) {
	readData[Count] = RawRfIdData;
	++Count;
	} 
 }
}

So, I wish to print those data in one line like:

Serial.println("Tag value: " + TagID);

and

 Serial.println("Welcome " + AuthTags[1][i]);

or how can I convert those data to be able to achieve my goal?

how can I convert those data to be able to achieve my goal?

It may be more effort to convert your data than to do it in 2 separate print commands but have a look at the sprintf() function and you decide

char TagID[] = {"1234567890"};
char buffer[25];

void setup()
{
  Serial.begin(115200);
  sprintf(buffer, "Tag Value: %s", TagID);
  Serial.println(buffer);
}

void loop() {}

OMG, you saved me a lot of trouble, It's working like a boss, thanks to you UKHeliBob! 8)

One more thing, an Off-topic question if we are here...

How can I implement accidental double reading ?

e.g. To hold the last TagID and always compare to new one.

A little help would be great because I don't have any Java or C++ background, I'm a VB6 geek. :grinning:

Then you need a new variable to record "previous tag" and save the current tag into that after it's successfully recognised.

This variable can probably be local to loop(), so it should be declared as static so that it is stored in between loops.

Yeah, I figured that out, but I don't know where to put it! :frowning:

On the arduino, Serial.print( ) in general will print only one thing. It is not Java.

Serial.println("Tag value: " + TagID);

// should be

Serial.print("Tag value: ");
Serial.println( TagID );

The exception to this, is where you are using concatenation of two objects of the String class.

michinyon:
On the arduino, Serial.print( ) in general will print only one thing. It is not Java.

Yeah, I see it now!

So instead of the concatenate the strings with sprintf() the version of michinyon is better (faster, less memory).