Hi I am very new to Arduino, with very little programming experience.

I am using mega 2560 and trying to understand how interrupt works.

I have a code written, it is very similar as the example code Memsic2125, so i am gonna use that in here.

For my case, I am reading from my hardware, 5x5 array of sensors.

What I would like to do is to have it -- print out x and y using interrupts , I want to print outputs the x and y every 10ms. Instead of printing it continuously right now.

I will be sending it out in 16 bits. I tried some other example and read a lot of material. but still couldn't get it to work.

Any idea, suggestion, help, comment is appreciated.

const int xPin = 2;    
const int yPin = 3;

void setup() {

  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);

void loop() {

  int pulseX, pulseY;

  int x, y;

  // read pulse from x- and y-axes:
  pulseX = pulseIn(xPin, HIGH);
  pulseY = pulseIn(yPin, HIGH);

  x= ((pulseX / 10) - 500) * 8;
  y= ((pulseY / 10) - 500) * 8;

  // print x,y
  Serial.print("x,y= ( ");

  Serial.print(x);   Serial.print(,);
  Serial.print(y);   Serial.println(")");

  • You are already printing every ~100 ms.
    – gre_gor
    Jun 29 '17 at 22:03
  • You can’t truly have an interrupt within the code (it’s a hardware thing) but you can simulate an interrupt through an ISR which you don’t have
    – Aaron
    Jul 27 '19 at 20:22

There isn't any interrupt code in what you posted. What exactly is the problem? One important note is that you shouldn't try to print to Serial line in the interrupt handler as serial is itself driven from interrupts which are switched off during your handler code. If all you really want to do is to print something every 10ms then you don't need any interrupt. Just use the "Blink Without Delay" style of timing and keep the other code from blocking.


The simplest would-be to read the data and loop around for your desired time.

Alternatively, time stamp the events and onkybread the data if the desired time has elapsed.

Or to use a timer interrupt for that.


Your Answer

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