How to configure interrupt button

i want to build a simple program that do this:

when i push button led is on when i release the button led is off

Try an ‘if’ statement in your loop, combined with a digital read. Should work.

1 Like

can u give me example of digital read please

I could. But seeing as this is the most basic thing you can do, I would HIGHLY reccomend following/reading some tutorials on arduino for example (they are very much alike). If you don’t manage this, there’s an increased risk of damaging things if you mess up, which would be a shame.
I don’t mean to be harsh or anything, but be sure to read op on things, and start of easy, for your own good.
Examples of all the functions can be found on the documentation pages. docs.spark.io at the “firmware” section, including the digital read function.

Don’t hesitate to ask if you feel like you need help, but definitely read up on some or the basic arduino stuff. It’ll make thing a lot easier for you!

Best of luck!

3 Likes

Like @Moors7 mentioned, doing that is fairly simple:

  1. Within setup() attach your interrupt function to FALLING or RISING event. This depends on how you design your circuit.

  2. Within your interrupt function, do only simple things as fast as possible, like switching a boolean variable from false to true (like turning on and off).

  3. Write another function with a condition based on that variable to turn the LED on/off and call that from inside loop()

This forum is pretty helpful and quick to respond, but it’s helpful to read up on simple arduino docs and paste your code here when you have hit a problem.

2 Likes

That’s the neat way of doing it.

The “hello world”, stupidly simple, version would be something along the lines of:

if (digitalRead(pin) == HIGH){
	digitalWrite(led, HIGH);
}
else{
	digitalWrite(led, LOW);
}

Which is the equivalent of:

if (pushed){
	LED on
}
else{
	LED off
}

That certainly isn’t the preferred way of doing it, but it should somewhat work. It’ll at least teach you the workings of the Read/Write functions.