c - 当通过rx pin接收到数据时如何中断arduino

标签 c bluetooth arduino

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);

    }

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

这是代码的重新发布,,我已经删除了评论所说的内容,并添加了您推荐的大部分内容。代码运行但更改状态永远不会变回它只更改一次?我尝试取出 DetachInterrupt 但实际的中断停止工作然后?还有更多想法吗?感谢您的帮助顺便说一句

#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);

}

最佳答案

删除: 分离中断(0);

如果您可以将中断从“CHANGE”更改为“HIGH/LOW”并添加“debouncing”之类的内容。我的版本在这里(高状态中断):

void interruptfunction() { 

      //your interrupt code here

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

或者这个:(未测试)

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;
    }
  }
}

如果您必须对 CHANGE 使用react,请尝试离开 detachInterrupt(0); 但将其重新附加到主循环中的某个位置。

关于c - 当通过rx pin接收到数据时如何中断arduino,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21345350/

相关文章:

c - 有没有办法让我的补充代码更加高效并允许一次多个输出?

matlab - 延迟连接 Arduino 和 Simulink

c - 我的二维数组边界算法有什么问题?

c - 在 C 中反转链表时出现运行时错误

c - 如何在C中组合两个不同数据类型的变量?

ios - objective c 检查蓝牙可用性是否改变

android - 我可以绑定(bind)低功耗蓝牙设备吗?

c - 错误 : expected expression before "{"

actionscript-3 - 如何通过 AS3 连接和发送数据到蓝牙?

c++在arduino中传递HIGH和LOW的 vector