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.
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?
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.
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.
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.
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.
How to ignore interrupts while printing of data?
Just print - leave the ISR to keep running
How to turn "ON" while storing of data for 2 rotations?
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
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.
It is taking 2 interrupts at a time, while one interrupt is passes.
Not getting reliable output. i.e most of the time it does not gives readings as obtained in oscilloscope.
I have check all connections and grounded them properly.
I have NO idea what you are trying to say.
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.
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.
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.