When a microswitch pressed show a record and when it unperesed show a record

Hi there I wanted to make a microswitch when it pressed print it pressed on the serial monitor and when it unpressed print another "Microswitch Is Off" but it didn't work. below is my code how to solve it

#include <Wire.h>
#include <ds3231.h>

#define Switch_1 2
#define Switch_2 3

int pressSwitch = 0;
int pressSwitch_2 = 0;

struct ts t;
 
void setup() {
  Serial.begin(9600);
  Wire.begin();
  DS3231_init(DS3231_CONTROL_INTCN);
  t.hour=12;
  t.min=30;
  t.sec=0;
  t.mday=25;
  t.mon=12;
  t.year=2019;
 
  DS3231_set(t);
}
 
void loop() {
  pinMode(Switch_1,INPUT);
  pinMode(Switch_2,INPUT);
  pressSwitch = digitalRead(Switch_1);
  pressSwitch_2 = digitalRead(Switch_2);
  if(pressSwitch == HIGH)
  {    
    Serial.println("Switch Pressed!");
    DS3231_get(&t);
    Serial.print("Date : ");
    Serial.print(t.mday);
    Serial.print("/");
    Serial.print(t.mon);
    Serial.print("/");
    Serial.print(t.year);
    Serial.print("\t Hour : ");
    Serial.print(t.hour);
    Serial.print(":");
    Serial.print(t.min);
    Serial.print(".");
    Serial.println(t.sec);
    Serial.println(analogRead(0));
    if(pressSwitch == LOW){
      Serial.println("Switch Off!");
      DS3231_get(&t);
      Serial.print("Date : ");
      Serial.print(t.mday);
      Serial.print("/");
      Serial.print(t.mon);
      Serial.print("/");
      Serial.print(t.year);
      Serial.print("\t Hour : ");
      Serial.print(t.hour);
      Serial.print(":");
      Serial.print(t.min);
      Serial.print(".");
      Serial.println(t.sec);
      Serial.println(analogRead(0));
    }
  }
  if(pressSwitch_2== HIGH)
  {    
    Serial.println("Switch Pressed!");
    DS3231_get(&t);
    Serial.print("Date : ");
    Serial.print(t.mday);
    Serial.print("/");
    Serial.print(t.mon);
    Serial.print("/");
    Serial.print(t.year);
    Serial.print("\t Hour : ");
    Serial.print(t.hour);
    Serial.print(":");
    Serial.print(t.min);
    Serial.print(".");
    Serial.println(t.sec);
    Serial.println(analogRead(0));
    if(pressSwitch_2 == LOW){      
      Serial.println("Switch Off!");
      DS3231_get(&t);
      Serial.print("Date : ");
      Serial.print(t.mday);
      Serial.print("/");
      Serial.print(t.mon);
      Serial.print("/");
      Serial.print(t.year);
      Serial.print("\t Hour : ");
      Serial.print(t.hour);
      Serial.print(":");
      Serial.print(t.min);
      Serial.print(".");
      Serial.println(t.sec);
      Serial.println(analogRead(0));
    }
  }
  delay(100);
}


Mailtrack	Sender notified by
Mailtrack 05/30/21, 02:32:29 AM	

i believe you need to detect a change in state, not whether its HIGH or LOW as you are doing which will repeat some action.

when you detect a change by capturing and comparing the previous to the current state, check if HIGH or LOW to determine if pressed or released

can you put your code as an answer?

The IDE has a state change detection code example

Apart from detecting state change, there’s a fundamental flaw in. testing the button pressed or not pressed…
It can’t be undressed when it’s already pressed.

… and This block of code

Serial.print("Date : ");
    Serial.print(t.mday);
    Serial.print("/");
    Serial.print(t.mon);
    Serial.print("/");
    Serial.print(t.year);
    Serial.print("\t Hour : ");
    Serial.print(t.hour);
    Serial.print(":");
    Serial.print(t.min);
    Serial.print(".");
    Serial.println(t.sec);
    Serial.println(analogRead(0));

Could be written once, and put in a function(), called whenever it’s needed..

Reading the DS3231 could be moved out, and either read once, or put into the above function, so it’s only needed at one place in the code.

There may be more, but those stood out.

void
loop ()
{
    static byte butState = Off;
           byte but      = digitalRead (Button);

    if (butState != but)  {
        butState = but;

        if (On == but)  {
            digitalWrite (LED, ! digitalRead (LED));

            const char *s = digitalRead (LED) ? "Off" : "On";
            Serial.println (s);
        }

        delay (10);     // debounce
    }
}