0

I am using an arduino mega. I'm trying to use interrupts in one of my programs. I have 8 pins that I will need to make separate interrupts for.

The pins are 40 - 47. These pins are attached to limit switches, so when the limit switch is pressed or not it'll indicate that via digitalRead().

#define switch_1 43     // Pin 43 is connected to Switch 1 (1: X+)
#define switch_2 42     // Pin 42 is connected to Switch 2 (1: X-)
#define switch_3 41     // Pin 41 is connected to Switch 3 (1: Y+)
#define switch_4 40     // Pin 40 is connected to Switch 4 (1: Y-)
#define switch_5 46     // Pin 46 is connected to Switch 5 (2: FR)
#define switch_6 47     // Pin 47 is connected to Switch 6 (2: BL)
#define switch_7 45     // Pin 45 is connected to Switch 7 (2: BR)
#define switch_8 44     // Pin 44 is connected to Switch 8 (2: FL)
int interruptVar = 5;

In my set up function I have:

attachInterrupt(digitalPinToInterrupt(switch_6), tightenStrings1, CHANGE); 

Then I have:

void tightenStrings1()
{
  interruptVar = 9;

}

When I change the status of the limit switches, by pressing them or unpressing them, this function never activates. The variable interruptVar is still 5.

What am I doing wrong?

Also, Serial isn't the problem in this case. I understand now not to use it though, but still doesn't solve the problem. Additionally I tried instead of CHANGE using RISING or FALLING and still no changes.

I have a feeling it's because the pins I'm using aren't technically interrupt pins...is there a way to make them usable as interrupt pins? I'm not allowed to change the pins being used, and using an interrupt is the best solution for the coding portion I'm working on.

  • Never use Serial in an interrupt. – Majenko Jul 1 '18 at 20:08
  • I changed it as you were commenting lol. But Serial isn't the issue..do you have any other suggestions why this doesn't work? – sGlow Jul 1 '18 at 20:15
  • your programming style is inconsistent .... you declare #define switch_6 47 and then you use attachInterrupt(digitalPinToInterrupt(47), tightenStrings1, CHANGE); ...... you should be using attachInterrupt(digitalPinToInterrupt(switch_6), tightenStrings1, CHANGE); ...... the pin name should really be something like switch_BL – jsotola Jul 1 '18 at 20:53
  • Ok....thank you. I'll fix that. But again, this doesn't point out a solution. Do you guys have any idea how to fix the actual problem? – sGlow Jul 1 '18 at 21:12
1

You are right when you say "technically they aren't interrupt pins".

The only pins you can use as interrupts are 2, 3, 18, 19, 20 and 21. There is no way of changing that. You can't magic more interrupts out of thin air.

However there is a different interrupt system that may help you out - the pin change interrupts. These are harder to work with since they are grouped into sets of pins that share the same interrupt, but there are far more pins with this kind of interrupt available. Note that you still have to be careful which pins you choose.

There are a couple of libraries available to help you configure the PCINT registers should you need it.

| improve this answer | |
0

Limit switch implies some mechanical thing is closing the switches, which hardly needs an interrupt as that will be pretty slow electronically. Simple polling is probably more than sufficient at the top of loop() or similar.

You can declare all the pins as INPUT_PULLUP, wire the switches to connect the pins to Gnd when pressed, then do digitalRead of each to determine its state, or put all 8 on one port and do a PORTx read

for example, and if the result is other than 0xFF, then one has been closed and you can act on it, or read it directly and act on like here for example:

switches = PORTL;
if (switches != 0xff){
switch(switches){
case 0b01111111:
//code for switch 7
break;
case 0b10111111:
// code for switch 6
break;
etc for 0b11011111, 11101111, 11110111, 111111011, 11111101, 111111110
}
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.