极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 12381|回复: 4

求助,关于配合中断子程序的问题。加了if else 函数以后出现没有声明

[复制链接]
发表于 2016-4-4 11:54:27 | 显示全部楼层 |阅读模式
本帖最后由 alpacapie 于 2016-4-18 21:24 编辑

【下边有正确代码】在下是个初学者,是这样的,我买了一个心率传感器,店家给的文件里有两个代码,打开其中一个会同时打开这两个【鹦鹉学舌一下,另一个文件叫中断子程序,不懂啊】如图


原本的代码是这样的
  1. //  VARIABLES
  2. int pulsePin = 0;                 // Pulse Sensor purple wire connected to analog pin 0
  3. int blinkPin = 13;                // pin to blink led at each beat
  4. int fadePin = 5;                  // pin to do fancy classy fading blink at each beat
  5. int fadeRate = 0;                 // used to fade LED on with PWM on fadePin


  6. // these variables are volatile because they are used during the interrupt service routine!
  7. volatile int BPM;                   // used to hold the pulse rate
  8. volatile int Signal;                // holds the incoming raw data
  9. volatile int IBI = 600;             // holds the time between beats, must be seeded!
  10. volatile boolean Pulse = false;     // true when pulse wave is high, false when it's low
  11. volatile boolean QS = false;        // becomes true when Arduoino finds a beat.


  12. void setup(){
  13.   pinMode(blinkPin,OUTPUT);         // pin that will blink to your heartbeat!
  14.   pinMode(fadePin,OUTPUT);          // pin that will fade to your heartbeat!
  15.   Serial.begin(115200);             // we agree to talk fast!
  16.   interruptSetup();                 // sets up to read Pulse Sensor signal every 2mS
  17.    // UN-COMMENT THE NEXT LINE IF YOU ARE POWERING The Pulse Sensor AT LOW VOLTAGE,
  18.    // AND APPLY THAT VOLTAGE TO THE A-REF PIN
  19.    //analogReference(EXTERNAL);   
  20. }



  21. void loop(){
  22.   sendDataToProcessing('S', Signal);     // send Processing the raw Pulse Sensor data
  23.   if (QS == true){                       // Quantified Self flag is true when arduino finds a heartbeat
  24.         fadeRate = 255;                  // Set 'fadeRate' Variable to 255 to fade LED with pulse
  25.         sendDataToProcessing('B',BPM);   // send heart rate with a 'B' prefix
  26.         sendDataToProcessing('Q',IBI);   // send time between beats with a 'Q' prefix
  27.         QS = false;                      // reset the Quantified Self flag for next time   
  28.      }
  29.   
  30.   ledFadeToBeat();
  31.   
  32.   delay(20);                             //  take a break
  33. }


  34. void ledFadeToBeat(){
  35.     fadeRate -= 15;                         //  set LED fade value
  36.     fadeRate = constrain(fadeRate,0,255);   //  keep LED fade value from going into negative numbers!
  37.     analogWrite(fadePin,fadeRate);          //  fade LED
  38.   }


  39. void sendDataToProcessing(char symbol, int data ){
  40.     Serial.print(symbol);                // symbol prefix tells Processing what type of data is coming
  41.     Serial.println(data);                // the data to send culminating in a carriage return
  42.   }
  43.     Serial.print(symbol);                // symbol prefix tells Processing what type of data is coming
  44.     Serial.println(data);                // the data to send culminating in a carriage return
  45.   }





复制代码

第二个是这样的(看说明叫中断子程序)



  1. volatile int rate[10];                    // array to hold last ten IBI values
  2. volatile unsigned long sampleCounter = 0;          // used to determine pulse timing
  3. volatile unsigned long lastBeatTime = 0;           // used to find IBI
  4. volatile int P =512;                      // used to find peak in pulse wave, seeded
  5. volatile int T = 512;                     // used to find trough in pulse wave, seeded
  6. volatile int thresh = 512;                // used to find instant moment of heart beat, seeded
  7. volatile int amp = 100;                   // used to hold amplitude of pulse waveform, seeded
  8. volatile boolean firstBeat = true;        // used to seed rate array so we startup with reasonable BPM
  9. volatile boolean secondBeat = false;      // used to seed rate array so we startup with reasonable BPM


  10. void interruptSetup(){     
  11.   // Initializes Timer2 to throw an interrupt every 2mS.
  12.   TCCR2A = 0x02;     // DISABLE PWM ON DIGITAL PINS 3 AND 11, AND GO INTO CTC MODE
  13.   TCCR2B = 0x06;     // DON'T FORCE COMPARE, 256 PRESCALER
  14.   OCR2A = 0X7C;      // SET THE TOP OF THE COUNT TO 124 FOR 500Hz SAMPLE RATE
  15.   TIMSK2 = 0x02;     // ENABLE INTERRUPT ON MATCH BETWEEN TIMER2 AND OCR2A
  16.   sei();             // MAKE SURE GLOBAL INTERRUPTS ARE ENABLED      
  17. }


  18. // THIS IS THE TIMER 2 INTERRUPT SERVICE ROUTINE.
  19. // Timer 2 makes sure that we take a reading every 2 miliseconds
  20. ISR(TIMER2_COMPA_vect){                         // triggered when Timer2 counts to 124
  21.   cli();                                      // disable interrupts while we do this
  22.   Signal = analogRead(pulsePin);              // read the Pulse Sensor
  23.   sampleCounter += 2;                         // keep track of the time in mS with this variable
  24.   int N = sampleCounter - lastBeatTime;       // monitor the time since the last beat to avoid noise

  25.     //  find the peak and trough of the pulse wave
  26.   if(Signal < thresh && N > (IBI/5)*3){       // avoid dichrotic noise by waiting 3/5 of last IBI
  27.     if (Signal < T){                        // T is the trough
  28.       T = Signal;                         // keep track of lowest point in pulse wave
  29.     }
  30.   }

  31.   if(Signal > thresh && Signal > P){          // thresh condition helps avoid noise
  32.     P = Signal;                             // P is the peak
  33.   }                                        // keep track of highest point in pulse wave

  34.   //  NOW IT'S TIME TO LOOK FOR THE HEART BEAT
  35.   // signal surges up in value every time there is a pulse
  36.   if (N > 250){                                   // avoid high frequency noise
  37.     if ( (Signal > thresh) && (Pulse == false) && (N > (IBI/5)*3) ){        
  38.       Pulse = true;                               // set the Pulse flag when we think there is a pulse
  39.       digitalWrite(blinkPin,HIGH);                // turn on pin 13 LED
  40.       IBI = sampleCounter - lastBeatTime;         // measure time between beats in mS
  41.       lastBeatTime = sampleCounter;               // keep track of time for next pulse

  42.       if(secondBeat){                        // if this is the second beat, if secondBeat == TRUE
  43.         secondBeat = false;                  // clear secondBeat flag
  44.         for(int i=0; i<=9; i++){             // seed the running total to get a realisitic BPM at startup
  45.           rate[i] = IBI;                     
  46.         }
  47.       }

  48.       if(firstBeat){                         // if it's the first time we found a beat, if firstBeat == TRUE
  49.         firstBeat = false;                   // clear firstBeat flag
  50.         secondBeat = true;                   // set the second beat flag
  51.         sei();                               // enable interrupts again
  52.         return;                              // IBI value is unreliable so discard it
  53.       }   


  54.       // keep a running total of the last 10 IBI values
  55.       word runningTotal = 0;                  // clear the runningTotal variable   

  56.       for(int i=0; i<=8; i++){                // shift data in the rate array
  57.         rate[i] = rate[i+1];                  // and drop the oldest IBI value
  58.         runningTotal += rate[i];              // add up the 9 oldest IBI values
  59.       }

  60.       rate[9] = IBI;                          // add the latest IBI to the rate array
  61.       runningTotal += rate[9];                // add the latest IBI to runningTotal
  62.       runningTotal /= 10;                     // average the last 10 IBI values
  63.       BPM = 60000/runningTotal;               // how many beats can fit into a minute? that's BPM!
  64.       QS = true;                              // set Quantified Self flag
  65.       // QS FLAG IS NOT CLEARED INSIDE THIS ISR
  66.     }                       
  67.   }

  68.   if (Signal < thresh && Pulse == true){   // when the values are going down, the beat is over
  69.     digitalWrite(blinkPin,LOW);            // turn off pin 13 LED
  70.     Pulse = false;                         // reset the Pulse flag so we can do it again
  71.     amp = P - T;                           // get amplitude of the pulse wave
  72.     thresh = amp/2 + T;                    // set thresh at 50% of the amplitude
  73.     P = thresh;                            // reset these for next time
  74.     T = thresh;
  75.   }

  76.   if (N > 2500){                           // if 2.5 seconds go by without a beat
  77.     thresh = 512;                          // set thresh default
  78.     P = 512;                               // set P default
  79.     T = 512;                               // set T default
  80.     lastBeatTime = sampleCounter;          // bring the lastBeatTime up to date        
  81.     firstBeat = true;                      // set these to avoid noise
  82.     secondBeat = false;                    // when we get the heartbeat back
  83.   }

  84.   sei();                                   // enable interrupts when youre done!
  85. }// end isr



复制代码

我想在中间加一个霍尔传感器,就是,如果霍尔传感器触发了,就按照以上程序执行(即小灯随着模拟信号闪亮同时电机也会随着心跳振动),如果霍尔开关没有被触发,小灯泡进行一个呼吸灯的过程
我改完的代码如下

  1. //  VARIABLES
  2. int pulsePin =A1;                 // Pulse Sensor purple wire connected to analog pin 0
  3. int blinkPin = 13;                // pin to blink led at each beat
  4. int fadePin = 5;                  // pin to do fancy classy fading blink at each beat
  5. int hallPin= 7;                    //霍尔数字引脚
  6. int motorPin = 6;                   //电机振动引脚
  7. int fadeRate = 0;                 // used to fade LED on with PWM on fadePin
  8. int breathingPin=A3;               //呼吸灯引脚
  9. int x = 0;  
  10. int y = 5;   



  11. // these variables are volatile because they are used during the interrupt service routine!
  12. volatile int BPM;                   // used to hold the pulse rate
  13. volatile int Signal;                // holds the incoming raw data
  14. volatile int IBI = 600;             // holds the time between beats, must be seeded!
  15. volatile boolean Pulse = false;     // true when pulse wave is high, false when it's low
  16. volatile boolean QS = false;        // becomes true when Arduoino finds a beat.


  17. void setup(){
  18.   pinMode(hallPin,INPUT);
  19.   pinMode(blinkPin,OUTPUT);         // pin that will blink to your heartbeat!
  20.   pinMode(fadePin,OUTPUT);          // pin that will fade to your heartbeat!
  21.   Serial.begin(115200);             // we agree to talk fast!
  22.      pinMode(A1, OUTPUT);      //呼吸灯模式引脚
  23. interruptSetup();                 // sets up to read Pulse Sensor signal every 2mS
  24.    // UN-COMMENT THE NEXT LINE IF YOU ARE POWERING The Pulse Sensor AT LOW VOLTAGE,
  25.    // AND APPLY THAT VOLTAGE TO THE A-REF PIN
  26.    //analogReference(EXTERNAL);   
  27. }

  28. void loop(){
  29.   if (digitalRead(hallPin)==HIGH){
  30.     sendDataToProcessing('S', Signal);     // send Processing the raw Pulse Sensor data
  31.   if (QS == true){                       // Quantified Self flag is true when arduino finds a heartbeat
  32.         fadeRate = 255;                  // Set 'fadeRate' Variable to 255 to fade LED with pulse
  33.         sendDataToProcessing('B',BPM);   // send heart rate with a 'B' prefix
  34.         sendDataToProcessing('Q',IBI);   // send time between beats with a 'Q' prefix
  35.         QS = false;                      // reset the Quantified Self flag for next time   
  36.      }
  37.   
  38.   ledFadeToBeat();
  39.   
  40.   delay(20);                             //  take a break以上过程是数字输出LED闪烁,不能删



  41. void ledFadeToBeat(){
  42.     fadeRate -= 15;                         //  set LED fade value
  43.     fadeRate = constrain(fadeRate,0,255);   //  keep LED fade value from going into negative numbers!
  44.     analogWrite(motorPin,fadeRate);        //电机振动信号
  45.     analogWrite(fadePin,fadeRate);          //  fade LED
  46.   }


  47. void sendDataToProcessing(char symbol, int data ){
  48.     Serial.print(symbol);                // symbol prefix tells Processing what type of data is coming
  49.     Serial.println(data);                // the data to send culminating in a carriage return
  50.   }
  51.   }
  52.   else (digitalRead(hallPin)==LOW)
  53.   {
  54.    analogWrite(breathingPin, x);  
  55.    x = x + y;
  56.    delay(30);   
  57.    if (x==0 || x==255)
  58.      {
  59.       y=-y;
  60.      }              
  61.   
  62.   
  63.   
  64.   }
  65.   }
复制代码


之后就报错了,说有几个东西我没有声明,但是原来代码就这样啊。。。想不懂了,求大大们指点迷津

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2016-4-4 19:30:32 | 显示全部楼层
把自定义函数放在loop的外面试试,你好像放在了if的里面
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-4 20:11:14 | 显示全部楼层
janeasy 发表于 2016-4-4 19:30
把自定义函数放在loop的外面试试,你好像放在了if的里面

嗯嗯,是的,刚才我也请教了我的同学,谢啦,之后又出现了一些问题
回复 支持 反对

使用道具 举报

发表于 2016-4-7 10:13:49 | 显示全部楼层
本帖最后由 li23108 于 2016-4-7 10:25 编辑

这里错的太多了。  大括号范围,if  else  反了。


  1. void sendDataToProcessing(char symbol, int data ){
  2.     Serial.print(symbol);                // symbol prefix tells Processing what type of data is coming
  3.     Serial.println(data);                // the data to send culminating in a carriage return
  4.   }
  5.   }
  6.   else (digitalRead(hallPin)==LOW)
  7.   {
  8.    analogWrite(breathingPin, x);  
  9.    x = x + y;
  10.    delay(30);   
  11.    if (x==0 || x==255)
  12.      {
  13.       y=-y;
  14.      }              



  15.   }
  16.   }
复制代码



你看看这样
  1. void sendDataToProcessing(char symbol, int data ){
  2.         Serial.print(symbol);
  3.         Serial.println(data);
  4.        
  5.         if (digitalRead(hallPin)==LOW){
  6.                 analogWrite(breathingPin, x);  
  7.                 x = x + y;
  8.                 delay(30);
  9.                 }//if end
  10.         else(x==0 || x==255){
  11.                 x=-y;  //应该是x-y吧?  y-y 是做什么呢?
  12.                 }//else end            
  13. }
复制代码



我也是新手。也在学习。希望能帮到你。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-18 21:23:03 | 显示全部楼层
以下是调试以后成功的代码,
实现的功能:当霍尔传感器没有触发时,A3小灯泡呼吸灯闪烁
当霍尔传感器触发时5引脚小灯泡随脉搏跳动同时振动电机随心跳振动
  1. //  VARIABLES
  2. int pulsePin = A1;                 // Pulse Sensor purple wire connected to analog pin 0
  3. int blinkPin = 13;                // pin to blink led at each beat
  4. int fadePin =5;                  // pin to do fancy classy fading blink at each beat
  5. int hallPin= 7;
  6. int fadeRate = 0;                 // used to fade LED on with PWM on fadePin
  7. int motorPin = 6;
  8. int breathingPin= A3;
  9. int x = 0;
  10. int y = 5;
  11. // these variables are volatile because they are used during the interrupt service routine!
  12. volatile int BPM;                   // used to hold the pulse rate
  13. volatile int Signal;                // holds the incoming raw data
  14. volatile int IBI = 600;             // holds the time between beats, must be seeded!
  15. volatile boolean Pulse = false;     // true when pulse wave is high, false when it's low
  16. volatile boolean QS = false;        // becomes true when Arduoino finds a beat.


  17. void setup(){
  18. pinMode(hallPin,INPUT);
  19.   pinMode(blinkPin,OUTPUT);         // pin that will blink to your heartbeat!
  20.   pinMode(fadePin,OUTPUT);
  21.    pinMode(motorPin,OUTPUT); // pin that will fade to your heartbeat!
  22.   Serial.begin(115200);             // we agree to talk fast!
  23.   interruptSetup();                 // sets up to read Pulse Sensor signal every 2mS
  24.    // UN-COMMENT THE NEXT LINE IF YOU ARE POWERING The Pulse Sensor AT LOW VOLTAGE,
  25.    // AND APPLY THAT VOLTAGE TO THE A-REF PIN
  26.    //analogReference(EXTERNAL);   
  27. }



  28. void loop(){
  29.   sendDataToProcessing('S', Signal);     // send Processing the raw Pulse Sensor data
  30.   if (QS == true){                       // Quantified Self flag is true when arduino finds a heartbeat
  31.         fadeRate = 255;                  // Set 'fadeRate' Variable to 255 to fade LED with pulse
  32.         sendDataToProcessing('B',BPM);   // send heart rate with a 'B' prefix
  33.         sendDataToProcessing('Q',IBI);   // send time between beats with a 'Q' prefix
  34.         QS = false;                      // reset the Quantified Self flag for next time   
  35.      }
  36.    if (digitalRead(hallPin)==HIGH){
  37. analogWrite(breathingPin,0);
  38.   ledFadeToBeat();
  39.    }
  40.    else if(digitalRead(hallPin)==LOW){
  41.   analogWrite(motorPin,0);
  42.    analogWrite(breathingPin, x);  
  43.    x = x + y;
  44.    delay(30);   
  45.    if (x==0 || x==255) {
  46.       y=-y;
  47.   }  
  48.   }
  49.   delay(20);                             //  take a break
  50. }


  51. void ledFadeToBeat(){
  52.     fadeRate -= 15;                         //  set LED fade value
  53.     fadeRate = constrain(fadeRate,0,255);   //  keep LED fade value from going into negative numbers!
  54.      analogWrite(motorPin,fadeRate);
  55.     analogWrite(fadePin,fadeRate);          //  fade LED
  56.   }


  57. void sendDataToProcessing(char symbol, int data ){
  58.     Serial.print(symbol);                // symbol prefix tells Processing what type of data is coming
  59.     Serial.println(data);                // the data to send culminating in a carriage return
  60.   }





复制代码

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-18 08:19 , Processed in 0.066278 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表