我想在我的 atTiny 中不循环任何内容,直到我读取特定输入或达到最大时间。读数来自 Arduino。
这是我的示例代码,但它似乎不起作用,代码直接跳出循环。
void openGate()
{
//Set Read Gate to Pullup will LOW when closed and HIGH when open
pinMode(readGate,INPUT_PULLUP);
//We want this PIN to output for the time the loop runs
digitalWrite(pinGate, HIGH);
whileCounter = 0;
//Wait for Reading HIGH when the Arduino Opens the Output Pin
//Or Exit when a certain time is reached
while(digitalRead(readGate) == LOW || whileCounter >= waitTime ) {
delay(1);
whileCounter++;
}
digitalWrite(pinGate, LOW);
}
在 Arduino 中,我简单地将 PIN 更改为 OUTPUT,这部分工作正常。
pinMode(2, OUTPUT);
digitalWrite(2,LOW);
提前致谢!干杯
将注释中的代码更改为主循环
void loop(){
if (f_wdt==1) { // wait for timed out watchdog / flag is set when a watchdog timeout occurs
if(circles <= count){
//Start Setup our PINS and the millis to compare
if(f_start==0){
pinMode(pinGate,OUTPUT); //Set Gate as Output
pinMode(readGate,INPUT_PULLUP); //Set Read Gate to Pullup will LOW when closed and HIGH when open
unsigned long startMillis = millis(); //Snapshot of time
f_start = 1;
}
if(f_start==1){
digitalWrite(pinGate, HIGH); //Output High to MOSFET to open the gate
if(digitalRead(readGate) == LOW) f_done = 1; //We got a signal, lets start new sleep circle
unsigned long currentMillis = millis(); //Our current Millis
if ((unsigned long)(currentMillis - startMillis) >= waitTime) f_done = 1; //Timeout time is reached, lets start new sleep circle
}
if(f_done==1){
digitalWrite(pinGate, LOW);
pinMode(pinGate,INPUT); // set all used port to intput to save power
pinMode(readGate,INPUT);
f_start = 0; // reset setup flag
f_done = 0; // reset open circle flag
count = 0; // reset sleep cycle count
f_wdt=0; // reset watchdog flag
system_sleep(); // back to sleep little tiny
}
} else {
count++;
f_wdt=0; // reset watchdog flag
system_sleep(); // back to sleep little tiny
}
}
}