How To Prevent Relay From Staying On When Trigger Is Pressed Forever?

Hi all, Im currenly working on this little project using an ATTINY85 to control a relay by the press of the button. The relay is meant to stay on for the amount of time that you set with a potentiometer. At the moment it works with the code [ below ] You press the button, and the load stay on for the set amount of time and turns back off again. But if i press the button again after the intial press the timer restarts, and if i hold the button down, the load stays on for that time, an then starts the timer, essentially i can have the load on forever.

I cant seem to work out the logic to make sure it only triggers for the set amount of time, regardless of multiple press/ holding down.

#define relayPin 0
#define potPin  A1
#define triggerPin 1

const long maxTime = 10000;
const long minTime = 3000;

long duration;
int potValue;
long rememTime;
int relayState = 0;

void setup() {
  pinMode(relayPin, OUTPUT);
  pinMode(triggerPin, INPUT);
}

void loop() {

  potValue = analogRead(potPin)/10;

  duration = map(potValue, 0, 102, minTime, maxTime);
  
    if(digitalRead(triggerPin) == HIGH)
    {
     rememTime = millis();
     relayState = 1;
     controlRelay();
    }
   if((millis()- rememTime) > duration)
   {
    relayState = 0;
    controlRelay();
   }   
 delay(200); 
}

 void controlRelay() {
  if(relayState == 1) {
    digitalWrite(relayPin, HIGH);
     }else{
    digitalWrite(relayPin, LOW);
     }
  }

Welcome

You could simply check the value of relayState

if( relayState == 0 && digitalRead(triggerPin) == HIGH)
{
     rememTime = millis();
     relayState = 1;
     controlRelay();
}

I can't try it but tried @guix's solution looks like it would and it retriggers the relay every time it times out, if you were to keep you finger on the button.

@countessaflufforossa you should learn about state change detection, there is an example on offer in the IDE, or google

 Arduino state change detection

The idea is to react, or do anything, when the button becomes pressed, rather than because it is pressed.

So you only do something when the button goes from up to down.

Follow the example and figure out how to count button presses. Learning this end of things will def be no waste of time.

HTH

a7

2 Likes

Use delay() instead of millis() and your problem will be solved.

Best advice you will get from Alto. In programming languages, it is just called a "state machine". If you want to learn more, look that up for C++ examples.

This sounds helpfull thank you, ill give it a shoot and get back to you.

Making the "advice" that short for a newcomer IMHO is not "best" advice
this word state-machine is away from allday experience.

Modes of operation is IMHO easier to understand
So your device would need the following modes of operation
mode 1 relay off "waiting" for a button-press if button pressed change to mode 2
mode 2 button-press detected switch relay on and start timer based on potentiometer-position change to mode 3
mode 3 wait for relay ontime to finish if ontime is over switch relay off change to mode 4
mode 4 wait for button to be unpressed if unpressed change to mode 1

Now in programming instead of the word "mode" the word "state" is used
state 1 relay off "waiting" for a button-press if button pressed change to state 2
state 2 button-press detected switch relay on and start timer based on potentiometer-positionchange to state 3
state 3 wait for relay ontime to finish if ontime is over switch relay off change to state 4
state 4 wait for button to be unpressed if unpressed change to state 1

in case the button is released after a short time
mode 4 / state 4 simply detects button unpressed and changes to mode 1 / state 1

1 Like

That's your opinion and I disagree. Perhaps you misunderstood. I said that Alto gave the best advice and was expanding on what he shared. Perhaps English is not your native language and "state machine" is called something else? How long have you been programming? I think you meant "everyday" and not "allday" to mean "common everyday experience". Modes of operation" could mean anything and would not lead to a productive google search, where "state machine" is a standard term known since before I started programming in the 1970s and is still used in all the corporate environments I am a part of (I sell controls to programmers so deal with many professional environments in multiple programming languages other than just C++ ). So it IS part of "everyday experience". Would we call an "if-then statement" something else like "branch choice mode"?
Alto pointed Countessa to an example for "state change detection" that would show exactly what she needed and encourage her to learn. IMHO, you jumping in talking about modes only come right back to the word "state" unnecessarily confused things.

@fdecker
you are right everyday is the word to use in this case.

not nescessary to do a google-search as I explain it with the use-case of countessa.

On a hobby base 40 years.

Sure. But this fact does not explain it to newcomers.

So please explain the term SPS. You don't know it? Well it is a very very common term in automation. You should really know it. Same situation for you. And with a simple translation to a TLA (don't know the abbrev TLA? well a three letter acronym) You understand it and the confusion is over. SPS = german TLA for english PLC.

It is only part of the everyday experience of automation-experts and software-developpers = professionals
but not for beginners in programming.
I call this the beginner-difficulty-blindness of the experts

best regards Stefan

Well, we could go round and round, but I am certainly not blind to beginners since my job is relating to them. And I am a beginner every week in some other subject I need to learn, so I can relate pretty easily. The point I am making is that we speak a certain language in any discipline and whether you are a newbie or an expert, using standard terms is important, especially in this case where someone would need to search and find good results.

If we were talking about car repair, we would mention fuel injectors, alternator, and spark plug wires. We wouldn't call them "gas thingies", "spinning electrical motor" and "power cables". It is ok and even appropriate to mention a term and then define it, then we are all talking the same language. In Arduino-speak, they have the horrible convention (IMHO) of calling a program a "sketch" (supposedly to help newbies??), but if we are working with Arduinos, we need to use the accepted term.

The same goes for "subroutine", "function", "if-then statement", "loop", "case statement" etc. This holds true for any discipline whether it is car repair, math, music, or automation. Teach the correct terms up front and you don't have to re-learn them later when people look at you strange and ask, "what are you talking about?" or searching for "modes of operation" and getting results about cipher block encryption, networking, plumbing, airport operations, or anything but computer programming.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.