Using snore function for ATtiny 85

Has anyone else had success using the snore library in projects?
My project involves a probe to determine if the water level falls below it and if so to turn on a pump. I want to only check the level occasionally to save power and was hoping the snore function would allow me to do this. The probe and pump work fine but as soon as I introduce the snore function the pump stays on all the time independent of the probe condition.

Sorry I have forgotten how to use codetags. I thought code was preceded by a < and to finish a > but it didn't work.

#include "tinysnore.h" // Include TinySnore Library
// Pin definitions
const uint8_t probePin = 0;       // Pin connected to the water level probe
const uint8_t pumpControlPin = 1; // Pin connected to the pump control MOSFET


void setup() {
  pinMode(probePin, INPUT);       // Probe as input
  pinMode(pumpControlPin, OUTPUT); // Output to control the pump
}

void loop() {
  delay(10); // Short delay for stability
  bool waterIsLow = digitalRead(probePin) == HIGH;
  while (waterIsLow)
  {
    digitalWrite(pumpControlPin, HIGH);
  }
  digitalWrite(pumpControlPin, LOW);
  snore(20000); // Deep sleeps for 20 seconds, (low power) then resumes from here
}

I added the code tags for you this time.

More info in How to get the best out of this forum

you can insert the tags by clicking on the code tool in the toolbar above your post
image

or by using the menu copy the code for the forum in the IDE which will add the code tags and let you paste in directly here (indent before).

How do you think you'll exit from the while statement ? (and why do you keep setting the pin HIGH every few microseconds, once is enough until you turn it off).

➜ you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

Your problem does not appear to have anything to do with snoring.

Post the code that does not snore but works as you describe.

The code you did post minus any snoring will eventually turn on the pump. And never again turn it off there after.

You may want to review the operation of the while statement.

https://docs.arduino.cc/language-reference/en/structure/control-structure/while/

Hint has been delivered:

a7

Thanks for the replies.
I have changed the code see below and this works perfectly without the statement
snore(20000); // Deep sleeps for 20 seconds, (low power) then resumes from here.

However when this statement is included, it operates unreliably particularly when a probe state changes during the snore period. At times when the probe state is high, the pump doesn't operate and other times, the pump may operate even when the probe state is low. In each case allowing for any 20 sec snooze time. It is bizarre.

Just reiterating, it works perfectly without the snore code.

#include "tinysnore.h" // Include TinySnore Library
// Pin definitions
const uint8_t probePin = 0;       // Pin connected to the water level probe
const uint8_t pumpControlPin = 1; // Pin connected to the pump control MOSFET


void setup() {
  pinMode(probePin, INPUT);       // Probe as input
  pinMode(pumpControlPin, OUTPUT); // Output to control the pump
}

void loop() {
  delay(10); // Short delay for stability

  if (digitalRead(probePin) == HIGH)
  {
    digitalWrite(pumpControlPin, HIGH);
  }
else
  {
    digitalWrite(pumpControlPin, LOW);
    snore(20000); // Deep sleeps for 20 seconds, (low power) then resumes from here
  }
}

Does the probe pin needs a pull-up or pull-down?

This shows the water level controller schematic. There is no need for a pullup resistor as the FET attached to the input probe will supply a high or low to the PB0. This added FET was used so that when the water is quite pure it will respond whereas applying PB0 direct via the probe and including pinMode(probePin, INPUT_PULLUP) would not work with very pure water.

However, using the the unit without the input FET gave the same results. A simple switch was used to leave the probe either open or short circuited for convenience.

I will try and use a sleep method using a watchdog timer to see if I get correct results.

How is the probe powered ?

One side of the probe connects to VCC and the P chan FET source whilst the other side connects to the FET gate as well as to a resistor to the negative of the supply. The drain of the FET connects to the PB0 of the ATtiny85. When the probe is open circuited, it turns on the FET and puts Vcc to PB0. When water is applied to the probe and providing it is less than about 2Mohms, the FET is turned off and PB0 is low. I could have used a N chan FET but I didn't want to change the s/w. Note that when the FET is used, the s/w PULLUP is not used.

I have tried many iterations of the software and hardware but although it works for a while eventually I get inconsistencies using "snore". Other people have also experienced similar problems using the "tinysnore.h" library. I quote one such user "I have a really simple script running on a ATtiny85 which turns on a ESP8266 every 30 minutes to sent a temperature reading to domoticz. I am using the tinysnore.h library to make the tiny go into deep sleep. It works for a few hours and then the relay connected to the ESP stays on and drains the battery. I can't seem to figure out what is the problem."
The funny thing is I have used this library to produce very simple timers without any problems. I am at the point of giving up unless someone can point out a possible s/w solution.