Example 5: Power Detect

Code


/*In this simulation we will make the LCD screen show us the current to which the robot is connected,
either by itsown batteries or by the computer's current*/

/*We include the appropriate libraries to be able to activate certain commands and instructions*/

#include <Wire.h>
#include <Zumo32U4.h>

/*In the next line we name the variant of the robot that we are going to use*/

Zumo32U4OLED display;

/*In the void setup section it is not necessary to add anything since in the circuit we do not have to assign pins, outputs, inputs, etc*/

void setup()
{

}

/*In void loop we add all the steps that the robot will follow continuously when the simulation starts*/

void loop()
{

/*The following two lines list the actors to be evaluated, the power of the USB input and that of the batteries*/

  bool usbPower = usbPowerPresent();

  uint16_t batteryLevel = readBatteryMillivolts();

/*We clear the screen in case there are already texts*/

  display.clear();

/*We write the value of the battery with "B="*/

  display.print(F("B="));

/*We then put the value that is being read from the battery current*/
  
  display.print(batteryLevel);

/*We put the figures in which the current is measured, in this case milliwatts*/
  
  display.print(F("mV   "));

/*We go to the second line*/
  
  display.gotoXY(0, 1);

/*We write the value of the USB current with "USB="*/
  
  display.print(F("USB="));

/*We then put the value that is being read from the USB current*/
  
  display.print(usbPower ? 'Y' : 'N');

/*We apply a delay of 200 since otherwise the screen fails when representing the values*/

  delay(200);
}

Video of the Circuit