// https://wokwi.com/projects/412029350708941825
int ChargePin = 8;
int inputPinC = A0;
unsigned long previousMeasureMillis = 0;
unsigned long previousMillis = 0;
const long chargeInterval = 10000; // 10s charge/discharge time
const long measureInterval = 40;
int chargeState = LOW;
float Vcapacitor = 0.0;
int burst = 50; //
void setup() {
pinMode(ChargePin, OUTPUT);
Serial.begin(115200);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= chargeInterval) {
previousMillis += chargeInterval;
burst = 50; // reset
if (chargeState == LOW) {
chargeState = HIGH;
} else {
chargeState = LOW;
}
digitalWrite(ChargePin, chargeState);
}
if (currentMillis - previousMeasureMillis >= measureInterval) {
previousMeasureMillis += measureInterval;
if (burst) { // print samples only during burst
// Read analog value and convert to voltage
Vcapacitor = (analogRead(inputPinC) / 1023.0) * 5.0; // Convert raw value to voltage
// Print the capacitor voltage in volts
Serial.print("DATA: ");
Serial.print(Vcapacitor, 3); // Print capacitor voltage with 2 decimal places
Serial.println();
--burst;
}
}
}