Program acting odd

Hello All,
Can anyone make me wiser.
I have an exercise for students to make the principle of indirect switching with a microcontroller.
It is a programme which needs to control a relay with an on button and off button. We notice with the code below, the programme works perfectly.
I didn't think this programme would work. When the on button is pushed, the buttonstate will be briefly low and consequently the relay will also tighten briefly to release immediately when the button is released. So it does not do this. Somehow it remembers its buttonstate and keeps it at low.
Another oddity in an older version formerly 2.0 it does behave this way and you have to add a flag.
What am I overlooking?

Thank you all.

const int D1 = 2;
const int D2 = 3;
const int relais = 6;

bool state_DK1 = 0;
bool state_DK2 = 0;

void setup() {
pinMode (D1,INPUT_PULLUP);
pinMode(D2, INPUT_PULLUP);
pinMode(relais, OUTPUT);
}

void loop() {
state_DK1 = digitalRead(D1);
state_DK2 = digitalRead(D2);

if(state_DK1 == LOW){
  digitalWrite (relais,HIGH);
}
if(state_DK2 ==LOW){
  digitalWrite(relais,LOW);
}
}

The fact that your loop is operating very very quickly in relation to how slow it takes a relay to move. So you can have several hundred times round the loop before you can perceive the relay has moved.

You code doesn't do anything when state_DK1 == HIGH so once it detects the 'ON' button press, it turns on the relay. When you release the button, nothing changes since state_DK1 will be HIGH and your code does not react to that state.

There is no code in your sketch that does that. The relay only switches off when the other button is pressed.

If both buttons are pressed at the same time (which your students will try), the relay will receive the ON and OFF signals hundreds of times per second (kind of a PWM signal). It may flicker, it may do nothing apparent.

Not likely me.

But I can recommend adding Serial.print() statements all over the place to see the value of variables and observe the flow through the code.

For something like this, placing a delay(250); in the loop() function cuts down the amount of printing, and makes it easier to observe what is really going on when, for example, two buttons are presst at the same time. Which your students will try. :wink:

Otherwise you flying blind.

HTH

a7

here is a version of your code with added serial printing for debugging.
Printing to the serial monitor at 115200 baud is quick but still needs some time.
This means without the serial printing the bouncing of mechanical buttons will have more effect.

The special debug-macro dbgc used here prints only once if the value of the variable has changed.

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);

#define dbgi(myFixedText, variableName,timeInterval) \
  { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  }

#define dbgc(myFixedText, variableName) \
  { \
    static long lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }

#define dbgcf(myFixedText, variableName) \
  { \
    static float lastState; \
    if ( lastState != variableName ){ \
      Serial.print( F(#myFixedText " "  #variableName" changed from ") ); \
      Serial.print(lastState); \
      Serial.print( F(" to ") ); \
      Serial.println(variableName); \
      lastState = variableName; \
    } \
  }
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *



const int D1 = 2;
const int D2 = 3;
const int relais = 6;

bool state_DK1 = 0;
bool state_DK2 = 0;

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  pinMode (D1, INPUT_PULLUP);
  pinMode(D2, INPUT_PULLUP);
  pinMode(relais, OUTPUT);
  Serial.println("press buttons to switch relais");
}

void loop() {
  state_DK1 = digitalRead(D1);
  state_DK2 = digitalRead(D2);

  dbgc("ToL", state_DK1);
  dbgc("ToL", state_DK2);
  
  if (state_DK1 == LOW) {
    digitalWrite (relais, HIGH);
  }
  
  if (state_DK2 == LOW) {
    digitalWrite(relais, LOW);
  }
}

best regards Stefan