0

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

}
}
  • What code are you running? The "Getting Advanced Code"? Or something different? – Majenko Aug 19 '17 at 13:27
  • I suggest you just change what pins you use for your LEDs. One function per LED. Trying to use one LED for two functions is a bit daft really. What do you want the LED to be - low charge, or pulse? Make your mind up. – Majenko Aug 19 '17 at 13:28
  • Yes, I am running "Getting Advance Code" from SparkFun page.. I cannot change or add more pins to my system, it's sealed in an enclosure.. – Mudassir Hussain Aug 19 '17 at 13:37
1

Timer 2 is fundamental to that code. It is used to sample the heart rate sensor at a regular period. If you disable that timer you will never sample the sensor.

You cannot have two functions on one LED - it makes no sense. You should decide on one and only one function to use that LED for. If you want to use it for the battery status then remove the code from the timer interrupt that controls the LED. If you want to use it to display the hear beat then you will have to make do with just two LEDs for your battery status.

If the two operations are mutually exclusive then yes you can disable the timer. Just turn it off, then turn it on again when you want sampling to start again.

Off:

TCCR2B = 0;

On (assuming you are using this code):

TCCR2B = 0x06;
  • I can reset my Arduino using Software Reset when i disconnect the charger. Which means i can disable timer when i connect charger and re-enable it when the arduino reset..? Will this work.? – Mudassir Hussain Aug 19 '17 at 13:51
  • By the way! i don't want to sample Heart Rate pulse when i connect to charger. – Mudassir Hussain Aug 19 '17 at 13:53
  • See my edit.... – Majenko Aug 19 '17 at 14:29
  • Rather than clearing TCCR2A (the CTC mode), I would clear either TCCR2B (to stop the timer) or TIMSK2 (which leaves the timer running but disable the interrupt). – Edgar Bonet Aug 19 '17 at 15:05
  • I have added TCCR2A = 0; in my code at charging(); ,Please see my edited question – Mudassir Hussain Aug 19 '17 at 15:09
0

you provide lots of information that has nothing to do with your question, and at the same time omitted critical information that can help other help you.

how to overcome with this issue.?

the simplest would be to move the pins so they have their dedicated pins.

disabling Timer2 helps.? if'Yes' then how to do that.?

if you want to reuse the pins -> multiple functions on the same pin, you may have to code as such. For example, you disconnect the led briefly, read the pulse sensor, and then restore the led. if you do that sufficiently quick, you wouldn't notice that the led had been disconnected.

the feasibility of such approaches will depend on the sensors connected to that pin and your ability to code.

0

in the linked repo in the product page you linked there is a explanation page how to use the device without using timers buried at the bottom of a file:

https://github.com/WorldFamousElectronics/PulseSensor_Amped_Arduino/blob/master/PulseSensorAmped_Arduino_1.5.0/Timer_Interrupt_Notes.ino#L217

In essence instead of using the timer interrupt you put the sampling code in the loop() body triggered by a software timer and make sure that there are no delay() or busy loops happening in it.

  • I have used delay(); function in void loop – Mudassir Hussain Aug 19 '17 at 15:54
  • it's not that hard to rewrite the loop to avoid delay() though. – ratchet freak Aug 19 '17 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.