Is it possible to have 5 interrupts on Uno?

I am doing a project which requires me to use 5 press sensors to control 5 LEDs independently/separately, which means that when I press button 1, 2 and 3 at the same time, the LED 1, 2 and 3 will be on simultaneously and so on. So, to do that, I need to use 5 interrupts.

I am using Arduino Uno. My plan is that I will use PIN 2 and PIN 3 for two sensors (hardware interrupt), and use Timer1 for another sensor (software interrupt). By now I still have two interrupt to do, and I am trying to use PinChangeInterrupt for them.

However, I found that this does not work. All other interrupts are working, but not the PinChangeInterrupt. And I found that when I use PIN2 and PIN3 as my hardware interrupts, the PinChangeInterrupt will not work at all, but if I just leave PIN2 and PIN3 empty, the PinChangeInterrupt works, but the LED is very dim.

Could someone please help me? Or just tell me if it is really possible having 5 interrupts on Uno?

My code is here. I am now trying to play with 4 sensors.

#include "PinChangeInterrupt.h"
#include "TimerOne.h"
#define interrupt0 0
#define interrupt1 1

#define sensor1 11 //controlled by timer1
#define sensor2 2 //interrupt 0
#define sensor3 3 //interrupt 1
#define sensor4 4 //pinchangeinterrupt

#define LED1 5
#define LED2 6
#define LED3 7
#define LED4 8

volatile byte flag0, flag1, flag2, flag3;

void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);

  pinMode(sensor1, INPUT);
  pinMode(sensor2, INPUT);
  pinMode(sensor3, INPUT);
  pinMode(sensor4, INPUT);
  
  Serial.begin(9600);
  attachInterrupt(interrupt0,LED1ON,CHANGE);
  attachInterrupt(interrupt1,LED2ON,CHANGE);
  
  attachPinChangeInterrupt(sensor4,LED4ON,CHANGE);
  
  Timer1.initialize(100000);
  Timer1.pwm(sensor1, 512); 
  Timer1.attachInterrupt(LED3ON);

}

void loop() {
  if(flag0 == 1){
    Serial.println("11111111");
    Serial.print(sensor1);
    flag0 = 0;
  }
  if(flag1 == 1){
    Serial.println("22222222");
    Serial.print(sensor2);
    flag1 = 0;
  }
    if(flag2 == 1){
    Serial.println("33333333");
    Serial.print(sensor3);
    flag2 = 0;
  }
    if(flag3 == 1){
    Serial.println("44444444");
    Serial.print(sensor4);
    flag3 = 0;
  }  
}

void LED1ON(){
  if (digitalRead(sensor2) == LOW){
    digitalWrite(LED1, LOW);
  }
  if (digitalRead(sensor2) == HIGH){
    digitalWrite(LED1, HIGH);
    //digitalWrite(LED1, LOW);
    flag0 = 1;
  }
}
void LED2ON(){
  if (digitalRead(sensor3) == LOW){
    digitalWrite(LED2, LOW);
  }
  if (digitalRead(sensor3) == HIGH){
    digitalWrite(LED2, HIGH);
    //JdigitalWrite(LED2, LOW);
    flag1 = 1;
  }
}
void LED3ON(){
  if (digitalRead(sensor1) == LOW){
    digitalWrite(LED3, LOW);
  }
  if (digitalRead(sensor1) == HIGH){
    digitalWrite(LED3, HIGH);
    //JdigitalWrite(LED2, LOW);
    flag2 = 1;
  }
}
void LED4ON(){
  if (digitalRead(sensor4) == LOW){
    digitalWrite(LED4, LOW);
  }
  if (digitalRead(sensor4) == HIGH){
    digitalWrite(LED4, HIGH);
    //JdigitalWrite(LED2, LOW);
    flag3 = 1;
  }
}

Why isn’t it good enough to just scan the switches every 10ms and look for change in state?

Interrupts are best for fast (not switches) devices.

I actually thought about it, but the way I did it did not allow me to control the switches independently, which means that even I press multiple sensors at the same time, only one LED will be on. Probably I did not do it correctly.

Could you please give me a little bit more information about how to do it?

Would the LEDs going on a few 100 microseconds apart from one another be a problem?

Your eyes will definitely not see any lag between the LEDs coming on.

This should give you something to build on:

#define LED1 8
#define LED2 9
#define LED3 10
#define LED4 11

#define sensor1 2
#define sensor2 3
#define sensor3 4
#define sensor4 5

byte lastSensor1State;
byte lastSensor2State;
byte lastSensor3State;
byte lastSensor4State;

//Timer variables used
unsigned long currentMillis;
unsigned long pin13Millis;
unsigned long SwitchMillis;

//if these are not changed in the sketch, they can be const
unsigned long debounceMillis = 10UL;  //10ms
unsigned long ledOnTime      = 500UL; //500ms seconds

//**********************************************************************
void setup()
{
  Serial.begin(9600);

  pinMode(LED1, OUTPUT);
  digitalWrite(LED1, LOW);  //LED off

  pinMode(LED2, OUTPUT);
  digitalWrite(LED2, LOW);  //LED off

  pinMode(LED3, OUTPUT);
  digitalWrite(LED3, LOW);  //LED off

  pinMode(LED4, OUTPUT);
  digitalWrite(LED4, LOW);  //LED off

  digitalWrite(13, LOW);
  pinMode(13, OUTPUT);

  pinMode(sensor1, INPUT_PULLUP);
  pinMode(sensor2, INPUT_PULLUP);
  pinMode(sensor3, INPUT_PULLUP);
  pinMode(sensor4, INPUT_PULLUP);

} //END of setup()

//**********************************************************************
void loop()
{
  //save the current time
  currentMillis = millis();

  //*************************************
  //Heartbeat LED
  //Toggle LED on and off, helps show if there is blocking code
  if (currentMillis - pin13Millis >= ledOnTime)
  {
    pin13Millis = millis();             //re-initialize Timer
    digitalWrite(13, !digitalRead(13)); //toggle LED condition
  }

  //*************************************
  //is it time to check the switches?
  if (currentMillis - SwitchMillis >= debounceMillis)
  {
    //code here runs every debounceMillis ms
    SwitchMillis = millis(); //re-initilize Timer
    
    //go check the switches
    checkSwitches();
  }

} //END of loop()


//**********************************************************************
//                  c h e c k S w i t c h e s ( )
//
void checkSwitches()
{
  //reusable for all the switches
  boolean thisState;

  //check if this switch has changed state
  thisState = digitalRead(sensor1);

  if (thisState != lastSensor1State)
  {
    //update the switch state
    lastSensor1State = thisState;

    //this switch position has changed so do some stuff
    //"LOW condition code"
    //has switch gone from HIGH to LOW?
    if (thisState == LOW)
    {
      //Do some LOW switch stuff here
      digitalWrite(LED1, HIGH); //turn on LED
    }
    else
    {
      //Do some HIGH switch stuff here
      digitalWrite(LED1, LOW); //turn off LED
    }

  } //END of sensor1 code

  //**************************************
  //check if this switch has changed state
  thisState = digitalRead(sensor2);

  if (thisState != lastSensor2State)
  {
    //update the switch state
    lastSensor2State = thisState;

    //this switch position has changed so do some stuff
    //"LOW condition code"
    //has switch gone from HIGH to LOW?
    if (thisState == LOW)
    {
      //Do some LOW switch stuff here
      digitalWrite(LED2, HIGH); //turn on LED
    }
    else
    {
      //Do some HIGH switch stuff here
      digitalWrite(LED2, LOW); //turn off LED
    }

  } //END of sensor2 code

  //**************************************
  //check if this switch has changed state
  thisState = digitalRead(sensor3);

  if (thisState != lastSensor3State)
  {
    //update the switch state
    lastSensor3State = thisState;

    //this switch position has changed so do some stuff
    //"LOW condition code"
    //has switch gone from HIGH to LOW?
    if (thisState == LOW)
    {
      //Do some LOW switch stuff here
      digitalWrite(LED3, HIGH); //turn on LED
    }
    else
    {
      //Do some HIGH switch stuff here
      digitalWrite(LED3, LOW); //turn off LED
    }

  } //END of sensor3 code

  //**************************************
  //check if this switch has changed state
  thisState = digitalRead(sensor4);

  if (thisState != lastSensor4State)
  {
    //update the switch state
    lastSensor4State = thisState;

    //this switch position has changed so do some stuff
    //"LOW condition code"
    //has switch gone from HIGH to LOW?
    if (thisState == LOW)
    {
      //Do some LOW switch stuff here
      digitalWrite(LED4, HIGH); //turn on LED
    }
    else
    {
      //Do some HIGH switch stuff here
      digitalWrite(LED4, LOW); //turn off LED
    }
    
  } //END of sensor4 code
  
} //END of checkSwitches()

//**********************************************************************

Sry I was working on it and did not check the forum for a while.

Thank you very much for your help and I'll learn your code now.

And it is my bad that I did not mention that for this project, the LED needs to stay on when I press and hold the sensor.

larryd:
This should give you something to build on:

#define LED1 8

#define LED2 9
#define LED3 10
#define LED4 11

#define sensor1 2
#define sensor2 3
#define sensor3 4
#define sensor4 5

byte lastSensor1State;
byte lastSensor2State;
byte lastSensor3State;
byte lastSensor4State;

//Timer variables used
unsigned long currentMillis;
unsigned long pin13Millis;
unsigned long SwitchMillis;

//if these are not changed in the sketch, they can be const
unsigned long debounceMillis = 10UL;  //10ms
unsigned long ledOnTime      = 500UL; //500ms seconds

//**********************************************************************
void setup()
{
  Serial.begin(9600);

pinMode(LED1, OUTPUT);
  digitalWrite(LED1, LOW);  //LED off

pinMode(LED2, OUTPUT);
  digitalWrite(LED2, LOW);  //LED off

pinMode(LED3, OUTPUT);
  digitalWrite(LED3, LOW);  //LED off

pinMode(LED4, OUTPUT);
  digitalWrite(LED4, LOW);  //LED off

digitalWrite(13, LOW);
  pinMode(13, OUTPUT);

pinMode(sensor1, INPUT_PULLUP);
  pinMode(sensor2, INPUT_PULLUP);
  pinMode(sensor3, INPUT_PULLUP);
  pinMode(sensor4, INPUT_PULLUP);

} //END of setup()

//**********************************************************************
void loop()
{
  //save the current time
  currentMillis = millis();

//*************************************
  //Heartbeat LED
  //Toggle LED on and off, helps show if there is blocking code
  if (currentMillis - pin13Millis >= ledOnTime)
  {
    pin13Millis = millis();            //re-initialize Timer
    digitalWrite(13, !digitalRead(13)); //toggle LED condition
  }

//*************************************
  //is it time to check the switches?
  if (currentMillis - SwitchMillis >= debounceMillis)
  {
    //code here runs every debounceMillis ms
    SwitchMillis = millis(); //re-initilize Timer
   
    //go check the switches
    checkSwitches();
  }

} //END of loop()

//**********************************************************************
//                  c h e c k S w i t c h e s ( )
//
void checkSwitches()
{
  //reusable for all the switches
  boolean thisState;

//check if this switch has changed state
  thisState = digitalRead(sensor1);

if (thisState != lastSensor1State)
  {
    //update the switch state
    lastSensor1State = thisState;

//this switch position has changed so do some stuff
    //"LOW condition code"
    //has switch gone from HIGH to LOW?
    if (thisState == LOW)
    {
      //Do some LOW switch stuff here
      digitalWrite(LED1, HIGH); //turn on LED
    }
    else
    {
      //Do some HIGH switch stuff here
      digitalWrite(LED1, LOW); //turn off LED
    }

} //END of sensor1 code

//**************************************
  //check if this switch has changed state
  thisState = digitalRead(sensor2);

if (thisState != lastSensor2State)
  {
    //update the switch state
    lastSensor2State = thisState;

//this switch position has changed so do some stuff
    //"LOW condition code"
    //has switch gone from HIGH to LOW?
    if (thisState == LOW)
    {
      //Do some LOW switch stuff here
      digitalWrite(LED2, HIGH); //turn on LED
    }
    else
    {
      //Do some HIGH switch stuff here
      digitalWrite(LED2, LOW); //turn off LED
    }

} //END of sensor2 code

//**************************************
  //check if this switch has changed state
  thisState = digitalRead(sensor3);

if (thisState != lastSensor3State)
  {
    //update the switch state
    lastSensor3State = thisState;

//this switch position has changed so do some stuff
    //"LOW condition code"
    //has switch gone from HIGH to LOW?
    if (thisState == LOW)
    {
      //Do some LOW switch stuff here
      digitalWrite(LED3, HIGH); //turn on LED
    }
    else
    {
      //Do some HIGH switch stuff here
      digitalWrite(LED3, LOW); //turn off LED
    }

} //END of sensor3 code

//**************************************
  //check if this switch has changed state
  thisState = digitalRead(sensor4);

if (thisState != lastSensor4State)
  {
    //update the switch state
    lastSensor4State = thisState;

//this switch position has changed so do some stuff
    //"LOW condition code"
    //has switch gone from HIGH to LOW?
    if (thisState == LOW)
    {
      //Do some LOW switch stuff here
      digitalWrite(LED4, HIGH); //turn on LED
    }
    else
    {
      //Do some HIGH switch stuff here
      digitalWrite(LED4, LOW); //turn off LED
    }
   
  } //END of sensor4 code
 
} //END of checkSwitches()

//**********************************************************************

Oh man, I am almost gonna cry now. This perfectly works for my project, except for the on/off of LEDs, cuz it's kind of flipped (on is off, off is on). Ill figure it out. Thank you very much!!!!!!!!!!!!!!

Yes, depending on wiring things can be reversed.

larryd:
Yes, depending on wiring things can be reversed.

You are a life savior for me. Thank you very much for your time and help!

Thank you very much!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!