Timed Function from input without using delays ?

Been trying to wrap my head around it but, its now bloody from bashing it against the wall.

What im trying to do is grab a sensor value (analog input) once every (insert desired time here) but lets say 10 mins and if that sensor value is >= an arbitrary value, then it causes a second time function to happen such as a relay turning on for an arbitrary set of time, say 30secs, and turning back off after that time, but having it all run again when that 10mins is up.

Now all this can easily be done with delays, but there is the obvious problem of it delaying all your other code you might be running, so ive been looking into the whole blinkwithoutdelay tutorials which i understand (i think) but when adding in the second time function i get mixed up.

Also i cant use timers as the code im already running in a program uses my timers for pulse width modulation.

So a point form run down of it again

Check sensor >= (number)
if (turn on digitalouput for 30sec)
else (do nothing)

Till current time is >= the interval we set aka 10mins

then repeats

all without delays

any info or links where there is a second time function involved would be appreciate
Thanks ahead of time

If needed i am running a RTC in a section of my other code that could be used, but it seems doable to be able to do it without using it, just cant wrap my head around it

Have you read this? several things at a time

To get help, you must show us your complete sketch. Attach your code using the </> icon on the left side of the posting menu. Show us what you have so far.

Maybe this will give you some ideas:

//Blink without Delay skeleton
//LarryD
 
unsigned long Wait13         = millis();
unsigned long Wait12         = Wait13;
unsigned long Wait11         = Wait13;
unsigned long Wait10         = Wait13;
unsigned long ToggleWait10   = Wait13;
unsigned long Wait9          = Wait13;
unsigned long currentmillis  = Wait13;
unsigned long DebounceWait   = Wait13;
unsigned long debounceMillis = 50UL;
 
byte lastMySwitchState       = 1;
byte counter                 = 0;
 
//the following are enable flags
//some of these might not be used
boolean flag13      = true;
boolean flag12      = true;
boolean flag11      = true;
boolean flag10      = true;
boolean flag9      = true;
 
const byte mySwitch = 2;
 
//**********************************************************************
 
void setup()
{
  Serial.begin(9600);
 
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9,  OUTPUT);
 
  pinMode(mySwitch, INPUT_PULLUP);
 
 
} //  >>>>>>>>>>>>>> E N D  O F  s e t u p ( ) <<<<<<<<<<<<<<<<<
 
 
void loop()
{
  //leave this line of code at the top of loop()
  currentmillis = millis();
 
  //5 different ways for controlling a digital output D13 - D9
 
  //***************************
  //toggle pin 13 every 200mS
  if (flag13 == true && CheckTime(Wait13, 200UL, true))
  {
    //if you only want this section of code to happen once
    //uncomment the next line
    //flag13 = false;
 
    //toggle pin 13
    digitalWrite(13,!digitalRead(13));
  }
 
  //***************************
  //after 3 seconds, pin 12 goes and stays HIGH
  if (flag12 == true && CheckTime(Wait12, 3000UL, true))
  {
    digitalWrite(12,HIGH);
    //disable above code
    flag12 = false;
  }
 
  //***************************
  //pin 11 is HIGH for 10 seconds, then goes and stays LOW
  if (flag11 == true && !CheckTime(Wait11, 10000UL, true))
  {
    digitalWrite(11,HIGH);
  }
 
  //10 seconds is now up, go LOW
  else
  {
    digitalWrite(11,LOW);
    //disable above code
    flag11 = false;
  }
  //***************************
  //for 6 seconds, toggle pin 10
  if (flag10 == true && !CheckTime(Wait10, 6000UL, true))
  {
    //toggle pin 10 every 50mS
    if(CheckTime(ToggleWait10, 50UL, true))
    { 
      digitalWrite(10,!digitalRead(10));   
    }
  }
 
  //6 seconds is now up, stop toggling
  else
  {
    digitalWrite(10,LOW);
    //disable above code
    flag10 = false;
  }
 
  //***************************
  //is it time to check the switches?
  if (CheckTime(DebounceWait, debounceMillis, true))
  {
    checkSwitches();     
  }
 
  //***************************
  //put other non-blocking stuff here
 
 
} //  >>>>>>>>>>>>>> E N D  O F  l o o p ( ) <<<<<<<<<<<<<<<<<
 
 
//======================================================================
//                      F U N C T I O N S
//======================================================================
 
//**********************************************************************
//Delay time expired function
//lastMillis = time we started, wait = delay in mS, restart = do we start again 
boolean CheckTime(unsigned long &lastMillis, unsigned long wait,boolean restart)
{
  //is the time up for this task?
  if (currentmillis - lastMillis >= wait)
  {
    //should this start again?
    if(restart)
    {
      //get ready for the next iteration
      lastMillis += wait;  //get ready for the next iteration
    }
    return true;
  }
  return false;
 
} // END of CheckTime()
 
//**********************************************************************
//switches are checked every debounceValue milli seconds
//no minimum switch press time is validated with this code (i.e. No glitch filter)
void checkSwitches() 
{
  //re-usable for all the switches 
  boolean thisState;   
 
  //***************************   
  //check if this switch has changed state
  thisState = digitalRead(mySwitch);
  if (thisState != lastMySwitchState)
  { 
    //update the switch state
    lastMySwitchState = thisState; 
 
    //this switch position has changed so do some stuff
 
    //"HIGH condition code"
    //switch goes from LOW to HIGH
    if(thisState == HIGH)       
    {
      //example: LED on pin 9 is Push ON, Push OFF
      digitalWrite(9,!digitalRead(9));
    }
 
    //"LOW condition code"
    //switch goes from HIGH to LOW
    else                         
    {
      //example: display the current switch push count
      Serial.println(++counter);     
    }
 
  } //END of mySwitch code
 
  //*************************** 
  //similar code for other switches goes here
 
} //END of checkSwitches()
 
//**********************************************************************
 
//======================================================================
//                      E N D  O F  C O D E
//======================================================================

loop
{
if now - 10min > timestamp
{
timestamp := now
relay on
}

if now - 30 seconds > timestamp and relay is on
{
relay off
}
}

thanks to all, you started me off on the right track

think i solved my problem by using two timers and boolean for a mock one off timed event. the base code that i came up with is as follows and seems to work, still needs further testing but thanks all.

void ssw()
{
  if(currentMillis - previousMillis >= interval) 
    {
      previousMillis = currentMillis;
      if (analogRead(Sensor1) >= value1)
      {
        digitalWrite(Relay_2, LOW);       //Reversed switching actually on
        ssTimerFired = true;
        previousMillis2 = currentMillis;
      }
    }
   if((ssTimerFired) && (currentMillis - previousMillis2 >= interval2)) 
    {
      ssTimerFired = false;
      digitalWrite(Relay_2, HIGH);        //Reversed switching actually off
    }     
}

Loki440:
Been trying to wrap my head around it but, its now bloody from bashing it against the wall.

What im trying to do is grab a sensor value (analog input) once every (insert desired time here) but lets say 10 mins and if that sensor value is >= an arbitrary value, then it causes a second time function to happen such as a relay turning on for an arbitrary set of time, say 30secs, and turning back off after that time, but having it all run again when that 10mins is up.

Hi! I'm loop()!
Is that gosh-darned relay on? It is? Shoot - has it been on for 30 sec? It has? Well, I better turn it off then (and note that it's now off).
Ok. How long has it been since I last checked the sensor? Over ten minutes? Damn - I better check it then. Oh, and I should make a note of the time now. Woah! The reading is over arbitrary value! Better turn the relay on (and make a note a) that it's on, and b) the time I turned it on). But what if it's already on? Look - that will never happen: I only leave it on for 30 seconds, anyway.

Ok! All done! (until the next cycle).