baterai — Arduino Nano simulation

tes program

An interactive Arduino Nano circuit simulation you can run free in your browser on Velxio, by aripaintco.

Components

Sketch code

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1

Adafruit_SSD1306 display(
  SCREEN_WIDTH,
  SCREEN_HEIGHT,
  &Wire,
  OLED_RESET
);

#define BATTERY_PIN A0

// Pembagi tegangan
const float R1 = 100000.0;
const float R2 = 33000.0;

// Kalibrasi tegangan referensi Arduino
const float VREF = 5.00;

// Batas baterai lithium-ion 3S
const float BAT_FULL = 12.60;
const float BAT_EMPTY = 9.00;

const float LOW_WARNING = 10.80;
const float CRITICAL = 9.60;

// Tabel perkiraan tegangan istirahat 3S
const float voltageTable[] = {
  12.60, 12.30, 12.00, 11.76,
  11.55, 11.40, 11.25, 11.10,
  10.95, 10.80, 10.50, 10.20,
  9.90, 9.60, 9.00
};

const int percentTable[] = {
  100, 95, 90, 80,
  70, 60, 50, 40,
  30, 20, 15, 10,
  5, 2, 0
};

float readBatteryVoltage() {
  long total = 0;

  // Buang pembacaan pertama
  analogRead(BATTERY_PIN);

  for (int i = 0; i < 20; i++) {
    total += analogRead(BATTERY_PIN);
    delay(2);
  }

  float adc = total / 20.0;

  float pinVoltage = adc * VREF / 1023.0;

  float batteryVoltage =
    pinVoltage * (R1 + R2) / R2;

  return batteryVoltage;
}

int calculatePercent(float voltage) {

  if (voltage >= voltageTable[0])
    return 100;

  if (voltage <= voltageTable[14])
    return 0;

  for (int i = 0; i < 14; i++) {

    if (voltage <= voltageTable[i] &&
        voltage >= voltageTable[i + 1]) {

      float ratio =
        (voltage - voltageTable[i + 1]) /
        (voltageTable[i] - voltageTable[i + 1]);

      int result =
        percentTable[i + 1] +
        ratio * (percentTable[i] - percentTable[i + 1]);

      return constrain(result, 0, 100);
    }
  }

  return 0;
}

void drawBattery(int percent) {

  display.drawRoundRect(
    5, 31, 116, 17, 3, WHITE
  );

  display.fillRect(121, 36, 4, 7, WHITE);

  int width = map(percent, 0, 100, 0, 110);

  if (width > 0) {
    display.fillRoundRect(
      8, 34, width, 11, 2, WHITE
    );
  }
}

void setup() {

  Serial.begin(9600);

  if (!display.begin(
    SSD1306_SWITCHCAPVCC, 0x3C
  )) {
    while (true);
  }

  display.clearDisplay();
  display.setTextColor(WHITE);

  display.setTextSize(1);
  display.setCursor(15, 25);
  display.print("BATTERY MONITOR");
  display.display();

  delay(1200);
}

void loop() {

  float voltage = readBatteryVoltage();

  int percent = calculatePercent(voltage);

  display.clearDisplay();

  // HEADER
  display.setTextSize(1);
  display.setCursor(0, 0);
  display.print("BATTERY MONITOR");

  display.drawLine(0, 11, 128, 11, WHITE);

  // PERSENTASE
  display.setTextSize(2);
  display.setCursor(5, 15);

  display.print(percent);
  display.print("%");

  // TEGANGAN
  display.setTextSize(1);
  display.setCursor(76, 19);

  display.print(voltage, 2);
  display.print("V");

  // BAR BATERAI
  drawBattery(percent);

  // STATUS
  display.setTextSize(1);
  display.setCursor(5, 53);

  if (voltage <= CRITICAL) {
    display.print("BATTERY CRITI

More projects by aripaintco