latching ELSE

Hi All,
I want to use a library by Marvin Rogers to control 4 shutters. Shutters
In the end I want to control the shutters based on weather conditions, time, sunlight AND manual control. The actual buttons are typical on-OFF-on type (non-latching with center position). The API gives one option to move the shutters (shutters.setLevel) and one option to stop the movement (shutters.stop).
I tried an IF, ELSE IF, ELSE construction for reading the switchstate(s) but this creates a problem when adding another conditional statement. Pseudeo code is there to explain where the problem occurs.

  int switchState1 = digitalRead(switchPin1); //UP
  int switchState2 = digitalRead(switchPin2); //DOWN
  
  if (switchState1 == LOW) {
    shutters1.setLevel(0);      //move shutter down to predefined level
  }
  else if (switchState2 == LOW) {
    shutters1.setLevel(100);    //move shutter up to predefined level
  }
  else {
    shutters1.stop();  //stop shutter moving regardless
  }

  if light=none {move shutter all the way down} //pseudo code!

When I don't set the switch up or down, ELSE is true and since it's in the loop it keeps telling the API to stop the shutter. If I use a separate statement to lower the shutter based on -let's say- time, it will only move one notch until the loop bumps on that ELSE again. The relais goes on/off constantly. In a way, the ELSE is latching witch is fine when using a pushbutton. If you release the button, the shutter stops. Without the ELSE it keeps running to the predefined setLevel and there's no way to manually stop it at any random level.

This lib stores the shutters position in EEPROM and moves the shutters from 0>100%. It keeps track where the shutters are and performs an initialization. To take advantage of this, all movement instructions must go through the API. How do I solve this? My mind is becoming a blur...

It sounds like you need to detect when a button becomes pressed rather than when it is pressed. Take a look at the StateChangeDetection example in the IDE

Yes, this is a common confusion - normally you want to respond to events, not to steady states,
as you want an action to happen once upon the change, not repeatedly.

This means you need to detect changes in the result of digitalRead(), which requires storing
the last value you read and comparing the current value to it.

Debouncing is also required for mechanical switch inputs, you'll find various ways to do this if
you search, including libraries, many of which will do state change detection for you.

MarkT:
Debouncing is also required for mechanical switch inputs, you'll find various ways to do this if
you search, including libraries, many of which will do state change detection for you.

The standard examples that ship with the IDE also include a debounced switch example.

You were so right! :slight_smile:

Stupid me! :confused:

thx!

ps one more question: delay is blocking code. Is there a way to debounce without delay? Maybe capacitors on the switches?

You could do hardware debounce, but it’s not really worth it. A tiny delay is enough to do the job and you’re not likely to notice it.

Is there a way to debounce without delay?

The Bounce2 library is millis()-based if I'm not mistaken.

Is there a way to debounce without delay?

Even the Debounce example in the IDE manages it without delay()

I tried to do the trick without debounce delay, but it needs as little as 2ms to work properly for the switches I use right now. Any delay() causes an tracking off-set. However, 2ms is relatively nothing AND the shutters get calibrated every time they reach full up or down position.
Got it working and all is fine (for) now.
I'll have a look at a millis() based debouncer.
Thx!

RogMoe:
I tried to do the trick without debounce delay, but it needs as little as 2ms to work properly for the switches I use right now. Any delay() causes an tracking off-set. However, 2ms is relatively nothing AND the shutters get calibrated every time they reach full up or down position.
Got it working and all is fine (for) now.
I'll have a look at a millis() based debouncer.
Thx!

What is "tracking offset"?

Moving the shutters is tracked by time vs percentage. If the clock stops ticking...

RogMoe:
Moving the shutters is tracked by time vs percentage. If the clock stops ticking...

Shouldn't matter. You only need a delay when you detect a button push, then command the shutters.

true, that's what I thought too.
thx