Pages: 1 [2]   Go Down
Author Topic: Serial.print not printing...  (Read 746 times)
0 Members and 1 Guest are viewing this topic.
Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25802
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?
Logged

"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.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

... 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.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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()?
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  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.
Logged

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 792
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Try this:

Code:
    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
Logged

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Pages: 1 [2]   Go Up
Jump to: