Go Down

Topic: 4 timed relays (Read 1 time) previous topic - next topic

3oh6

Hello all,

I'm fairly new to all of this, and I'm finding it quite a steep learning curve. I have been trying something for a little while and I'm running out of time before I need to use what I'm trying to do.

I'm wondering if (in exchange for some beer tokens ;) ) someone would be able to put together a little sketch for me. I have 4 momentary buttons that I need to control 4 relays (all independent). The thing is, the relays must be timed (the longest being 2 minutes), and I need to make it that a second button push de-energises the relay (regardless of time remaining on the countdown).

I'm aware that the best way is to learn for myself (which I am trying to do), but I need this within the next few days. So if anyone can help, it would be very very much appreciated.

Thanks so much :)

Bainesbunch

Do you want to be able to activate/deactivate a relay whilst another is is timing out ?


This needs to have non blocking timers for the relays.



Cheers Pete.
EmbeddedAT .. From Concept to Prototype to Production
Electronics and firmware design and project mentoring
I do answer personal requests for help when accompanied with a PayPal receipt ;)

3oh6

Thanks for your reply Pete. Yes that's correct, is it possible to achieve this with a single Arduino?

Bainesbunch

Sure you can. the simplest way to do it is to have an integer counter and Boolean state variable for each relay.

When you press the button associated with that relay you load its counter with the timer value for that button/relay but only if the state variable says it is off. If the state say it is on the you set the counter to 1.

Then during the loop method you can ( if they are  > 0)  decrement the counters for all the relays, if any of them reach  0 then switch that relay off.

That is it in simple terms. You will need to make sure that you are decrementing the counters at known interval so that the value of the initial counter can be calculated to give you the time that you are wanting.

Cheers Pete.
EmbeddedAT .. From Concept to Prototype to Production
Electronics and firmware design and project mentoring
I do answer personal requests for help when accompanied with a PayPal receipt ;)

3oh6

Ah OK, thanks for your help :) I think me trying to code something to include what you just mentioned is a little beyond me at the moment. Would it be something you'd be interested in writing for me? If so, what would you want for it?

Bainesbunch

really this is not that difficult
EmbeddedAT .. From Concept to Prototype to Production
Electronics and firmware design and project mentoring
I do answer personal requests for help when accompanied with a PayPal receipt ;)

3oh6

Subjectively it may not be, but people learn new things at different rates. By the time I've completely understood what you meant, how to use it etc., it'll be a little late. I really need this by tomorrow or Tuesday and to be honest, I don't believe I'll get it done in time on my own.

If it's not that difficult, then surely easy money? ;)

larryd

#7
Feb 28, 2016, 10:05 pm Last Edit: Feb 28, 2016, 10:05 pm by LarryD
I am sure you could take this example and come up with your sketch in 1/2 hour.

Code: [Select]
//Blink without Delay skeleton example using a structure.
//LarryD

//======================================================================
struct timer
{
  //lastMillis = the time this "timer" was (re)started
  //waitMillis = delay time (mS) we are looking for. You can use math 60*60*1000 for 1 hour
  //restart    = do we start "this timer" again and again 
  //enableFlag = is "this timer" enabled/allowed to be accessed
  //**********************
  //For each timer object you need:
  //Example:
  //   timer myTimer = //give the timer a name "myTimer"
  //   {
  //     0, 200UL, true, true  //lastMillis, waitMillis, restart, enableFlag
  //   };
  // You have access to:
  // myTimer.lastMillis, myTimer.waitMillis, myTimer.restart, myTimer.enableFlag, myTimer.CheckTime()
  //**********************

  unsigned long lastMillis;
  unsigned long waitMillis;
  bool          restart;
  bool          enableFlag;
  bool CheckTime() //Delay time expired function "CheckTime()"
  {
    //is the time up for this task?
    if (enableFlag && millis() - lastMillis >= waitMillis)
    //Note: if delays of < 2 millis are needed use micros() and adjust waitMillis as needed
    {
      //should this start again?
      if(restart)
      {
        //get ready for the next iteration
        lastMillis += waitMillis; 
      }
      //time was reached
      return true;
    }
    //time was not reached
    return false;
  } //END of CheckTime()

}; //END of structure timer
//======================================================================


//**********************************************************************
//Let's create 6 timer objects and initialize them in this sketch
//**********************************************************************
timer pin13 = //create timer pin13
{
  0, 200UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************
timer pin12 = //create timer pin12
{
  0, 3*1000UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************
timer pin11 = //create timer pin11
{
  0, 10*1000UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************
timer pin10 = //create timer pin10
{
  0, 6*1000UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************
timer Toggle10 = //create timer Toggle10
{
  0, 50UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************
timer checkSwitches = //create timer checkSwitches
{
  0, 50UL, true, true //lastMillis, waitMillis, restart, enableFlag
};
//***************************

byte lastMySwitchState = 1; //for mySwitch on Pin 2
byte counter           = 0;

const byte Pin13 = 13;
const byte Pin12 = 12;
const byte Pin11 = 11;
const byte Pin10 = 10;
const byte Pin9  =  9;

const byte mySwitch = 2;

//**********************************************************************

void setup()
{
  Serial.begin(9600);

  pinMode(Pin13,OUTPUT);
  pinMode(Pin12,OUTPUT);
  pinMode(Pin11,OUTPUT);
  pinMode(Pin10,OUTPUT);
  pinMode(Pin9, OUTPUT);

  digitalWrite(Pin13,LOW);
  digitalWrite(Pin12,LOW);
  digitalWrite(Pin11,LOW);
  digitalWrite(Pin10,LOW);
  digitalWrite(Pin9, LOW);

  pinMode(mySwitch,INPUT_PULLUP);


} //  >>>>>>>>>>>>>> E N D  O F  s e t u p ( ) <<<<<<<<<<<<<<<<<


void loop()
{
  //Below are examples demonstrating different timing situations

  //***************************
  //example 1    Toggle Pin13 every 200ms
  if (pin13.CheckTime())
  {
    //Toggle Pin13
    digitalWrite(Pin13,!digitalRead(Pin13));

    //if you only want this section of code to happen once
    //uncomment the next line
    //pin13.enableFlag = false;
  }

  //***************************
  //example 2    After 3 seconds, Pin12 goes and stays HIGH
  if (pin12.CheckTime())
  {
    //Make Pin12 HIGH now
    digitalWrite(Pin12,HIGH);
    //disable timing section of code
    pin12.enableFlag = false;
  }

  //***************************
  //example 3    Pin11 is HIGH for 10 seconds, then goes and stays LOW
  if (pin11.enableFlag && !pin11.CheckTime())
  {
    digitalWrite(Pin11,HIGH);
  }
  //10 seconds is now up now, leave the Pin11 LOW
  else
  {
    digitalWrite(Pin11,LOW);
    //disable timing section of code
    pin11.enableFlag = false;
  }

  //***************************
  //example 4    For 6 seconds, toggle Pin10
  if (pin10.enableFlag && !pin10.CheckTime())
  {
    //example 5  Toggling Pin10 every 50mS
    if(Toggle10.CheckTime())
    { 
      //toggle Pin10
      digitalWrite(Pin10,!digitalRead(Pin10));   
    }
  }
  //6 seconds is now up, toggling is stopped
  else
  {
    digitalWrite(Pin10,LOW);
    //disable timing section of code
    pin10.enableFlag = false;
  }

  //***************************
  //example 6    Is it time to check the switches?
  if (checkSwitches.CheckTime())
  {
    //time to read the switches
    Switches();     
  }

  //**********************************
  //Put other non-blocking stuff here
  //**********************************

} //  >>>>>>>>>>>>>> E N D  O F  l o o p ( ) <<<<<<<<<<<<<<<<<


//======================================================================
//                      F U N C T I O N S
//======================================================================


//**********************************************************************
//switches are checked every checkSwitches.waitMillis milli seconds
//no minimum switch press time is validated with this code (i.e. No glitch filter)
void Switches() 
{
  boolean thisState; //re-usable for all the switches     

  //*************************** mySwitch Pin 2 code 
  //check if this switch has changed state
  thisState = digitalRead(mySwitch);
  if (thisState != lastMySwitchState)
  { 
    //update the switch state
    lastMySwitchState = thisState; 

    //This switch position has changed, let's do some stuff

    //"HIGH condition code"
    //switch goes from LOW to HIGH
    if(thisState == HIGH)       
    {
      //example: LED on Pin9 is Push ON, Push OFF
      digitalWrite(Pin9,!digitalRead(Pin9));
    }

    //"LOW condition code"
    //switch goes from HIGH to LOW
    else                         
    {
      //example: display the current switch push count
      Serial.println(++counter);     
    }
  } //END of mySwitch Pin 2 code

  //****************************************** 
  //similar code for other switches goes here
  //****************************************** 

} //END of Switches()


//======================================================================
//                        E N D  O F  C O D E
//======================================================================


No technical PMs.
The last thing you did is where you should start looking.

3oh6

Thanks for your reply and I really appreciate your help. I've been trying for hours and hours, and it's a no go. I'm just not understanding it all well enough yet, and all I can do is chop your bits of code about, but I get constant errors.

Would someone be able to write me a sketch to do as described in my first post at all?

Thanks again.

cedarlakeinstruments

I sent you a PM in case you still need to get this done quickly.
Electronics and firmware/software design and assistance. No project too small

Go Up