我正在使用arduino uno板开发项目,正在使用与交换机绑定的外部中断 我希望仅当我向开发板发送激活订单时此开关才能工作 问题是,如果在我发送命令之前按下了开关,即使发送命令没有按下,我在发送命令后也会处于按下状态,这意味着外部中断会在我之前保存该状态并在我启用后对其进行检索它 这是代码段
volatile boolean EX_INT = 0, activate = 0;
const byte interruptBin = 3;
const byte ACTIVATE = 0x55;
unsigned char frame[] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36};
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(interruptBin, INPUT_PULLUP);
}
void loop() {
if(activate == 1){
//EIMSK =0; EIFR = 0; I tried to clear the last interrupt but with no effect
activate = 0; EX_INT = 0;
attachInterrupt(digitalPinToInterrupt(interruptBin),buttonPressed,RISING);
while(EX_INT != 1);
EX_INT = 0;
Serial.write((uint8_t*)frame, sizeof(frame));
detachInterrupt(digitalPinToInterrupt(interruptBin));
}
}
void serialEvent(){
while (Serial.available()){
value = Serial.read();
if(value == ACTIVATE)
activate = 1;
}
}
void buttonPressed()
{
EX_INT = 1;
}
答案 0 :(得分:2)
您可以在中断处理程序中检查activate
:
void buttonPressed()
{
if(activate) {
EX_INT = 1;
}
}