0
#define monitor_pin 14

void setup()
{
  pinMode(monitor_pin , INPUT);
}

void loop()
{
    unsigned long d1 = pulseIn(14, HIGH);
    if (d1>0){//trigger function}
}

I know there is a function pulseIn(), but it doesnt really solve my problem. I just want to trigger a function when voltage change from LOW to HIGH/HIGH to LOW. pulseIn() need to wait for the voltage to back to original level which in my case will exceed 3 minutes and cause timeout.

How can I detect a voltage change other than pulseIn()?

2
  • The length of the pulse is of no interest to you at all?
    – timemage
    Jan 5 at 15:01
  • 1
    @timemage yes, i dont need the pulse length at all.
    – Jess
    Jan 5 at 15:02

1 Answer 1

3

You can save the previous state of the pin and then trigger the function when the current state is different from the previous state:

#define monitor_pin 14
int previous_state;

void setup()
{
  pinMode(monitor_pin , INPUT);
  previous_state = digitalRead(monitor_pin); // Initialize previous state with initial reading of the pin
}

void loop()
{
  int current_state = digitalRead(monitor_pin);
  if(current_state != previous_state){
    // Execute your code here
    previous_state = current_state; // update previous state
  }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.