我写这个程序是为了检查一些东西,但是对我来说它不起作用。
int counter = 0;
int aState;
int aLastState;
int StateAreTheSamePrinted = 0;
int StatePrinted = 0;
void setup() {
// put your setup code here, to run once:
pinMode(outputA, INPUT);
pinMode(3, INPUT);
Serial.begin(115200);
aLastState = digitalRead(outputA);
Serial.print("Arduino started");
}
void loop() {
// put your main code here, to run repeatedly:
aState = digitalRead(outputA);
if(StatePrinted == 0)
{
Serial.print(aState);
StatePrinted = 1;
}
if(aState == aLastState && StateAreTheSamePrinted == 0)
{
Serial.print("States are the same");
StateAreTheSamePrinted = 1;
}
if(aState != aLastState)
{
if(aState == 1){
Serial.print("A is high \n");
}
else
{
Serial.print("A is low \n");
}
StateAreTheSamePrinted = 0;
StatePrinted = 0;
}
aLastState = aState;
}
它总是打印一旦arduino启动,1(输入状态),和状态是相同的,当我电线5v从arduino到端口7,有时它React一次,有时没有,几分钟后,它开始打印输出像50-100行的消息,并停止和滞后再次.
我原以为在给arduino 7端口供电后,它会打印A为高或A为低,并在它们之间切换
1条答案
按热度按时间wb1gzix01#
听起来你需要在状态改变后“去抖动”。当你把5V钩到引脚上时,它可能不会马上建立稳定的连接,所以它会非常快地改变状态几次。一个简单的修复方法是在它检测到状态改变后放置一个~ 10 ms的延迟。