atTiny85循环,直到没有读取arduino或超时

时间:2016-08-30 16:19:32

标签: arduino attiny

我想在我的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
  }
 }
}

1 个答案:

答案 0 :(得分:0)

对不起,我的大脑放屁。 INPUT_PULLUP是标准的HIGH而whileCounter是错误的,因为@Pawel提到了。

  while(digitalRead(readGate) == HIGH && whileCounter <= waitTime ) { 

对不起。