Garbage data in the serial Monitor !! Need Help

Hi All,

i have a question regarding the unwanted/garbage data that shown in the serial monitor while running the Serialevent example. .

Well, my project is about sending the soil temperature sensor to other Arduino UNO by using an Arduino UNO in the sender side with XBee(type ZigBee Series 2) connected using Arduino wireless SD Shield which has a place for the XBee & for the SD Card, the receiver side will have the same components Arduino UNO, XBee ,Arduino wireless SD Shield & SD Card. .

i want to transmit the data of the sensor to the receiver and saving them in the SD Card, but the Problem is when i receive the data , i receive them with extra Unwanted data in the beginning of each line of the results , as seen in the picture in the Attachment !

I think the problem is with the code of the Serialevent .

the code for Receiver is in the attachment !

Please Need Ur help as fast as possible !!

Reciever_sensor.ino (2.22 KB)

This line is wrong
serialEvent(); //call the function
YOU don't call serialEvent() - it happens automatically.

serialEvent() is a bit of a nonsense. I does exactly the same as this code

void loop() {
  // other code
  if (Serial.available() > 0) {
     mySerialReadFunction();
  }
}

Have a look at Serial Input Basics - simple reliable ways to receive data

...R

i did what you said, but it gave me the same results, with the sama garbage

i tried to recieve them in a n array like this:

void serialEvent() {

received_from_computer=Serial.readBytesUntil(40,computerdata,14);
computerdata[received_from_computer]= 0;

Serial.print(computerdata);
Serial.print(’\r’);
}

it removes some of the garbage, but still there is a small amount of garbage , also some times it does not print all the letters .
as in the picture .

Untitled.jpg

Post your code in code tags.
All of it.

AishaJ:
i did what you said, but it gave me the same results, with the sama garbage

You need to post the latest version of your program so we can keep up with you.

...R

Have you first tried using a wired connection between the two arduinos for trouble shooting? As others have said, if you really want anybody to look at your code, you need to post sending and receiving using the code box </>.

These are the two code for the sender side & the Receiver side . . i think the problem is with the receiver, but if you can’t find the problem see the sender code.

Sender_side.ino (1.72 KB)

Receiver_side.ino (1.46 KB)

These are the two code for the sender side & the Receiver side

#7 below:

http://forum.arduino.cc/index.php/topic,148850.0.html

First off, you could simplify this

void serialEvent() {
      
     received_from_computer=Serial.readBytesUntil('~',computerdata,14);    
           computerdata[received_from_computer]=0; 
           Serial.print(computerdata); 
           Serial.print('\r'); 
}
  
//------------------------------------------------------------------------

void loop() {
    if(Serial.available() > 0){      
  //    serialEvent();
}
}

to

void loop() {
    if(Serial.available() > 0){      
           received_from_computer=Serial.readBytesUntil('~',computerdata,14);    
           computerdata[received_from_computer]=0; 
           Serial.print(computerdata); 
           Serial.print('\r'); 
    }
}

but it would probably be better to use the 2nd example from Serial Input Basics. Unlike readBytesUntil() this code does not block the Arduino.

...R

SoftwareSerial XBee(0,1);

It makes NO sense to do software serial on the hardware serial pins. If you are going to sacrifice the hardware serial pins, use an instance of HardwareSerial (Serial).

  Serial.begin(9600);

You can NOT do hardware serial AND software serial on the same set of pins.

  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

Do you HAVE a Leonardo?

char Receive = Serial.read();

Before Serial.begin()? That was useless.

I'm going to assume that the receiver is a flawed as the sender.

Robin2 :

i tried the 2nd example, but it just print the first letter of the sting, i do not know why, so i changed to the previous code .

PaulS :

these lines i added them previously to try something else, so i just kept them here since they have No effect on the results if i removed them as i noticed . . so , when i remove them, the result still the same


Know i tried to remove some of the Garbage like the symbol ( ~ ) by just doing this step :

     received_from_computer=Serial.readBytesUntil('~',computerdata,14);    
           computerdata[received_from_computer]=0;

Since, i noticed that it removes whateve in first place of the Serial.readBytesUntil( , , ).
And it really removes the (~) sign & left with only one symbol of Garbage

While , the whole receiver code is :

#include <SPI.h>
#include <SD.h>

String inputString ;        
const int chipSelect = 4;

//------------------------
char computerdata[14]; 
byte received_from_computer=0; 
//-----------------------

void setup() {
  Serial.begin(9600);
  
 //---------------------------------------------------------------------- 
  Serial.print("Initializing SD card...");
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    return;                   }
    
  Serial.println("card initialized.");
}
//-----------------------------------------------------------------------

void serialEvent() {

     received_from_computer=Serial.readBytesUntil('~',computerdata,14);    
           computerdata[received_from_computer]=0;
           Serial.print(computerdata);  
           Serial.print('\r'); 
}
  
//------------------------------------------------------------------------

void loop() {
    if(Serial.available() > 0){      

}


//-------------------------------------------------------------

File dataFile = SD.open("datalog8.txt", FILE_WRITE);
  //delay(1000);
  
  if (dataFile) { 
    dataFile.println(computerdata);
    dataFile.println(" ");
    dataFile.close();
  }
  else {
    Serial.println("error opening datalog.txt");
  }
}
    if(Serial.available() > 0){     

}

If

you

are

not

going

to
do

anything,

what is the point of checking?

File dataFile = SD.open("datalog8.txt", FILE_WRITE);
  //delay(1000);
 
  if (dataFile) {

Log stuff to the SD card, even if nothing came in. I see.

No, actually, I don't.

What is sending the serial data?

AishaJ:
Robin2 :

i tried the 2nd example, but it just print the first letter of the sting, i do not know why, so i changed to the previous code .

If you post the code that caused the problem I may be able to help. Without it, I can't.

...R