0
votes

So, I am an amateur programmer at Arduino, and have never used Arduino MKR1000 before. I used an Arduino Uno and wrote the attached code that detects heart beat and temperature using the Grove Ear clip heart beat sensor and the Grove Temperature sensor and then prints them in the console every 20 seconds. Previously, this code was written to show in an Grove OLED screen but later simplified it back to using it by reading on just the console.

Because of the wearable nature of my project I have to switch to using a MKR1000 instead. I know the MKR1000 uses the same Arduino code and should work the same way as it did on my Arduino Uno, but I have been having some issues with using the MKR1000 with the same code.

The issue is that the code only runs once and stops after that. While I am aware of the for loops and how it works to certain extent, I cannot find the exact issue why it stops looping instead of taking the data constantly and publishing it on the console, like it did previously with my Uno.

Just for the heads up, following is how my code reacted to Arduino Uno:

The result in the console shows:

  Please be ready
  This will now begin

then it prints number 1 to 20 every second followed by sensor readings. After publishing this, it repeats this process again.

Again sorry for the inconvenience and thank you for your help.

I used direct codes from the documentation blog of the sensors (bottom of the page of the linked page):

Grove heart bear sensor

Grove Temperature sensor

#define LED 4//indicator, Grove - LED is connected with D4 of Arduino
boolean led_state = LOW;//state of LED, each time an external interrupt
                                //will change the state of LED
float tempa;
int tempPin = 0;
unsigned char counter;
unsigned long temp[21];
unsigned long sub;
bool data_effect=true;
unsigned int heart_rate;//the measurement result of heart rate

const int max_heartpluse_duty = 2000;//you can change it follow your system's request.
                        //2000 meams 2 seconds. System return error
                        //if the duty overtrip 2 second.
void setup()
{

    pinMode(LED, OUTPUT);
    Serial.begin(9600);
 while (!Serial){
  ;
  }
    Serial.println("Please be ready");
    delay(5000);
    arrayInit();
    Serial.println("This will now begin.");
    attachInterrupt(0, interrupt, RISING);//set interrupt 0,digital port 2
}
void loop()
{
    digitalWrite(LED, led_state);//Update the state of the indicator
}
/*Function: calculate the heart rate*/


void sum()
{
 if(data_effect)
    {
        heart_rate=1200000/(temp[20]-temp[0]);//60*20*1000/20_total_time
        Serial.print("Heart_rate_is:\t");
        Serial.println(heart_rate);
        tempa = analogRead(tempPin);
        tempa = tempa * 0.11;
         Serial.print("Body Temperature = ");
         Serial.print(tempa);
         Serial.print("*C");
         Serial.println();
         delay(1000);
    }
   data_effect=1;//sign bit
}
/*Function: Interrupt service routine.Get the sigal from the external interrupt*/
void interrupt()
{
    temp[counter]=millis();
    Serial.println(counter,DEC);
    switch(counter)
    {
        case 0:
            sub=temp[counter]-temp[20];

            break;
        default:
            sub=temp[counter]-temp[counter-1];

            break;
    }
    if(sub>max_heartpluse_duty)//set 2 seconds as max heart pluse duty
    {
        data_effect=0;//sign bit
        counter=0;
        Serial.println("measurement error,test will restart!" );
        arrayInit();
    }
    else if (counter==20&&data_effect)
    {
      counter=0;
      sum();
    }
    else if(counter!=20&&data_effect)
    {
    counter++;
    }
    else
    {
        counter=0;
        data_effect=1;
    }

}
/*Function: Initialization for the array(temp)*/
void arrayInit()
{
    for(unsigned char i=0;i < 20;i ++)
    {
        temp[i]=0;
    }
    temp[20]=millis();
}
1

1 Answers

0
votes

Your problem is the interrupt pin. On the Arduino UNO the digital pin D2 is interrupt pin 0, as you have done in your code. On the Arduino MKR1000 the interrupt pin is tha same as the physical pin number, so if you'r connecting to pin 2, change attachInterrupt(0, interrupt, RISING); to attachInterrupt(2, interrupt, RISING);

If you wanna be sure to use the right pin you can use digitalPinToInterrupt(pin) to get the correct interrupt number on any Arduino board. Like this attachInterrupt(digitalPinToInterrupt(pin), interrupt, RISING); where pin is the physical pin number.

Info

On the Arduino MKR1000 you can use pin 0, 1, 4, 5, 6, 7, 8, 9, A1 and A2 as interrupt