I'm starting a new project that will be using a Teensy 4.0 or 4.1, but I haven't ordered it yet, so I'm prototyping some concepts on an ATmega328 based SparkFun RedBoard. I'm simply testing interrupt code from a limit switch I intend to use using the following example code from the Arduino reference site:
My problem is that it's very easy to get the LED out of sync with the switch by rapid switching, (flicking it with a finger), so my question is whether I'd see improvement with the Teensy. The reason I'm asking is because the particular limit switch I'm using has a resistor and capacitor which I presume are there as an RC circuit for debouncing the input, (I'm using this switch), and I'm wondering if that might be effecting things. It's also configured to be active low, taking a supply voltage and returning a separate signal line. If I remove the interrupt and simply poll the state of the switch I have zero issues of course, but this is going to be part of a more complex system so interrupts would be ideal. If it's not simply a microcontroller performance issue but something that might be common between this RedBoard and a Teensy then I need to do a rethink of my design. For example this particular aspect of the overall system could be isolated to a Teensy LC and I could use polling, but I'd rather just have one Teensy providing a complete solution.
A second question would be whether to use a pull-up resistor or not. The pin can be declared as INPUT_PULLUP, but it makes no difference in my testing. Since I'm supplying voltage to the switch which returns HIGH when the switch is open and LOW when closed I'm not sure what I need in the way of pull-up, pull-down or nothing. I always find this part confusing.
Code:
const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}
void loop() {
digitalWrite(ledPin, state);
}
void blink() {
state = !state;
}
My problem is that it's very easy to get the LED out of sync with the switch by rapid switching, (flicking it with a finger), so my question is whether I'd see improvement with the Teensy. The reason I'm asking is because the particular limit switch I'm using has a resistor and capacitor which I presume are there as an RC circuit for debouncing the input, (I'm using this switch), and I'm wondering if that might be effecting things. It's also configured to be active low, taking a supply voltage and returning a separate signal line. If I remove the interrupt and simply poll the state of the switch I have zero issues of course, but this is going to be part of a more complex system so interrupts would be ideal. If it's not simply a microcontroller performance issue but something that might be common between this RedBoard and a Teensy then I need to do a rethink of my design. For example this particular aspect of the overall system could be isolated to a Teensy LC and I could use polling, but I'd rather just have one Teensy providing a complete solution.
A second question would be whether to use a pull-up resistor or not. The pin can be declared as INPUT_PULLUP, but it makes no difference in my testing. Since I'm supplying voltage to the switch which returns HIGH when the switch is open and LOW when closed I'm not sure what I need in the way of pull-up, pull-down or nothing. I always find this part confusing.