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