I am working on a Heart Rate Monitoring project, using this Sensor from sparkFun, plugged to Arduino pro mini and reading proper BPM, I have an LED connected to pin 13 of my Arduino that blinks when the pulse happen. The code given on SparkFun page works on Timer2
I have two more LEDs connected to pin12 and pin11, which indicates battery percentage - because i am powering my Arduino using a 2 cell lithium ion battery with Voltage regulator.. I also have a voltage divider circuit at the voltage regulator with 1K resistors and connected to analog pin A0 for monitoring battery voltage.
I have assigned LED connected on pin 13 as LED1, on 12 as LED2 and 11 as LED3
LED1 for low charge LED2 for half charge LED3 for full charge
Here is the problem :
When i connect charger ,Arduino reads data from voltage divider and outputs levels to LEDS
Here - LED1 is conflicting between the pulse sensor (which is working on Timer2) and also for indicating the low level charging, and LED1 is flickering.
how to overcome with this issue.? disabling Timer2 helps.? if'Yes' then how to do that.?
Note : I don't want to sample Heart Rate Pulse when charger is connected!
Thank you very much.
Best
void charging() {
TCCR2A = 0;
usbState = digitalRead(usb_detect);
while (usbState == HIGH) {
usbState = digitalRead(usb_detect);
// read the input on analog pin 0:
int sensorValue = analogRead(A1);
// Convert the analog reading (which goes from 0 - 512) to a voltage (0
- 3.3V):
float voltage = sensorValue * (3.3 / 512.0);
// print out the value you read:
if (voltage >= 2.5 && voltage <= 3.3)
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(gps_led, ledState);
digitalWrite(blinkPin, LOW);
digitalWrite(save_led, LOW);
}
}
if (voltage >= 3.4 && voltage <= 3.7)
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(blinkPin, ledState);
digitalWrite(gps_led, HIGH);
digitalWrite(save_led, LOW);
}
}
if (voltage >= 3.8 && voltage <= 4.1)
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(save_led, ledState);
digitalWrite(blinkPin, HIGH);
digitalWrite(gps_led, HIGH);
}
}
Serial.println(voltage);
delay(1); // delay in between reads for stability
}
}