Skip to content

Instantly share code, notes, and snippets.

@andrewhummus
Last active April 27, 2024 16:07
Show Gist options
  • Save andrewhummus/a8df5a782132ca5978b0e57620e29345 to your computer and use it in GitHub Desktop.
Save andrewhummus/a8df5a782132ca5978b0e57620e29345 to your computer and use it in GitHub Desktop.
const int dischargePin = 10; // Digital output pin for controlling transistor
const int voltagePin = A0; // Analog input pin for measuring battery voltage
float dischargeCurrent = 0.80; // Specify the discharge current in amperes
int pulseDuration = 900000; // Specify the pulse duration in milliseconds
bool dischargeComplete = false; // Flag variable to track pulse discharge completion
void setup() {
pinMode(dischargePin, OUTPUT);
pinMode(voltagePin, INPUT);
Serial.begin(9600); // Initialize serial communication
Serial.print("start");
}
void loop() {
unsigned long startTime = millis(); // Record start time
int k = 1;
dischargeComplete = false;
// Run the discharge for the specified pulse duration
while (millis() - startTime < pulseDuration && !dischargeComplete) {
analogWrite(dischargePin, 255); // Set maximum PWM duty cycle to deliver maximum current
int batteryVoltage = analogRead(voltagePin);
float voltage = batteryVoltage * (5.0 / 1023.0); // Convert ADC value to voltage (assuming 5V Arduino)
// Print voltage to Serial Monitor
Serial.print(k);
Serial.print(", ");
Serial.print(voltage, 5);
Serial.print("\n");
k++;
delay(1000); // Delay between voltage measurements
}
// Turn off the discharge after the specified pulse duration
analogWrite(dischargePin, 0);
dischargeComplete = true; // Set discharge complete flag
// Measure battery voltage continuously until discharge is complete
while (dischargeComplete) {
// Measure battery voltage
int batteryVoltage = analogRead(voltagePin);
float voltage = batteryVoltage * (5.0 / 1023.0); // Convert ADC value to voltage (assuming 5V Arduino)
// Print voltage to Serial Monitor
Serial.print(k);
Serial.print(", ");
Serial.print(voltage, 5);
Serial.print("\n");
k ++;
delay(1000); // Delay between voltage measurements
}
}
@andrewhummus
Copy link
Author

should ensure the discharge process runs for the specific pulse or until iot's manually stopped

fixed the while loops so i make sure to test it cuz i cant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment