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;
}
}