4

The closest thing I can find is if(Serial) but that only returns true if there's a serial port open.

The function I want, ideally, should:

Return true if plugged into a computer

Return false if plugged into a USB power bank

Return false if not plugged in at all(Running off of its own lithium cell)

Stephen
  • 43
  • 3

1 Answers1

5

ADDEN of UDADDR is set if the '32U4 has been assigned a USB address by the host and the firmware wants to enable USB connectivity.

UDADDR & _BV(ADDEN)
Ignacio Vazquez-Abrams
  • 17,733
  • 1
  • 28
  • 32