Serial.print problem

Hi there I am having some problem transferring some numbers between two arduinos with two xbees. On the transmitter side I have the following code:

int A = 10;
int B = 20;
int C = 30;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
Serial.println(A, DEC);
delay(1000);
Serial.println(B, DEC);
delay(1000);
Serial.println(C, DEC);
delay(1000);
}

On the receiver side I have the following code

int Number;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
if (Serial.available() > 0) {
  
  Number = Serial.read();
  
  
  Serial.println(Number, DEC);
  delay(1000);
 
}
}

There is communication between both xbees but i keep getting random numbers. I attached a snapshot of the values I keep getting.

This is part of a larger problem where I am trying to send the values of an MQ7 gas sensor to another arduino. Please help if you can.

I believe your receiving code requires an array to buffer the data. You can then read out the values by using a delimiter.

Also be aware that you are sending the decimal form of the character, so you should expect to receive a numeric value.

13 carriage return 10 linefeed 49 ASCII code for 1 48 ASCII code for 0 13 10 50 ASCII code for 2 48 ASCII code for 0

etc etc Can you see the relationship between what you are sending and what you are receiving ? Do a Google search on ASCII for more explanation.

Also be aware that you are sending the decimal form of the character, so you should expect to receive a numeric value.

No, he is sending characters representing numbers, not the numbers themselves.

So, how to fix it ? For a simple print output, declare the the number variable as a byte or char instead of an int. read a character when one becomes available and if it is not 13 or 10 subtract 48 from it and print it.

Serial.print(number - 48);

When you get a 10 or a 13 print a new line.

You can also put the numbers received into a variable but get the simple print working first so that you understand what is going on.

So, how to fix it ?

Or, use Serial.write() if the values are all byte-sized.

UKhelibob I tried your suggestion but i ended up not receiving anything. All it did was print a new line when a 10 0r 13 was received.

This is part of a larger problem where i am trying to send the reading from a gas sensor from one arduino to the other so i don't really know what's going wrong.

This is the current set up at the transmitter side:

byte A = 10;
byte B = 20;
byte C = 30;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
Serial.println(A, DEC);
delay(1000);
Serial.println(B, DEC);
delay(1000);
Serial.println(C, DEC);
delay(1000);
}

This is the set up at the receiver side:

byte number;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
if (Serial.available() > 0) {
  
  number = Serial.read();
  
  if (number = 13 ) {
    Serial.println();
  }
  if (number = 10 ) {
    Serial.println();
  }
  else
  
  Serial.println(number - 48);
  delay(1000);

}
}

This is what i am currently receiving. Please check the attachments.

While the arduino is receiving the correct information. it is printing the information wrongly so instead of
10
20
30
it prints as
1
0
2
0
3
0

after a while it changes down to
1
2
3

completely eliminating the zeros. Could this have anything to do with the speed?

While the arduino is receiving the correct information. it is printing the information wrongly so instead of 10 20 30 it prints as 1 0 2 0 3 0

That will be because you programmed it to print a newline after each character not just when you got a 10 or 13.

Serial.println(number - 48);

Do you understand the difference between Serial.print() and Serial.println() ?

Thanks UKhelibob that was an oversight on my part but this is the issue now. It prints everything on one line. i will at least like to seperate them

10-35-3820-35-3830-35-38

I also tried to get rid of the -35 and -38 which as you said is a result of the 10 and 13. Will a

if (Serial.read = 13)

work?

At a certain point it just prints 123123123123123? do you have any idea why?

  if (number = 13 ) {

This is ASSIGNing the value 13 to the variable number in the context of an if statement. There is a 99.999999999% certainty that this is NOT what you want to do.

= != ==

if (Serial.read = 13)

No. That does not do a Serial.read() and even if it did it would not compare it with 13 because of the single assignment operator = instead of the comparison operator ==

You nearly had it working in your previous program. When you get a 10 or a 13 do a Serial.println() to get a new line. For everything else do a Serial.print() to keep the output on the same line, but you need to fix the single = as well.

Hi PaulS and Ukhelibob. I’ve been able to make some progress using both your advice. See the attachment to see an image. The code is currently like this:

byte number;

void setup() {
  // put your setup code here, to run once:
Serial.begin(38400);
}

void loop() {
  // put your main code here, to run repeatedly:
if (Serial.available() > 0) {
  
    
  number = Serial.read();
    if (number == 13) {
    Serial.println();
    }
     else if (number == 10) {
    Serial.println();
      }  
  else
  Serial.print(number - 48);
  delay(1000);
 
      
    
}
}

It works fine until a certain point in time where it just prints 1234123412341234 on a straight line. Do you know any reason why it could be doing that?

Do you know any reason why it could be doing that?

I do:

  delay(1000);

Since your code is doing nothing unless there is serial data to deal with, this is plain stupid.

Learn to NOT ever use this function.

Thanks PaulS and UKhelibob. You're lifesavers.

I'll be trying the code now with an MQ7 gas sensor so hopefully it works. Thanks again

The way that you are doing it now just prints the value that you receive from the Serial monitor input but it is difficult to do anything with the value as it is. I can imagine that when reading the gas sensor you may want to do something, say display a warning if the value is above/below a certain level or sound an alarm is the value is more extreme. If that is the case then you will need to use a different technique to read and interpret the input.

Well yes that's the aim. I would like to set a threshold level and digitalWrite an alarm High or LOW if the gas level is above it. Do you have any ideas how that could work?

Do you have any ideas how that could work?

Yes. Save the characters as they arrive in an array of chars. When you receive an "end of message" marker, perhaps '>', that you sent from the transmitter put a zero in the next array position. Now you will have a C style string (not a [u]S[/u]tring) and you can use the atio() function to convert it to a real number to be used in the comparison.

Serial input basics should help.

Below is some simple serial test code I've used to communicate between two arduinos. You miht try it to see if you can get communication in your setup.

//zoomkat 3-5-12 simple delimited ',' string tx/rx 
//from serial port input (via serial monitor)
//and print result out serial port
//Connect the sending arduino rx pin to the receiving arduino rx pin. 
//Connect the arduino grounds together. 
//What is sent to the tx arduino is received on the rx arduino.
//Open serial monitor on both arduinos to test

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >0) {
        Serial.print(readString); //prints string to serial port out
        Serial.println(','); //prints delimiting ","
        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

Hi there so i’m trying to make this work with an alarm. My aim is to store the printed data in a variable and say if it is higher than a certain threshold, the alarm is digital written to high.

I’m currently trying this code from the Serial Input Basics:

[/int alarm = 13;
char receivedChar;
boolean newData = false;

void setup() {
	Serial.begin(38400);
	Serial.println("<Arduino is ready>");
        pinMode (alarm, OUTPUT);
}


void loop() {
	recvOneChar();
	showNewData();
}

void recvOneChar() {
	if (Serial.available() > 0) {
		receivedChar = Serial.read();
		newData = true;
	}
}

void showNewData() {
	if (newData == true) {
		Serial.print(receivedChar);
		newData = false;
	}

      if (receivedChar > 60) {
        digitalWrite (alarm, HIGH);
        delay (1000);
      }
}
]

I'm currently trying this code from the Serial Input Basics:

And? What is, or is not, happening?

What do you see in the Serial monitor ?