首页 文章

调试在Arduino MKR1000上运行的Arduino Uno代码

提问于
浏览
0

所以,我是Arduino的业余程序员,之前从未使用过Arduino MKR1000 . 我使用了Arduino Uno并使用Grove Ear夹心跳传感器和Grove温度传感器编写附加代码来检测心跳和温度,然后每隔20秒在控制台中打印它们 . 以前,这段代码是为了在Grove OLED屏幕上显示,但后来通过仅在控制台上阅读将其简化为使用它 .

由于我项目的可穿戴性,我不得不改用MKR1000 . 我知道MKR1000使用相同的Arduino代码,并且应该像我在Arduino Uno上一样工作,但是我在使用MKR1000和相同代码时遇到了一些问题 .

问题是代码只运行一次并在此之后停止 . 虽然我知道for循环以及它在某种程度上是如何工作的,但我找不到确切的问题,为什么它停止循环而不是不断地获取数据并在控制台上发布它,就像之前使用我的Uno一样 .

只是为了抬头,以下是我的代码对Arduino Uno的反应:

控制台中的结果显示:

Please be ready
  This will now begin

然后它每秒打印1到20,然后是传感器读数 . 发布后,它再次重复此过程 .

再次抱歉给您带来不便,感谢您的帮助 .

我使用了传感器文档博客中的直接代码(链接页面的页面底部):

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 回答

  • 0

    你的问题是中断引脚 . 在Arduino UNO上,数字引脚D2是中断引脚0,正如您在代码中所做的那样 . 在Arduino MKR1000上,中断引脚与物理引脚编号相同,因此如果连接到引脚2,请将 attachInterrupt(0, interrupt, RISING); 更改为 attachInterrupt(2, interrupt, RISING);

    如果你想确保使用正确的引脚,你可以使用 digitalPinToInterrupt(pin) 在任何Arduino板上获得正确的中断号 . 像这样 attachInterrupt(digitalPinToInterrupt(pin), interrupt, RISING); 其中pin是物理引脚号 .

    信息

    在Arduino MKR1000上,你可以使用引脚0,1,4,5,6,7,8,9,A1和A2作为中断

相关问题