我想在光障(连接到wsensP引脚)检测到某些东西时立即停止电机(“脉冲”功能)。
我用“上升中断”做了第一个测试:
void setup() { attachInterrupt(digitalPinToInterrupt(wsensP), dropped, RISING);}
void dropped(){ wsensor = 1;}
void drop{
wsensor = 0;
while (!wsensor){
pulse();
}
}而且它工作得很完美:一旦水滴落入光障,“脉冲”函数就不再被调用。
但我想要有更详细的检测,于是我切换到“更改”中断:
void setup() { attachInterrupt(digitalPinToInterrupt(wsensP), detected, CHANGE); }
void detected(){
wsensor = !wsensor;
if (wsensor){
rising = 1;
} else {
rising = 0;
}
}
void drop() {
rising = 0;
while (!rising){
pulse();
}
}而且这并不会在第一个上升沿停止!我尝试直接测试"wsensor“变量(在while条件中)。我添加了这个“上升”布尔值,因为我认为在while条件被测试之前,王量可以有时间循环1和0……
我真的不明白我的代码出了什么问题。ISR是最小的(设置2个变量),其余的也非常简单……
发布于 2021-05-05 23:54:19
我明白了:问题是传感器可能有一个非常短的高电平,所以在while条件下测试之前,变量"rising“被设置为0。
解决方案==>删除"else { rising =0 }“
void setup() { attachInterrupt(digitalPinToInterrupt(wsensP), detected, CHANGE); }
void detected(){
wsensor = !wsensor;
if (wsensor){
rising = 1;
}
// part of the code to delete :
// else {
// rising = 0;
// }
}
void drop() {
rising = 0;
while (!rising){
pulse();
}
}https://stackoverflow.com/questions/67400293
复制相似问题