A Working Example with Interrupts and Serial Communication on Arduino
This example highlights Arduino's great capabilities in handling interrupts. In this example an output variable follows an input variable with a delay of a very few microseconds…
https://www.electronics-engineering.com/en/a-working-example-with-interrupts-and-serial-communication-on-arduino/

This example highlights Arduino’s great capabilities in handling interrupts.
In this example an output variable follows an input variable with a delay of a very few microseconds
A WORKING EXAMPLE WITH INTERRUPTS AND SERIAL COMMUNICATION
With this program, the Arduino generates a 20 µs pulse from the loop function. This pulse enters another input of the Arduino. Through an interrupt from this second input a serial command is generated and a new output is derived. From the serial port a 1 is obtained for every pulse and the new output is a square wave whose edges are the instants of the pulses.
// A simple sketch to read GPS data and parse the $GPRMC string
// see http://www.ladyada.net/make/gpsshield for more info
#include <NewSoftSerial.h>
NewSoftSerial mySerial = NewSoftSerial(2, 3);
#define GPSRATE 4800
//#define GPSRATE 38400
int pbIn = 0; // Interrupt 0 is on DIGITAL PIN 2!
int ledOut = 13; // The output LED pin
volatile int state = LOW; // The input state toggle
int ledPin = 8;
void setup() {
pinMode(ledPin, OUTPUT); // sets the digital pin as output
// Set up the digital pin 2 to an Interrupt and Pin 4 to an Output
pinMode(ledOut, OUTPUT);
//Attach the interrupt to the input pin and monitor for ANY Change
attachInterrupt(pbIn, stateChange, RISING);
Serial.begin(GPSRATE);
mySerial.begin(GPSRATE);
}
void loop() {
digitalWrite(ledPin, HIGH); // sets the LED on
delayMicroseconds(17); // waits for 17ms
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // wait for 1s with tune
}
void stateChange() {
Serial.print("1");
state = !state;
digitalWrite(ledOut, state);
}
Applications
With this code the Arduino can be used as a pulse generator through a coaxial cable and at the same time forward a flag message to a PC over the serial (USB) port. This flag can be read by software created on the PC (for example with C++) that sends a suitable signal synchronised with the pulses to a different serial port.

This is the typical example of a simulator of a GPS receiver that supplies position coordinates and time over the serial port associated with the instant of the 1PPS pulse.
5 TO 10 V HIGH-FREQUENCY SQUARE-WAVE AMPLIFIER
Often the pulse signal of the GPS receiver is 10 V: 10V 1pps, whereas the Arduino output is 5V, so a small 5V to 10V amplifier also has to be developed, described in this post






Benvenuti nella pagina su «Un Esempio Funzionante con Interrupt e Comunicazione Seriale con Arduino». L'ho scritta pensando a chi studia ingegneria elettronica: se un passaggio non vi torna, un esercizio vi dà un risultato diverso o volete altro materiale sull'argomento, chiedete pure nei commenti e vi risponderò. Un voto con le stelle qui sopra mi aiuta a capire quali pagine migliorare.