multiple push buttons multiple interrupts

so I am making a costume and I am a tv of sorts I have 6 different buttons I want to control in which when I push one it changes the display. The problem I am encountering is I have found multiple button stuff but none that in the drop of a hat I can change the display from a sad face to a happy face if someone wants a photo of me. Can someone help me?

I am a tv of sorts

Say what?

Can someone help me?

No, because you did not post your code or a schematic.

The odds that you need interrupts to deal with human-pressed switches are something less than 1 in 1000000000.

this is my first edition of the costumecostume 1.0 I am trying to modify something like this to use multiple buttons I hate having to cycle through the one button to select something and multiple buttons would help a ton. I am new at this my friend helped with the last one but he is no longer living.

multiple buttons would help a ton.

So, what is the problem? You have some code that does something. We need to see that code if we are to help you extend/enhance/modify it.

the problem is I have cant figure out how to write a multiple button code with multiple interrupts that work.
everything I have looked for doesnt seem to work. I do have this code two buttons work but thats 2 of 6 that I need to work

interrupt2 (1).ino (4.96 KB)

An interrupt is necessary when something needs to be handled RIGHT NOW, before data is lost. Pressing a button is NOT such a case.

What that code is doing is assuring that pressing a switch, while one of the blocking functions is running, is noted, so that something different can happen when the blocking function ends.

It is FAR better to write non-blocking code, so you can read the switches quite often and either queue the event for later handling or immediately change what you are doing.

We can help you change the code to non-blocking, but adding more interrupts is not going to happen unless you change to a Mega (with 5 external interrupt pins).

That would be fantastic I am going to my makerspace tonight to work on this and I could use all the help I can have since this is tribute to my friend.

void flasher() 
{

  digitalWrite(ledPin, HIGH);
  delay(500);
  digitalWrite(ledPin, LOW);
  delay(500);
  
}

This is blocking code. You could add some global variables:

unsigned long lastFlash = 0;
unsigned long flashTime = 500;
bool ledIsOn = false;

Then, flasher() could become:

void flasher()
{
   unsigned long now = millis();
   if(now - lastFlash >= flashTime)
   {
      lastFlash = now;

      ledIsOn = !ledIsOn;

      digitalWrite(ledPin, ledIsOn);
   }
}

loop() will still call flasher() over and over. But, most of the time, flasher() will do nothing. Once in a while, it will determine that the pin has been on, or off, for long enough, and change the state of the pin and record when the change is made.

You can see that heartbeat() does something very similar, except that it uses 4 different intervals. So, you add a beatState global variable, initialize to 0.

In heartbeat(), you would check the value of beatState. If it is 0, call setHeartRate() (instead of loop() calling setHeartRate()) and set beatState to 1.

If it is 1, and it is time to change states, change the state of the ledPin, record when that happened, and change beatState to 2.

If it is 2, and it is time to change states (notice that there needs to be a different variable used for the interval in this case), change the state of the ledPin, record when that happened, and change beatState to 3.

The same thing happens when the state is 3 and when it is 4, except that when the state changes from 4 it changes to 1.

Make the additions and change flasher() as I showed. Verify that the costume exhibits the same behavior.

When it does, make some attempt to change heartbeat using the above information. If you have problems, post your code (it was small enough to post without needing to attach it) and describe the problems.

Then, we can show you how to read the switches without interrupts, and how to add additional (non-blocking) behavior and switches to trigger the added behavior.

P.S. Sorry about the loss of your friend.

Thanks so much for the help

Have a look at the demo Several Things at a Time and the longer Planning and Implementing a Program

...R

I am just looking to separate the display functions into separate keys, I would love to be able to call each function from its own designated key press. Not just cycling through 1-6 from one single button press.
I am fooling with the code they told me to try now. Thanks for all the help guys it means a lot to me.

herobattousai:
I would love to be able to call each function from its own designated key press. Not just cycling through 1-6 from one single button press.

There is no problem using several buttons and deciding which function to call based on which button was pressed. There is no need to use interrupts just because you want to read the inputs from several buttons.

If it is in your mind that an interrupt triggered by a button goes straight to a particular function then that has no advantage compared to reading the states of all the buttons and then deciding which function to call.

...R