C++ code to detect an event

Hi :)

I need sample code to do something to detect event for an switch on/off .

when i press the button and keep hold on we can detect that

may this image clear my request

You can use external interrupt for button change. You can find the very good implementation of button change interrupt in the OBDuino skecth. Link is http://code.google.com/p/opengauge/source/browse/#svn/trunk/obduino

You don't need an interrupt for this. You need to connect the switch properly, though.

A switch is either on or off, so it should be connected to a digital pin, not an analog pin.

The state of the switch will be unpredictable, unless you use a pull-up or pull-down resistor. The Arduino has built-in pull-up resistors that you can use, if you use a digital pin.

The pin mode needs to be set to input, using the pinMode command. The pull-up resistor can be activated using digitalWrite(pin, HIGH). While it seems strange to write to an output pin, that is how the pull-up resistor is enabled.

Then, use digitalRead to read the state of the switch (LOW = pressed, HIGH = released).

Keep track of the previous state, so you can tell whether the state changed from pressed to released, or from released to pressed, if that matters.

Paul of course meant to type:

While it seems strange to write to an output input pin, that is how the pull-up resistor is enabled.

Paul of course meant to type:

Yes, he did.

Keep track of the previous state, so you can tell whether the state changed from pressed to released, or from released to pressed, if that matters.

ya this what i need , thank you for help dear & all

can i have very simple example for this Scenario to use pull-up switch with digital pin ?

i need it really because i'm beginner with Arduino

can i have very simple example for this Scenario to use pull-up switch with digital pin ?

const int switchPin = 4;

void setup()
{
  pinMode(switchPin, INPUT); // Set mode to IN
  digitalWrite(switchPin, HIGH); // Turn on pull-up resistor
}

void loop()
{
  if(digitalRead(switchPin) == LOW)
  {
    // Switch is pressed
  }
  else
  {
    // Switch is released
  }
}

Doesn't get much simpler than that.

#include <Button.h>

Button button = Button(4,PULLUP);
 
void setup(){
  //your setup
}
 
void loop(){
  if(button.isPressed()){
    //something if pressed
  }else{
    //something if pressed
  }
}

Maybe not much simpler, but I think it’s a little simpler.

Maybe not much simpler, but I think it's a little simpler.

And handles debouncing, too.

Which would be the next thing that needed to be explained.

But your else block does the same thing as the if block, so I'm not certain that it is clear what is going on. ;)

Dear PaulS :)

Thank you very much , yes it's work

i have you used for my House Door Lock , So any body open the Door i will get indication LED or i can connect with a Bell like security alarm

I have Project for Home Automation , so may i will get back to you for new ideas

Thank you dear