digitalwrite delay loop doesen't let me do anything while it sets pins

i’m developing a system to count pulses from a input
i need to be able to count pulses in the hole scope , set a limiter to when pulse reaches a certain ammount it reset the pulse count and then goes into sleep mode and turns back on after a interrupt signal and keeps doing the same. the trouble is that i need to be able to make a loop that turns on and off a relay and makes it so that the relay (connected to a solenoid valve) opens for a random ammount of time, so that the pulse count is irregular
but when i do the normal digitalwrite and delay after the hole program doesent cont pulses while it does it. i tried with the blink without delay method but no luck couse i need the interval to be random this is what i got, i stablished a function for the relayloop but left the attempt at blink with no delay couse nothing worked. any suggestions on the hole code are welcome.

#include <avr/sleep.h>
#define interruptPin 2
long randOn = 0;                
long randOff = 0; 

const int sensorPin = 3;
const int ledPin2 = 8;
const int ledPin = 7;
int timeSec;  
unsigned long myTime;
int pulse = 0;
int litrosServidor = 0;

int var = 0;
int desligarSolenoide = 0;
inline void funcaoRelay();
inline void pulseCounter();
inline void tempoReset();
inline void limitadorPulso();
int secTime = 0;
int valorDia = 0;
int ledState = LOW;
unsigned long previousMillis = 0;
int long interval = 0;   

void setup() {
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(2, INPUT_PULLUP);
   randomSeed (analogRead (0));    // randomize
  pinMode(interruptPin, INPUT_PULLUP);
  pinMode(sensorPin, INPUT);

  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  Serial.println(F("No pulses yet..."));


void loop(){
// get post dos liro
litrosServidor = 10;

 inline void limitadorPulso(){
 if(pulse >= litrosServidor){
  digitalWrite(8, HIGH);
  digitalWrite(8, LOW);
  digitalWrite(7, LOW);




inline void funcaoRelay(){
  if(pulse < litrosServidor){
 unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= random(1000, 6000)){
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    } else {
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);


inline void tempoReset(){
  // get post dos purso
  pulse = 0;

void Going_To_Sleep(){
  attachInterrupt(0,wakeUp, LOW); 
  digitalWrite(LED_BUILTIN, LOW);
  Serial.println("going to sleep");
  Serial.println("just woke up!");
  digitalWrite(LED_BUILTIN, HIGH);
void wakeUp(){
    Serial.println("Interrupt Fired");
 inline void pulseCounter(){

  if(digitalRead(sensorPin) > var)
    var = 1;
    Serial.print(F(" pulse"));

    if(pulse > 1) {
    Serial.println(F(" detected."));

  if(digitalRead(sensorPin) == 0) {var = 0;}


it sounds like you want to energize a relay to open a valve dispensing a fluid, count pulses that represent some measure of the liquid and close the valve when a sufficient number of pulses is reached.

not sure the above is correct. i'm puzzled why pulseCounter() is invoked in several places in your code rather than simply in the place where checking the count is necessary.

in order to not miss counting a pulse, nothing time consuming such as serial prints should be performed and certainly no delays.

counting pulses should be: continually read an input pin, look for changes in state and for one state value (LOW or HIGH) increase a count. it may make sense to remain in a loop counting pulses until some count is reached.

so, open a valve dispensing fluid, count pulses until the desired number of pulses is reached and close the valve.


#include <avr/sleep.h>

#define interruptPin 2

enum { On = LOW, Off = HIGH };

#if 0
const int sensorPin = 3;
const int ledPin2 = 8;
const int ledPin = 7;

const int sensorPin = A1;
const int startPin  = A2;

const int ledPin2   = 13;
const int ledPin    = 10;

static int litrosServidor = 0;
static byte startPinSt    = Off;
static byte sensorPinSt   = Off;

// -----------------------------------------------------------------------------
void setup () {
    pinMode (ledPin,  OUTPUT);
    pinMode (ledPin2, OUTPUT);
    digitalWrite (ledPin,  Off);
    digitalWrite (ledPin2, Off);

    pinMode (sensorPin, INPUT_PULLUP);
    pinMode (startPin,  INPUT_PULLUP);

    Serial.begin (9600);
    Serial.println ("press start button to dispense");

// ---------------------------------------------------------
void loop (){
    // check for start button pressed
    byte but = digitalRead (startPin);
    if (startPinSt != but)  {
        startPinSt = but;

        if (On == but)  {
            litrosServidor = 5;
            digitalWrite (ledPin2, On);
            Serial.println ("start");

    if (0 < litrosServidor)
        limitadorPulso ();

// ---------------------------------------------------------
inline void limitadorPulso (){
    // check for sensor pin going LOW
    byte pin = digitalRead (sensorPin);
    if (sensorPinSt != pin)  {
        sensorPinSt = pin;

        if (On == pin)  {
            // stop dispensor
            if (0 == --litrosServidor)  {
                Serial.println ("stop");
                digitalWrite (ledPin,  Off);
                digitalWrite (ledPin2, Off);

        delay (10);
    // blink LED every 1/4 second
    static unsigned long msecLst = 0;
           unsigned long msec    = millis ();

    if (msec - msecLst > 250)  {
        msecLst = msec;
        digitalWrite (ledPin, ! digitalRead (ledPin));

// -----------------------------------------------------------------------------
// my hardware doesn't have access to the interrupt pins
// to exercise this code

void Going_To_Sleep (){
    Serial.println (__func__);

    sleep_enable ();
    attachInterrupt (0,wakeUp, LOW);
    set_sleep_mode (SLEEP_MODE_PWR_DOWN);
    digitalWrite (LED_BUILTIN, LOW);
    Serial.println ("going to sleep");
    delay (1000);
    sleep_cpu ();
    Serial.println ("just woke up!");
    digitalWrite (LED_BUILTIN, HIGH);

void wakeUp (){
    Serial.println (__func__);

    Serial.println ("Interrupt Fired");
    sleep_disable ();
    detachInterrupt (0);