Weird results with Serial and functions

Hi. I have been making a program for reading a string in the serial port, and I got some very rare results.
When I use the Serial in this program, the variable incomingByte was modified propperly when I get some data in the serial port buffer, but when I put the process in a function, and try to change the global variable incomingByte, using pointers or the variable, the value in the variable doesn’t change. Its very weird.
I was using an Arduino Nano V3 and Arduino 1.5.7 IDE (I tried too with 1.0.5 with the same results).
I put the example code and the modified code.
Thanks for the help in advance!!

This is the example, works like a charm…

int incomingByte = 0;   // for incoming serial data

void setup() {
        Serial.begin(115200);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

        // send data only when you receive data:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte, DEC);
        }
}

But this one doesn’t vary the incomingByte value!

int incomingByte = 0;

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

void loop() {
  recibir_byte(&incomingByte);

  Serial.print("I received: ");
  Serial.println(incomingByte,DEC);
}


void recibir_byte(int * incomingByte_) {

  if (Serial.available() > 0) {

    *incomingByte_ = Serial.read();

  }

}

I just compiled and ran your 2nd listing and it works fine for me.

However,

int incomingByte = 0;  //<-- do you really want to use an int for a variable named BYTE ?

The first sketch prints what is received (also the LineFeed and CarriageReturn).
The second sketch keeps printing, no matter if something is received or not.

byte incomingByte;

if( recibir_byte( &incomingByte))
{
  Serial.println(incomingByte, DEC);
}

boolean recibir_byte( byte * pData)
{
  if (Serial.available() > 0)
  {
    *pData = Wire.read();
    return true;
  }
  else
  {
    return false;
  }
}

Why do you feel the need to pass a global variable to a function? If there is a reason for the variable to be global, you don't need to pass it to functions. If the variable is to be local to a function, but shared by another function, it shouldn't be global.

Okay, I try it again, and the results are the same. I try the mod as PaulS said, and here is again. I made a Python program who sends a byte to the serial port and read the answer. The output for this program:

int incomingByte = 0;

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

void loop() {
  recibir_byte();

//  Serial.print("I received: ");
//  Serial.println(incomingByte,DEC);
}


void recibir_byte(void) {

  if (Serial.available() > 0) {
  
    incomingByte = Serial.read();
    
    Serial.print("I received: ");
    Serial.println(incomingByte,DEC);

  }

}

The output for the script for that program was the correct : 1,2,3,4,5,6,7. It echo the received data. The output for this program:

int incomingByte = 0;

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

void loop() {
  recibir_byte();

  Serial.print("I received: ");
  Serial.println(incomingByte,DEC);
}


void recibir_byte(void) {

  if (Serial.available() > 0) {
  
    incomingByte = Serial.read();
    
//    Serial.print("I received: ");
//    Serial.println(incomingByte,DEC);

  }

}

The output is : 0,0,0,0. It doesn’t echo the data. Do you see any error in that code? Thanks, this is driving me crazy! :angry:

It's because you do this

  Serial.print("I received: ");
  Serial.println(incomingByte,DEC);

for every call of recibir_byte() regardless of what happens in recibir_byte!

Mark

So, I've changed the program and put this in the Arduino:

int incomingByte = 0;

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

void loop() {
  recibir_byte();

  Serial.print("I received: ");
  Serial.println(incomingByte,DEC);
}


void recibir_byte(void) {

  if (Serial.available() > 0) {
  
    incomingByte = Serial.read();
    
    Serial.print("Function: ");
    Serial.println(incomingByte,DEC);

  }

}

And that should print two lines with the result, right? But never enter in the function if! the output is: " I received: 0" ; " I received: 0" ;" I received: 0" ;" I received: 0" .
Why is that?? Is the same input!

Thanks for the help!

Because the arduino has sent umpteen " I received: 0" before you can type any thing!

Mark

Are you expecting recibir_byte() to stop and wait until a byte is received? This code will do that for you...

void recibir_byte(void) {
  while(!Serial.available());
  
  incomingByte = Serial.read();
  Serial.print("Function: ");
  Serial.println(incomingByte,DEC);
}

This will wait until it gets something. The while loop is "empty" meaning it doesn't do anything while its waiting. This is usually not a good thing because you might want the Arduino to be able to do other tasks while its waiting.