atTiny85 循环直到没有从 arduino 读取或超时
atTiny85 loop till no read from arduino OR timeout
我不想在我的 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
}
}
}
对不起,我脑子放屁了。 INPUT_PULLUP 是标准的 HIGH,whileCounter 是@Pawel 提到的错误方法。
while(digitalRead(readGate) == HIGH && whileCounter <= waitTime ) {
对不起。
我不想在我的 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
}
}
}
对不起,我脑子放屁了。 INPUT_PULLUP 是标准的 HIGH,whileCounter 是@Pawel 提到的错误方法。
while(digitalRead(readGate) == HIGH && whileCounter <= waitTime ) {
对不起。