简体   繁体   English

通过 rx 引脚接收数据时如何中断 arduino

[英]how to interrupt arduino when data recieved via rx pin

what am I doing wrong ? I have the RX of the Bluetooth HC-07 wired into pin2 looking for a change in voltage when data is received to start a interrupt. 

I'm trying to cause an interrupt when data is received via the Bluetooth.     


    #include <SoftwareSerial.h>// import the serial library
    #include <PinChangeInt.h>


    SoftwareSerial Genotronex(10, 11); // RX, TX
    int ledpin=13; // led on D13 will show blink on / off
    int BluetoothData; // the data given from Computer
    void doSomething(void);

    void setup() {
      // wil run once
      Genotronex.begin(9600);// data rate for comunicating 
      Genotronex.println("Bluetooth On please press 1 or 0 blink LED ..");
      pinMode(ledpin,OUTPUT);


      interrupts();
      pinMode(2,INPUT);
      attachInterrupt(0,doSomething,  CHANGE);
    }

    void loop() {
      // Main body of code


    }



    void doSomething(){
     Genotronex.println("INTERRUPT!!!!!!!!!!!!");

    digitalWrite(ledpin,1);
    delay(1000);
    digitalWrite(ledpin,0);
    delay(1000);

    detachInterrupt(0);

    }

////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////

This is a repost of the code ,, I've taken out what the comments said and added what you recommended,, for the most part.这是代码的重新发布,我已经删除了评论所说的内容并添加了您推荐的内容,大部分。 The code runs but change state never changes back it only changes once?代码运行但改变状态永远不会改变它只改变一次? I've tried taking out DetachInterrupt but the actual interrupt stops working then ?我试过去掉 DetachInterrupt 但实际的中断停止工作了吗? Any more ideas?还有更多想法吗? Thanks for the help btw谢谢你的帮助顺便说一句

#include <SoftwareSerial.h>// import the serial library



SoftwareSerial Genotronex(10, 11); // RX, TX
int ledpin=13; // led on D13 will show blink on / off
int BluetoothData; // the data given from Computer
void doSomething(void);
volatile int state = LOW;


void setup() {
  // wil run once
  Genotronex.begin(9600);// data rate for comunicating 
  Genotronex.println("Bluetooth On please press 1 or 0 blink LED ..");
  pinMode(ledpin,OUTPUT);



  pinMode(2,INPUT);
  attachInterrupt(0,doSomething,  CHANGE);
}

void loop() {
  // Main body of code
digitalWrite(ledpin, state);

}



void doSomething(){

Genotronex.println("INTERRUPT!!!!!!!!!!!!");



 state = !state;



detachInterrupt(0);

}

delete: detachInterrupt(0);删除: detachInterrupt(0);

If you can change interrupt from CHANGE to HIGH/LOW and add something like 'debouncing'.如果您可以将中断从 CHANGE 更改为 HIGH/LOW 并添加诸如“去抖动”之类的内容。 My version here (HIGH state interrupts):我这里的版本(高状态中断):

void interruptfunction() { 

      //your interrupt code here

  while(1){
    if (digitalRead(2)==LOW){
      delay(50);
      if (digitalRead(2)==LOW) break;
    }
  }
}

Or this: (not tested)或者这个:(未测试)

void interruptfunction() { 

      //your interrupt code here

  if (digitalRead(2)==HIGH) state=true;
  else state=false;

  while(1){
    if (digitalRead(2)!=state){
      delay(50);
      if (digitalRead(2)!=state) break;
    }
  }
}

If you have to react on CHANGE try leaving detachInterrupt(0);如果您必须对 CHANGE 做出反应,请尝试离开detachInterrupt(0); but reattach it somewhere in your main loop.但将其重新附加到主循环中的某个位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM