Using/understanding Interupts

Hi

I have a little setup that I would like to put into different states using a simple push button. I was hoping to use interupts, but I need to understand first.

  1. Can I use a button to trigger an interupt, how should this be wired?
  2. Can I have mulltiple states depending on the number of pushes to the button? I would need 3 different states + a sleep mode.

Kind regards J;-)

I was hoping to use interupts,

Why ?

No need for interrupts for what sounds like a state machine except perhaps for the sleep mode. What type of sleep mode ?

More details of the project please.

Why interupts.

I thought it would be the only way to wake my adruino from sleep.

What type of sleep, the type of sleep where I dosn’t use a lot of power. I need it to stop the loops it’s running and go to thw lowest possible power level. I know the ammount of mA to be saved is depending on my hardware. But many projects want’s some sort of sleep/powersave so I’m thinking it’s an option.

J;-)

I thought it would be the only way to wake my adruino from sleep.

Hence my comment to that effect but don't conflate the state machine principle with putting the Arduino to sleep.

UKHeliBob:
Why ?

No need for interrupts for what sounds like a state machine except perhaps for the sleep mode. What type of sleep mode ?

More details of the project please.

I have 4 states, I want a push button to change the state/function of the Arduino/program. I want to have them change from state to state when pushed.... And loop back to state1 after sleep.

State1 -> State2 ->State3 -> Sleep ->State1.....

Each state is a loop that does the current state repeatedly.

Your interrupt handler will change the state, but it can not possibly force the running code to become aware of that change.

You will need to revise the rest of your code to periodically check to see if the state had changed.

Since you have to revise most of the code, anyway, you might as well chomp on this here bullet, and write non-blocking code, which can poll the state of the switch often enough to not need interrupts.

The state change detection tutorial shows how to count button presses by detecting state transitions. Then use the buttonPushCounter to select the state. No interrupt required. I would change the wiring of the switch to be wired from ground and the switch input with the internal pullup enabled. Place a 0.1uf cap across the switch to debounce the switch. The switch input will be HIGH when not pressed and LOW when pressed. Change the logic in the example code to reflect an active low switch.