Go Down

Topic: Serial.print not printing... (Read 1 time) previous topic - next topic

liudr

Thanks James. I'm sure I don't need flush() at the moment. Hope OP won't either.

AWOL

Code: [Select]
if (loadcell = 0)
  {
    Serial.print("000");
    Serial.print(loadcell);
  }
  else if (loadcell < 10)
  {
    Serial.print("00");
    Serial.print(loadcell);
  }
  else if (loadcell < 100)
  {
    Serial.print("0");
    Serial.print(loadcell);
  }

One way or another, you're going to be printing "loadcell" whatever the vale, so why put it in every conditional?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Nick Gammon


... they defined flush() as trapping execution until serial out buffer becomes empty.


Bearing in mind that, while it does that, it doesn't wait for the last byte to be actually transmitted by the UART. So you can't flush() and then turn Serial off.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

liudr

nick, that intrigued me. I'm not good with the hardware registers so if someone can point to me how this flush() will not wait until the byte in the UART hardware is sent out, with some helpful comments on the code, that would be great!

Code: [Select]

void HardwareSerial::end()
{
  // wait for transmission of outgoing data
  while (_tx_buffer->head != _tx_buffer->tail)
    ;

  cbi(*_ucsrb, _rxen);
  cbi(*_ucsrb, _txen);
  cbi(*_ucsrb, _rxcie); 
  cbi(*_ucsrb, _udrie);
 
  // clear any received data
  _rx_buffer->head = _rx_buffer->tail;
}

void HardwareSerial::flush()
{
  while (_tx_buffer->head != _tx_buffer->tail)
    ;
}

size_t HardwareSerial::write(uint8_t c)
{
  int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;

  // If the output buffer is full, there's nothing for it other than to
  // wait for the interrupt handler to empty it a bit
  // ???: return 0 here instead?
  while (i == _tx_buffer->tail)
    ;

  _tx_buffer->buffer[_tx_buffer->head] = c;
  _tx_buffer->head = i;

  sbi(*_ucsrb, _udrie);
 
  return 1;
}


I gather that turning off serial means calling end()?

Nick Gammon

I stumbled across this when trying to do a 2-wire RS485 connection, where I had to switch from transmitting to receiving.

http://www.gammon.com.au/forum/?id=11428

I added these lines:

Code: [Select]

  while (!(UCSR0A & (1 << UDRE0)))  // Wait for empty transmit buffer
    UCSR0A |= 1 << TXC0;  // mark transmission not complete
  while (!(UCSR0A & (1 << TXC0)));   // Wait for the transmission to complete


All that flush() does is make sure that the output buffer is empty, but at (say) 9600 baud you still have to wait 1/960 of a second for that last byte to be transmitted.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Krupski


Better question, can I simply make a math statement that converts 56 into 0056?


Try this:

Code: [Select]

    int loadcell = 1234; // this has the value you want to print
    char buffer[20]; // buffer for 20 characters max
    const char *mask = "Loadcell: %04d"; // template for string "Loadcell: 0000"
    sprintf(buffer, mask, loadcell); // copy template and value to buffer
    Serial.print(buffer); // print buffer

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Go Up