-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
120 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include "battery_monitor.h" | ||
|
||
static bool buzzer_on = false; | ||
|
||
float battery_monitor_pin_voltage = 0.0; | ||
float battery_monitor_battery_voltage = 0.0; | ||
|
||
void battery_monitor_init() { | ||
pinMode(BATTERY_MONITOR_PIN, INPUT); | ||
|
||
analogReference(INTERNAL); | ||
analogReadResolution(12); | ||
analogReadAveraging(32); | ||
} | ||
|
||
void battery_monitor_update() { | ||
uint16_t pin_value = analogRead(BATTERY_MONITOR_PIN); | ||
|
||
battery_monitor_pin_voltage = pin_value / BATTERY_MONITOR_PIN_DIVISOR * BATTERY_MONITOR_LOSS_FACTOR; | ||
battery_monitor_battery_voltage = battery_monitor_pin_voltage / (BATTERY_R2_VAL / (BATTERY_R1_VAL + BATTERY_R2_VAL)); | ||
|
||
if (battery_monitor_battery_voltage < 9.0) { // no battery connected | ||
noTone(BATTERY_ALERT_BUZZER_PIN); | ||
buzzer_on = false; | ||
|
||
} else if (battery_monitor_battery_voltage < 10.85) { // low voltage | ||
if (buzzer_on) { | ||
noTone(BATTERY_ALERT_BUZZER_PIN); | ||
buzzer_on = false; | ||
} else { | ||
tone(BATTERY_ALERT_BUZZER_PIN, 400); | ||
buzzer_on = true; | ||
} | ||
|
||
} else { // voltage good | ||
noTone(BATTERY_ALERT_BUZZER_PIN); | ||
buzzer_on = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma once | ||
|
||
#include "Arduino.h" | ||
|
||
#define BATTERY_MONITOR_PIN A11 | ||
#define BATTERY_ALERT_BUZZER_PIN 25 | ||
|
||
#define BATTERY_R1_VAL 100000.0f | ||
#define BATTERY_R2_VAL 10000.0f | ||
|
||
#define BATTERY_MONITOR_READ_RESOLUTION 12 | ||
#define BATTERY_MONITOR_PIN_DIVISOR ((float) pow(2, BATTERY_MONITOR_READ_RESOLUTION)) | ||
#define BATTERY_MONITOR_LOSS_FACTOR 1.198f | ||
|
||
void battery_monitor_init(); | ||
void battery_monitor_update(); | ||
|
||
extern float battery_monitor_pin_voltage; | ||
extern float battery_monitor_battery_voltage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
#include <i2c_t3.h> | ||
#include "i2c_helpers.h" | ||
|
||
void i2c1_begin() { | ||
Wire1.begin(I2C_MASTER, 0x00, I2C_PINS_29_30, I2C_PULLUP_EXT, I2C_RATE_800); | ||
void i2c_begin() { | ||
Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_INT, I2C_RATE_800); | ||
} | ||
|
||
void i2c1_update_register(uint16_t device_address, uint8_t data_address, uint8_t data_value) { | ||
Wire1.beginTransmission(device_address); | ||
Wire1.write(data_address); | ||
Wire1.write(data_value); | ||
Wire1.endTransmission(); | ||
void i2c_update_register(uint16_t device_address, uint8_t data_address, uint8_t data_value) { | ||
Wire.beginTransmission(device_address); | ||
Wire.write(data_address); | ||
Wire.write(data_value); | ||
Wire.endTransmission(); | ||
} | ||
|
||
void i2c1_send_byte(uint16_t device_address, uint8_t data_value) { | ||
Wire1.beginTransmission(device_address); | ||
Wire1.write(data_value); | ||
Wire1.endTransmission(); | ||
void i2c_send_byte(uint16_t device_address, uint8_t data_value) { | ||
Wire.beginTransmission(device_address); | ||
Wire.write(data_value); | ||
Wire.endTransmission(); | ||
} | ||
|
||
uint8_t i2c1_read_byte(uint16_t device_address) { | ||
Wire1.requestFrom(device_address, 1); | ||
return Wire1.read(); | ||
uint8_t i2c_read_byte(uint16_t device_address) { | ||
Wire.requestFrom(device_address, 1); | ||
return Wire.read(); | ||
} | ||
|
||
uint16_t i2c1_read_word(uint16_t device_address) { | ||
Wire1.requestFrom(device_address, 2); | ||
return (Wire1.read() << 8) | Wire1.read(); | ||
uint16_t i2c_read_word(uint16_t device_address) { | ||
Wire.requestFrom(device_address, 2); | ||
return (Wire.read() << 8) | Wire.read(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters