How to use interrupt function whenever needed?

Hello,

I am working on R&D Project at institute level. I have to use interrupt function whenever it is called and off it whenever i want to do processing of data. I have written simple code in which whenever interrupt is less than 2 then arduino read the data from analog pin and stored it in an array. Once count reaches to 2 then it turn off the interrupt and print the reading stored in an array. but my code is not running properly. I am also attaching my code here.

Thanking you,

sketch_sep19b.ino (815 Bytes)

The forum does not have an interrupt function. Neither does the website. Your post in the Website and Forum part of the forum makes no sense.

at institute level.

I’m not sure that the institute should allow you to have Arduinos.

I have to use interrupt function whenever it is called and off it whenever i want to do processing of data.

If you don’t even know what it is called, why do you think you have to use an interrupt (function) at all?

I have written simple code in which whenever interrupt is less than 2 then

The postman ringing the doorbell when he has a package to deliver is an interrupt. How can that be less than 2?

but my code is not running properly.

The code you posted does something. You forgot to tell us what it does.

Something is generating the interrupt. You forgot to tell us what that is.

You expect the code to do something. You forgot to tell us what you expect.

I was going to post the answer, but I forgot.

Please, place your codes online with code tags in the future post. These are your codes, correct?

#define sensorpin A5
const byte interrupt = 2;
volatile int count = 0;
int B[500] = {0};
int j = 0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(19200);
  pinMode(interrupt, INPUT);
  attachInterrupt(digitalPinToInterrupt(interrupt), blink, RISING);
}

void loop() {
  // put your main code here, to run repeatedly:
  //Serial.println(digitalRead(interrupt));
  while (count != 2)
  {
    B[j] =  analogRead(sensorpin);
    j++;
    delay(1);
  }


  if (count == 2)
  {
    noInterrupts();
    for (j = 0; j < 500; j++)
    {
      Serial.println(B[j]);
    }
  }
  interrupts();


  //  Serial.println(" ");
  //  Serial.println(" ");
  //  Serial.println(" ");
  Serial.println(" ");

}

void blink() {
  if (count < 2)
  { count++;
  }
  else
  {
    count = 0;
    j = 0;
  }
}

Please, modify your codes based on the answer of the following question.

The attachInterrupt(); function enables the local interrupt logic for INT0 (DPin-2 of UNO) interrupt. Does the function activate/enable Global Interrupt Bit? If the answer is no, then put tick marks on the functions of the following list from which we can choose one to activate the global interrupt bit?

noInterrupts(), interrupts(), sei(), cli(), bitClear(SREG, 7), bitSet(SREG, 7), bitWrite(SREG, 7, HIGH)
    noInterrupts();
    for (j = 0; j < 500; j++)
    {
      Serial.println(B[j]);
    }

Serial printing relies on interrupts…

Nasehmomin:
I am working on R&D Project at institute level. I have to use interrupt function whenever it is called and off it whenever i want to do processing of data. I have written simple code in which whenever interrupt is less than 2 then arduino read the data from analog pin and stored it in an array. Once count reaches to 2 then it turn off the interrupt and print the reading stored in an array. but my code is not running properly. I am also attaching my code here.

That seems a strange way to do something.

If you describe the overall project it will be much easier to give useful advice.

For example what is causing the interrupt and what is the significance of counting to 2?

Your count variable should be a byte.
You should not be trying to print when interrupts are off.

...R

Dear Sir,
I am extremely sorry for the incomplete post. Actually, my project is to monitor backlash in spur gears while it is in working condition. I have 45 number of teeth in spur gears and I had reduced the thickness of one tooth by 1mm. for e.g. Standard thickness of all teeth is 3 mm and tooth with backlash has 2mm thickness. I have attached a nano magnet at one tooth and I am using 2 sensors for monitoring backlash. One is hall effect proximity switch which is used to indicate starting point of each rotation by detecting nano magnet attached on the tooth. Second sensor is a linear hall effect sensor which changes amplitude according to the magnetic filed present in its sensing area. Whenever gear tooth comes in front of sensor then it shows low amplitude as magnetic filed gets accumulated in it and as gap between adjacent tooth comes then it shows higher amplitude. Due to presence of backlash the amplitude value increases as gap increases. Now, What I want to do is to store the reading of 2 rotation and display the value after 2 rotation then I have to compare each tooth's value with previous tooth's value and see if there difference is more than specified limit then backlash is present. Also I am using TFT screen shield to display results on it. I have written the code in which proximity switch acts as an interrupt and indicates starting point of each rotation, Once interrupt occur then for 2 rotations array stores the value and after 2 rotations it turn off the interrupt and display the stored value of an array and further processing of stored data that means comparison and all. but serial monitor did not shows the value properly. Sometime it shows 35 number of teeth (peaks), sometime shows exact 45 and sometimes less than 35 number of teeth.

Writing all that text in a single paragragh makes it impossible to comprehend. You need to split up the thoughts into paragraphs - like this

I am extremely sorry for the incomplete post.

Actually, my project is to monitor backlash in spur gears while it is in working condition.

I have 45 number of teeth in spur gears and I had reduced the thickness of one tooth by 1mm. for e.g. Standard thickness of all teeth is 3 mm and tooth with backlash has 2mm thickness.

I have attached a nano magnet at one tooth and I am using 2 sensors for monitoring backlash.

One is hall effect proximity switch which is used to indicate starting point of each rotation by detecting nano magnet attached on the tooth.

Second sensor is a linear hall effect sensor which changes amplitude according to the magnetic filed present in its sensing area.

Whenever gear tooth comes in front of sensor then it shows low amplitude as magnetic filed gets accumulated in it and as gap between adjacent tooth comes then it shows higher amplitude.

Due to presence of backlash the amplitude value increases as gap increases.

Now, What I want to do is to store the reading of 2 rotation and display the value after 2 rotation then I have to compare each tooth's value with previous tooth's value and see if there difference is more than specified limit then backlash is present.

Also I am using TFT screen shield to display results on it.

I have written the code in which proximity switch acts as an interrupt and indicates starting point of each rotation, Once interrupt occur then for 2 rotations array stores the value and after 2 rotations it turn off the interrupt and display the stored value of an array and further processing of stored data that means comparison and all.

but serial monitor did not shows the value properly. Sometime it shows 35 number of teeth (peaks), sometime shows exact 45 and sometimes less than 35 number of teeth.

...R

From your explanation (coupled with your code) it seems that you collect up to 500 analog readings with a 1 millisec gap between readings and it also seems that you expect to get less than 500 readings for 2 revolutions of the gear.

Which brings to mind that you have not told us how fast the gear rotates or whether it is rotating at a controlled fixed speed.

I presume then that you are looking at the pattern in the 500 analog readings to detect the effect on the readings of the passage of the teeth past the sensor.

Based on what you have told us so far I have no idea whether that is reasonable expectation. I am inclined to think that you should have some means to sync the analog readings with the passage of the teeth or with the frequency of the passage of teerh - for example so that you always get (say) 5 ADC readings for each tooth.

As far as controlling the system is concerned I don't think there is any need to turn of interrupts - just ignore them while you are printing and when the printing is finished wait for the next interrupt to signify the start of collection of a new set of data.

...R

  1. With this rate of sampling (i.e. recording values with 1 ms of interval), I am insure that it is taking

around 10 samples for passage of 1 single tooth.

  1. How to ignore interrupts while printing of data? and

  2. How to turn "ON" while storing of data for 2 rotations?

  3. With the help of interrupt I can decide on which tooth backlash is present.

Nasehmomin:

  1. With this rate of sampling (i.e. recording values with 1 ms of interval), I am insure that it is taking

around 10 samples for passage of 1 single tooth.

If that word is meant to be "unsure" then you should make yourself sure.

  1. How to ignore interrupts while printing of data?

Just print - leave the ISR to keep running

  1. How to turn "ON" while storing of data for 2 rotations?

  2. With the help of interrupt I can decide on which tooth backlash is present.

If the ISR is not turned off then there is no need to turn it on.

When your interrupt is triggered that represents the end of the data collection arrange for it to change a variable (let's call it dataCollectionInProgress) to false and another variable dataNotYetPrinted to true.

Your data collection code should be arranged so it will only work when dataCollectionInProgress is true and your printing code should only work when dataNotYetPrinted is true.

When the printing is finished the variable dataNotYetPrinted should be set to false and the next Interrupt should check that dataNotYetPrinted == true and dataCollectionInProgress == false and if so it should set dataCollectionInProgress to true and set count back to 0

...R

PaulS:

    noInterrupts();

for (j = 0; j < 500; j++)
   {
     Serial.println(B[j]);
   }



Serial printing relies on interrupts...

They fixed that. If the output buffer is full and interrupts are disabled, the .write() function will spin-wait on the serial output complete interrupt flag. When it is set, the ISR is called to take a character out of the output buffer and put it into the serial output register. Then .write() can put a new character into the output buffer. This repeats until .println() has called .write() for each character to be sent.
In the mean time, a bunch of millis() interrupts have probably been blocked so it’s as if no time has passed. :slight_smile:

Now I am facing this problem

  1. It is taking 2 interrupts at a time, while one interrupt is passes.

  2. Not getting reliable output. i.e most of the time it does not gives readings as obtained in oscilloscope.

  3. I have check all connections and grounded them properly.

Nasehmomin:
Now I am facing this problem

We can't help unless you post the latest version of your program.

Also please post some examples of the output it produces.

...R

volatile byte count = 0;
#define HallPin A5
int i = 0;
int A[400] = {0};
bool printing_done = false;
bool reading_done = false;
const byte interrupt = 2;

void setup() {
Serial.begin(9600);
pinMode(interrupt, INPUT);
attachInterrupt(digitalPinToInterrupt(interrupt), blink, FALLING);

if (count == 0)
Serial.println(“Reading Started”);

}

void loop() {

//Reading******
if (count < 5 && reading_done == false) //count < 2
{
if (i < 400) //i<400
{
A = analogRead(HallPin);

  • i++;*
  • //Serial.print(".");*
  • delay(3);*
  • }*
  • }*
  • // else if (count >= 5 && reading_done == false)*
  • // {*
  • // reading_done = true;*
  • // }*
    _ //**************************************************************_
    _ //Printing
    _
  • if (reading_done == true && printing_done == false)*
  • {*
  • printing_done = false;*
  • Serial.println(“Printing Started”);*
  • for (i = 0; i < 400; i++)*
  • {*
    _ Serial.println(A*);_
    _
    //Serial.print(".");_
    _
    delay(1);_
    _
    }_
    printing_done = true;
    _
    Serial.println(“Printing Done”);_
    _
    }_
    _ //*************_
    _
    }
    _
    void blink()
    _
    {
    _
    _
    // Reading until 4 intr
    _
    _
    //Serial.println(count);
    _
    if (count < 5 && reading_done == false && printing_done == false)

    _
    {
    _
    _
    if (count == 0) {
    _
    _
    Serial.println(“Reading Started”);
    _
    _
    }
    _
    _
    count++;
    _
    _
    if (count == 5) {
    _
    Serial.println(“Reading_done”);

    reading_done = true;

    _
    }
    _
    _
    }
    _
    _
    // when Reading is done then print
    _
    else if (reading_done == true && printing_done == false)

    _
    {
    _
    _
    //Serial.println("");
    _
    _
    }
    _
    _
    //when printing is done then reset everything
    _
    else if (reading_done == true && printing_done == true)

    _
    {
    _
    _
    count = 0;
    _
    _
    i = 0;
    _
    reading_done = false;

    printing_done = false;

    _
    }
    _
    _
    }
    _
    OUTPUT:
    Reading Started
    Reading_done
    Printing Started
    486
    484
    476
    463
    453
    447
    448
    458
    468
    474
    473
    466
    451
    441
    432
    434
    440
    454
    468
    476
    480
    475
    466
    458
    449
    447
    452
    464
    477
    486
    487
    484
    475
    462
    457
    457
    464
    473
    485
    493
    492
    487
    479
    466
    460
    459
    462
    474
    483
    496
    498
    492
    481
    469
    462
    461
    466
    479
    493
    500
    503
    494
    482
    469
    463
    461
    469
    478
    489
    499
    503
    495
    487
    479
    465
    465
    467
    479
    491
    500
    504
    497
    489
    475
    466
    464
    469
    478
    493
    504
    503
    495
    491
    468
    461
    462
    471
    481
    493
    502
    504
    496
    481
    473
    460
    464
    469
    480
    493
    504
    504
    497
    480
    471
    462
    464
    472
    487
    499
    507
    506
    494
    479
    470
    465
    466
    479
    493
    506
    511
    507
    498
    484
    472
    469
    471
    482
    497
    506
    510
    505
    490
    476
    464
    463
    473
    484
    498
    508
    506
    497
    480
    470
    463
    465
    477
    487
    499
    506
    498
    493
    480
    466
    465
    470
    482
    495
    503
    502
    497
    480
    466
    462
    468
    480
    491
    503
    506
    498
    487
    472
    463
    460
    471
    487
    500
    503
    504
    493
    482
    467
    464
    469
    485
    499
    508
    505
    497
    484
    470
    468
    473
    486
    499
    511
    510
    501
    484
    469
    464
    465
    479
    494
    504
    513
    505
    490
    474
    465
    468
    482
    497
    508
    510
    504
    492
    477
    466
    470
    481
    498
    507
    512
    504
    490
    478
    465
    460
    470
    486
    495
    500
    494
    481
    469
    464
    464
    473
    485
    499
    504
    500
    484
    469
    462
    463
    476
    484
    500
    508
    499
    489
    473
    462
    461
    474
    486
    500
    505
    501
    491
    473
    464
    463
    473
    487
    500
    503
    500
    486
    474
    463
    460
    469
    480
    493
    501
    495
    486
    473
    462
    458
    466
    475
    493
    500
    497
    486
    471
    458
    457
    464
    475
    494
    499
    499
    484
    470
    462
    458
    463
    472
    487
    497
    496
    491
    477
    464
    457
    462
    471
    484
    492
    493
    488
    474
    461
    456
    457
    467
    480
    494
    489
    485
    473
    460
    451
    449
    456
    471
    482
    488
    483
    474
    467
    453
    447
    453
    466
    478
    486
    486
    474
    460
    448
    447
    450
    462
    471
    480
    487
    478
    470
    457
    450
    448
    454
    465
    476
    486
    483
    476
    466
    454
    446
    450
    457
    472
    481
    485
    482
    472
    460
    450
    449
    454
    465
    475
    486
    488
    480
    472
    459
    453
    451
    454
    466
    477
    488
    489
    Printing Done
    See it printed reading started 2 times only.

Interrupts are disabled when interrupt service routines are running. Stop trying to Serial.print() in the ISR. blink() IS an ISR.

Nasehmomin:
Now I am facing this problem

  1. It is taking 2 interrupts at a time, while one interrupt is passes.

  2. Not getting reliable output. i.e most of the time it does not gives readings as obtained in oscilloscope.

  3. I have check all connections and grounded them properly.

  1. I have NO idea what you are trying to say.

  2. You have NOT told us what you are reading from, nor have you told us what is triggering the interrupt. You have not shown a schematic. You REALLY need to supply a lot more information.

For all I know, your problem is a floating pin condition, where you have no idea when the interrupt will be triggered.

  1. I am trying to see whether code is running properly or not. For that reason I am printing in ISR.

  2. As per your suggestion I removed serial printing in ISR but Still after 2 rotation itself, it is started printing values.

  3. Please suggest appropriate solution.

but Still after 2 rotation itself, it is started printing values.

After 2 rotations of what? If you can't answer ALL the questions that are asked, and provide all the information needed, we can NOT help you.

Nasehmomin:

  1. I am trying to see whether code is running properly or not. For that reason I am printing in ISR.

If you print in the ISR the code is guaranteed NOT to run properly.

  1. As per your suggestion I removed serial printing in ISR but Still after 2 rotation itself, it is started printing values.

We cannot see that version of your program.

Have you taken any account of what I said in Reply #9?

And PLEASE help us to help you by posting your code (and the output from it) using the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum.

…R