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");
}
}