I am new to Arduino and I was trying to figure out how to use an interrupt. When I ran this it would read my magnet but the interrupt would never be called and I couldn't figure out if anyone can help me that would be great.
Thanks.
Here is the code
#include <PinChangeInterrupt.h>
//#define INTERRUPT_PIN 12
#include <Math.h>
#define RADIUS 3
const int hallPin = 7; // the number of the hall effect sensor pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int hallState = 0; // variable for reading the hall sensor status
double inital_val = 0.0;
double distance = inital_val;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(hallPin, INPUT);
//attachInterrupt(hallPin, calc_distance, HIGH);
// initialize the hall effect sensor pin as an input:
attachInterrupt(digitalPinToInterrupt(hallPin), calc_distance , HIGH);
Serial.begin(9600);
}
void loop() {
//read the state of the hall effect sensor:
hallState = digitalRead(hallPin);
if (hallState == LOW) {
// turn LED on:
Serial.print("Main Loop: HallState LOW\n");
digitalWrite(ledPin, HIGH);
Serial.print(distance, 2);
Serial.print("\n");
} else {
Serial.print("Main Loop: HallState HIGH\n");
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
void calc_distance() {
Serial.print("INTERRUPT! \n");
if (digitalRead(hallPin) == HIGH) {
Serial.print("YES!\n");
distance += PI * 2 * RADIUS;
} else {
Serial.print("NO!\n");
}
}
-
1Are you sure hallPin=7 has an interrupt on your particular board? Unos interrupt on 2,3 only.Dave X– Dave X2017年03月03日 04:38:16 +00:00Commented Mar 3, 2017 at 4:38
1 Answer 1
The attachInterrupt function is only for normal interrupts. Read the reference: attachInterrupt
When you need the Pin Change Interrupts, there is a library for that: EnableInterrupt
Try to keep the interrupt routine as short as possible. Do not use the Serial functions in calc_distance because the Serial functions themself use also interrupts.
The Arduino Mega does not have 'double' floating point variables, the compiler changes it into 32-bit 'float' variables. You are reading the interrupt pin for a test ? I don't understand why you do that. Please make "distance" a volatile variable. In the loop function, the interrupts should be turned off when reading "distance". Because "distance" is a 4 bytes 'float' variable and the Arduino Mega is a 8-bits microcontroller. An interrupt could occur when the "distance" is only read for a part from memory.
-
Yes to moving the serial stuff out of the interrupt, but attachInterrupt should be able to handle pin changes just fine. Maybe
attachInterrupt(digitalPinToInterrupt(hallPin),calc_distance , RISING);
orattachInterrupt(digitalPinToInterrupt(hallPin),calc_distance , CHANGE);
would be better than whenever it is HIGH.Dave X– Dave X2017年03月03日 04:42:12 +00:00Commented Mar 3, 2017 at 4:42 -
The attachInterrupt function is only for pin 2 and 3 for an Arduino Uno, according to the reference. Because it uses the normal interrupts. The special "Pin Change Interrupt" (PCINT) requires specific code or a library. When a library is used, the functions from that library should be used for the interrupt. This code seems to use a little of both.Jot– Jot2017年03月03日 07:31:48 +00:00Commented Mar 3, 2017 at 7:31