0

I would like to loop nothing in my atTiny till I get a read on an specific Input or the maximum time is reached. The reading comes from Arduino.

Here is my sample code but it does not seem to work, the code jumps straight out of the loop.

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);
}

In Arduino I do simple change the PIN to OUTPUT, this part works fine.

pinMode(2, OUTPUT);
digitalWrite(2,LOW);

Thanks in advance! Cheers

changed code as told in comments into the main loop

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
  }
 }
}
  • should it be: `while(digitalRead(readGate) == LOW && whileCounter < waitTime )`? – Pawel Aug 30 '16 at 19:17
  • Yeah thanks, also the digitalRead is HIGH because I use INPUT_PULLUP – Hannes Oberreiter Aug 30 '16 at 19:36
  • 1. delay(1) is rather bad to build a timeout counter. Check and understand the BlinkWithoutDelay example. 2. Usually, you're best off, if your code does not need while loops at all. the main loop() should do all such stuff for you. – datafiddler Aug 31 '16 at 09:39
  • Hi @datafiddler thanks for the Input, I changed the code to be inside the main loop, lots of IF now. Do you think this is better? Always on the lookout to improve my skills! – Hannes Oberreiter Aug 31 '16 at 17:00
  • Sure it's better. If i see it correctly, you might save one IF and the variable f_done, but perhaps its clearer this way. And it's never good to hide the important part of the code (variable definitions). What's *circles* ? – datafiddler Sep 03 '16 at 12:40
  • Hi @datafiddler you can see my full code here https://github.com/HannesOberreiter/bScale/blob/master/attiny/attiny.ino did change it all a little bit around now it runs since 3 days without problems so far – Hannes Oberreiter Sep 04 '16 at 15:09

1 Answers1

0

Sorry, was a brain fart by me. INPUT_PULLUP is standard HIGH and whileCounter was the wrong way around as @Pawel mentioned.

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

Sorry.