Single Action Per State Change

Hello all,

I'm having trouble with the recommended use of state change detection, as the example code caters to using a push button. For my project I need the Arduino to act upon the changing state of a physical toggle switch, as such:

if togglePin becomes HIGH, blink the LED
if togglePin becomes LOW, blink the LED

If anybody would be kind to offer suggestions, it'd be greatly appreciated.

You forgot to post your code.

After wiring up the switch, you can use the Arduino IDE to write code to detect the change of state of the toggle. a variable can be created to store the current state into, I'd call the variable bool bPast = false.

In setup(), after setting up the pin the switch is connected on as an input pin, read the current state of the switch, and update bPast with the current state of the switch.

In loop() the state of the toggle switch can be checked and compared to the previously stored state in bPast and, if the 2 don't match, change bPast to match and do the LED blinky-winky thingy.

You'll want to understand how to properly wire a switch to the Arduino. I'd use a bit of a hardware debounce circuit. You can find how to hardware debounce a switch through a internet search. Try using the words " hardware debounce toggle switch" .

Then you'll want to learn about setting up a gpio pin, reading the gpio pin methods can be found here: Arduino Reference - Arduino Reference the section labeled "Digital".

Now get in there and do your research, wire up that switch and write some code. And if you have problems. Remember. Post your well formatted best attempt code in code tags, an image of the setup and a schematic. Note Fritz thingies are not schematics but can be of some help.

Good luck.

You will need to define a "blink". Actually, the code you need does less than the pushbutton examples, because it doesn't need to screen out button releases. So just remove the lines of code from the button sketch that performs that function.

You didn't say whether you needed a different action for button press and release...

push button

if togglePin becomes HIGH, blink the LED
if togglePin becomes LOW, blink the LED

if it is really a (non latching) push button, just combine the two examples

BlinkWithoutDelay
and
StateChangeDetection

Sorry if I wasn't very clear with the desired function but thankyou for the brilliant feedback.

There will be essentially 2 operating modes, and a rocking switch on the project to select the mode.
However, the Arduino must be reset each time to prevent code variables interfering with one another. Therefor the aim is to trigger a reset each time the physical rocker switch is flipped (in any direction). I currently have a small relay to open a path to send the reset pin to ground, however have had difficulty triggering it each time the rocker switch gets flipped.

I'm currently experimenting with the above methods.

I would be very very surprised if you need to resort to a hardware reset when switching between two modes.

But then, I don't know what this means:

prevent code variables interfering with one another.

read the switch state in setup and store the value in a global variable

switchStateStart = digitalRead(yourPin);

during loop check if the switch was changed

if (switchStateStart != digitalRead(yourPin))
{
// do your reset
}

CHARH:
I'm currently experimenting with the above methods.

Starting to sound like an x-y problem.