I am trying to make an Arduino count pulses via an interrupt and output the number over serial line (USB) to a computer.
I got everything to work with an Uno, but since it resets every time something connects to the USB I decided to use a Leonardo.
Here I understand that both interrupts and serial ports are slightly different. I still don't understand why this simple sketch causes problems. I have removed any attempts at actually output data on the serial port in the example code.
Pin 13 is the LED, interrupt 0 should be pin 3 on the Leonardo. No pins are connected to anything yet, so I can trigger the interrupt
just by touching the solder around the pin on the board.
// pin for LED
#define LEDpin 13
// pin for interrupt
#define IRintr 0
// state change
volatile int state = HIGH;
// the rotation counter - number of rotations seen
volatile int rotations=0;
void setup() {
// enable LED
pinMode(LEDpin, OUTPUT);
// use pin 0 for interrupts, triggered on falling level
attachInterrupt(IRintr, blink, LOW);
Serial.begin(9600); // serial output
}
void loop() {
digitalWrite(LEDpin, state);
}
void blink()
{
state = !state;
rotations++;
}
When I upload the sketch, my syslog shows that the Leonardo is no longer recognized after a few seconds:
Feb 23 17:04:58 linux1 mtp-probe: checking bus 4, device 82: "/sys/devices/pci0000:00/0000:00:1d.2/usb4/4-1"
Feb 23 17:04:58 linux1 mtp-probe: bus: 4, device: 82 was not an MTP device
Feb 23 17:05:06 linux1 kernel: [251091.472084] usb 4-1: USB disconnect, device number 82
Feb 23 17:05:06 linux1 kernel: [251091.808075] usb 4-1: new full-speed USB device number 83 using uhci_hcd
Feb 23 17:05:06 linux1 kernel: [251091.932060] usb 4-1: device descriptor read/64, error -71
Feb 23 17:05:07 linux1 kernel: [251092.160064] usb 4-1: device descriptor read/64, error -71
Feb 23 17:05:07 linux1 kernel: [251092.376057] usb 4-1: new full-speed USB device number 84 using uhci_hcd
Feb 23 17:05:07 linux1 kernel: [251092.500063] usb 4-1: device descriptor read/64, error -71
Feb 23 17:05:07 linux1 kernel: [251092.728059] usb 4-1: device descriptor read/64, error -71
Feb 23 17:05:07 linux1 kernel: [251092.944063] usb 4-1: new full-speed USB device number 85 using uhci_hcd
Feb 23 17:05:08 linux1 kernel: [251093.360042] usb 4-1: device not accepting address 85, error -71
Feb 23 17:05:08 linux1 kernel: [251093.472056] usb 4-1: new full-speed USB device number 86 using uhci_hcd
Feb 23 17:05:08 linux1 kernel: [251093.888059] usb 4-1: device not accepting address 86, error -71
Feb 23 17:05:08 linux1 kernel: [251093.888102] hub 4-0:1.0: unable to enumerate USB device on port 1
Happens every time I reset, the delay appears to be from the Arduino is plugged in to the program (sketch) is running.
I can press the reset button and upload a new sketch before the error occurs.
How can I configure one pin for interrupts and output to USB serial on the Arduino Leonardo?