I want to read the current from pin A0 at an interval of 10 seconds and the voltage from pin A1 at an interval of 10 mins. This is my code but I'm not able to get the readings at the interval I want.
// Initialize Sensor Pins
const int voltageSensorPin = A1;
const int currentSensorPin = A0;
const int currentSensorInterval = 10000;
const int voltageSensorInterval = 600000;
unsigned long lastcurrentSensor = 0;
unsigned long lastvoltageSensor = 0;
int i = 0;
unsigned long millis();
// Each 18650 Cell has a capacity of 3Ah, which equals 10,800 C. Since 2 cells are in series, capacity is 10,800 C
float totalCoulombs = 18360.0;
float remainingCoulombs = totalCoulombs;
// Power Resistor Value
// The measured value is 1.036 +/- 0.011 Ohms
float Rp = 0.9;
// Determine factor for voltage divider to measure cell OCV
// The measured value of R1 is 980 +/- 1 Ohm
float R1 = 1005;
// The measured value of R2 is 977 +/- 1 Ohm
float R2 = 974;
// Using a voltage divider equation, the total voltage is the voltage drop measured on R1 * voltageFactor
float voltageFactor = (R1 + R2) / R1;
// FUNCTIONS
// _______________
// A function used to measure the OCV of the cells
float takeCCVMeasurement() {
//digitalWrite(relayPin, LOW);
//int count = 0;
//delay(1000); // lets the batteries settle to equilibrium
//float periodVoltageSum = 0;
//while (count < 10) {
int rawVoltRead = analogRead(voltageSensorPin);
float voltage = (rawVoltRead / 1024.0) * 5.0* voltageFactor;
//periodVoltageSum += voltage;
//count++;
//}
//float voltageAve = periodVoltageSum/ count;
//digitalWrite(relayPin, LOW);
return voltage;
}
// A function to print out an OCV measurement to serial
void printOCVMeasurement(int code, float OCV) {
Serial.print(code);
Serial.print(',');
Serial.println(OCV, 3);
}
// A function used to measure the current through R-power
float takeCurrentMeasurement() {
int current = 0;
// The measurement start time is recorded
//unsigned long startTime = millis();
// 10000 voltage measurements are taken
//int count = 0;
//float periodAmpSum1 = 0;
//while (count < 10000) {
// Analog readings are mapped to a 5V scale
int rawVoltRead1 = analogRead(currentSensorPin);
float voltage1 = (rawVoltRead1 / 1024.0000) * 5.0;
// Using Ohm's Law, I = V/R
current = voltage1 / Rp;
//count++;
//}
//float ampAve = periodAmpSum1 / count;
return current;
}
void printCurrentMeasurement(int code, float current, float deltaT) {
Serial.print(code);
Serial.print(',');
Serial.print(current, 4);
Serial.print(',');
Serial.println(deltaT);
}
void setup() {
Serial.begin(9600);
pinMode(voltageSensorPin,INPUT);
pinMode(currentSensorPin,INPUT);
// Set digital pin to OUTPUT
//pinMode(relayPin, OUTPUT);
}
void loop() {
/*
- OVERVIEW OF LOOP
-
-
- The Arduino waits for Python to communicate via Serial that it needs a measurement
-
- The Arduino then either collects a current or OCV measurement and prints the data to Serial
- for Python to read and use in the Kalman Filter program
*/
//to collect data of current and voltage every 10 seconds and 10 mins
if (Serial.available() > 0) {
// for initial value of current and voltage
if (i<1){
float initialOCV = takeCCVMeasurement();
float initialCurrent = takeCurrentMeasurement();
Serial.print(initialCurrent, 4);
Serial.print(',');
Serial.println(initialOCV, 3);
i++;
}
//current measurement
if (millis() - lastcurrentSensor >= currentSensorInterval){
float initialCurrent = takeCurrentMeasurement();
Serial.println(initialCurrent, 4);
lastcurrentSensor = millis();
}
//voltage measurement
if (millis() - lastvoltageSensor >= voltageSensorInterval){
float initialOCV = takeCCVMeasurement();
//float initialCurrent = takeCurrentMeasurement();
//Serial.print(initialCurrent, 4);
//Serial.print(",");
Serial.println(initialOCV, 3);
lastvoltageSensor = millis();
}
}
}
///code = Serial.read();
// current code = 1
//if (code=='1') {
float initialCurrent = takeCurrentMeasurement();
Serial.println(initialCurrent, 4);
//
// OCV code = 2
//else if (code=='2') {
float initialOCV = takeOCVMeasurement();
Serial.println(initialOCV, 3);
}
}/