Go Down

Topic: Control a relay with arduino nano (Read 2115 times) previous topic - next topic

Zonze

I want to control (with adruino nano) a relay module to turn on and delayed off (5-7 sec), triggered by a simple button. When push the button( even for 1 or 50 seconds) relay stays on and after release button, relay goes off in 5-7 sec.I wanted to add a 200 ms debounce and a delay of 600 ms...in fact  i want something like that: if button is pressed for less than 600 ms, nothing will happen. Can you help me , please? i'm a really noob on this one. Thank you in advance.
This is what i realised.....it is completly mess but somehow it is working  :o  The only problem is after i release button, i have different delay times


float pressLength_milliSeconds = 0;
int pinButton = 8;
int Relay = 2;
int stateRelay = LOW;
int stateButton;
int previous = LOW;
long time = 7000;
long debounce = 200;
 
void setup() {
  pinMode(pinButton, INPUT);
  pinMode(Relay, OUTPUT);
}
 
void loop(){
while (digitalRead(pinButton) == LOW )
 
    delay(600);   
 
{  stateButton = digitalRead(pinButton); 
  if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
    if(stateRelay == HIGH){
      stateRelay = LOW;
    } else {
       stateRelay = HIGH;
    }
    time = millis();
  }
  digitalWrite(Relay, stateRelay);
  previous == stateButton;
}
pressLength_milliSeconds = 0; }

PaulS

The state change detection example shows how to determine that a switch has become pressed, or has become released.

Knowing that a switch has become pressed would let you record when that change happened (using millis()).

Knowing when the switch became pressed, and what time it is now, you can determine how long the switch has been pressed. If it has been pressed long enough, you can take some action AND record when that action happened (like turning a relay on).

Knowing when the action happened, and what time it is now, you can determine if it is necessary to take some other action (like turning a relay off).

If there is some part of this that seems difficult, post the code that shows that you have taken a stab at it, and describe what problems you are having.
The art of getting good answers lies in asking good questions.

cedarlakeinstruments

Not tested, but should work. I think I understood what you were after :-)

Code: [Select]

//... define I/O here

void setup()
{
  pinMode(pinButton, INPUT_PULLUP);
  pinMode(Relay, OUTPUT);
}
 
void loop()
{
  unsigned long start= millis();
  bool pretrigger = false;

  // Hold while button depressed
  while(digitalRead(pinButton) == LOW)
  {
    if (millis() > (unsigned long)(start + 600))
    {
         pretrigger = true;
    }
  }
  // Pushbutton released
  if (pretrigger == true)
  {
       // Turn Relay on
       digitalWrite(RELAY, HIGH);
       // Delay 7 seconds
       delay(7000);
       // Turn off
       digitalWrite(RELAY, LOW);
  }
}
Electronics and firmware/software design and assistance. No project too small

Zonze

thank both of you very much for your help.
@PaulS - good logic, but i don't know how to write it.
@cedarlakeinstruments- it is working partially...the relay functions are inverted ( relay stay on and when contact is made, relay goes off) and from the moment when contact occurs, after 7 seconds relay goes on again, even if contact is continous.

cedarlakeinstruments

Change the HIGH  to LOW and vice versa in the digitalOutput statements. I forgot some of those Arduino relay boards use inverted logic.
Electronics and firmware/software design and assistance. No project too small

Zonze

I did it allready and it is ok. But still remain one problem. If  contact is on for 10 seconds, relay stops after 7 sec... i wanted to work like this: push button (contact on, relay on);  keep contact on as long as I want( 1,3,8,23... seconds etc), relay still on ; release buton - contact off, relay stays on for 7 seconds more and then relay goes off.
This is the hardest part.

from your sketch i removed this line:
if (millis() > (unsigned long)(start + 600)) 

and replaced with this one:
delay(600);
 
Now if i push button and keep it pushed, relay stay continous on and when i release button, delay appears but is variable.

Anyway thank you again for your effort and sorry for my english .

Zonze

And schematics

cedarlakeinstruments

Code: [Select]

//... define I/O here

void setup()
{
  pinMode(pinButton, INPUT_PULLUP);
  pinMode(Relay, OUTPUT);
}
 
void loop()
{
  unsigned long start= millis();
  bool pretrigger = false;

  // Hold while button depressed
  while(digitalRead(pinButton) == LOW)
  {
    // Relay on
    digitalWrite(RELAY, LOW);
    if (millis() > (unsigned long)(start + 600))
    {
         pretrigger = true;
    }
  }
  // Pushbutton released
  if (pretrigger == true)
  {
       // Turn Relay on
       digitalWrite(RELAY, LOW);
       // Delay 7 seconds
       delay(7000);
       // Turn off
       digitalWrite(RELAY, HIGH);
  }
  else
  {
     // Off immediately
     digitalWrite(RELAY, HIGH);
  }
}
Electronics and firmware/software design and assistance. No project too small

Zonze

YES !!! it is working !!!
After I modifyed "while(digitalRead(pinButton) == LOW)" to HIGH.
Thank you

Go Up