Arduino Serial Connection

Hi,

I am new to arduino to arduino connections and have been reading on serial and I2C protocols. I am trying to obtain a sensor signal (say a photoresistor) from one arduino, and transmit this signal to another arduino so it can be displayed in the serial monitor. The problem im having is that the signal appears on the serial monitor as weird characters. I understand that you cannot simply send the analogRead(A0) value using serial.Write as it is not a character. But when i try to convert it into a character, it doesn't work and just displays weird characters. Could someone explain the procedure i should take to get the photoresistor signal from one arduino and display it on the serial monitor of the other arduino using either I2C or Serial Transmission. Thank you very much

My sender code looks like this:

char str[4];

void setup() {
Serial.begin(9600);
}

void loop() {
int value=analogRead(A0);
itoa(value, str, 10);
Serial.write(str, 4);
Serial.println("");
delay (500);
}

im not sure what to do with the receiving code though...

I tried developing this receiver code which initially outputs data for like the first two values and then just outputs zero:

//Receiver Code

char str[4];

void setup() {
Serial.begin(9600);

}

void loop() {
int i;

Serial.readBytes(str,4);

i= atoi(str);
Serial.println(i);
delay(500);
}

I also tried printing the character directly but this resulted in weird characters being displayed in the serial monitor:

//Receiver Code

char str[4];

void setup() {
Serial.begin(9600);

}

void loop() {
int i;

Serial.readBytes(str,4);

Serial.println(str);
delay(500);
}

From Reply #1, this

void loop() {
  int value=analogRead(A0);
  itoa(value, str, 10);
  Serial.write(str, 4);
  Serial.println("");
  delay (500);
}

should be the much simpler

void loop() {
  int value=analogRead(A0);
  Serial.println(value);
  delay (500);
}

If you do that, does it help deal with your questions in Reply #2.

By the way it is much easier to help if you wait to get one question answered before opening up some other questions.

And if you just want to send data from one Arduino and receive it on another have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

The technique in the 3rd example will be the most reliable.

You can send data in a compatible format with code like this

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

…R

As per your suggestion I have turned my code into:

Sender code:

void setup() {
Serial.begin(9600);
}

void loop() {
int value=analogRead(A0);
Serial.print(’<’); // start marker
Serial.println(value);
Serial.print(’,’); // comma separator
Serial.println(’>’); // end marker
delay (500);
}

and receiver code

const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;

void setup() {
Serial.begin(9600);
Serial.println("");
}

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

void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = ‘<’;
char endMarker = ‘>’;
char rc;

while (Serial.available() > 0 && newData == false) {
rc = Serial.read();

if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = ‘\0’; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}

else if (rc == startMarker) {
recvInProgress = true;
}
}
}

void showNewData() {
if (newData == true) {
Serial.print("This just in … ");
Serial.println(receivedChars);
newData = false;
}
}

This seems to work, thank you very much! I just have a question concerning the code in example 3, since i am not very good at programming:
the function recvwithStartEndMarkers() is quite complex, would you mind breaking it down a bit so I can understand it? I am really struggling to understand it…

I’ll add some more comments to the function which might help clarify things.

void recvWithStartEndMarkers() {
//
//  This functions reads from the Serial port, looking for a string of characters enclosed in '<' and '>'
//  markers (up to numChars-1 long), and saves it to the global char array receivedChars.
//
    static boolean recvInProgress = false;    // flag to tell us if we are in the string we want
    static byte ndx = 0;                             // index into the destination array
    char startMarker = '<';
    char endMarker = '>';
    char rc;                                             // latest received character
 
    while (Serial.available() > 0 && newData == false) {    // if we're starting a new string
        rc = Serial.read();                                               // read the next character

        if (recvInProgress == true) {                                // if we're already in a marked sequence
            if (rc != endMarker) {                                      // but not got to the end of it yet
                receivedChars[ndx] = rc;                             // store the character 
                ndx++;                                                     // increment the index ready for the next 
                if (ndx >= numChars) {                               // if the output array is full
                    ndx = numChars - 1;                               // fudge it
                }                                                              // (this is not very good code, but I haven't
            }                                                                  // time to rewrite it)
            else {                                                            // we're done for this time
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {   // we weren't already in a marked string, so is the start of one?
            recvInProgress = true;       // yes, so flag it so that we know
        }
    }
}

I hope this helps. Using the Serial.readBytesUntil(character, buffer, length) function might have made the code a bit simpler.

Thanks @alexmonro. I might have written it a bit differently myself here and there.

The purpose of the two lines

if (ndx >= numChars) {                               // if the output array is full
         ndx = numChars - 1;                               // fudge it
}

is not a fudge. They are there simply to prevent writing past the end of the array. If the array size is correctly chosen those lines should never come into play.

@yowusgudfam, a high-level description of the code is something like this

  • check what has been received
  • if we don't see a start-marker just throw stuff away
  • when we see a start-marked start saving the data (recvInProgress will be true)
  • keep saving data until we see the end-marker
  • when we see the end-marker set the variable newData = true so that the main program knows that the message is complete.

...R

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks… Tom… :slight_smile:

Thanks very much for everyone's help. Really helped me grasp a few concepts :slight_smile: