We are building an engine simulator which senses 22 screws equally spaced along a 360 degree radius on a flywheel rotating at 3600 rpm using an inductive proximity sensor. We are trying to figure how to start our program and the first problem that we are facing is that we are struggling to figure out how to determine the timing between two screws.
This is the program that we are currently using:
int pin = A0;
volatile unsigned long elapsedTimeInMicroseconds = 0;
void setup() {
static unsigned long previousTimeInMicroseconds = 0;
unsigned long time = micros();
elapsedTimeInMicroseconds = time - previousTimeInMicroseconds;
previousTimeInMicroseconds = time;
Serial.begin(115200);
}
void loop() {
noInterrupts(); // Keep the value from changing as we read it.
unsigned long interval = elapsedTimeInMicroseconds;
interrupts();
Serial.println(interval);
delay(1);
}
Arrange for your proximity sensor to trigger an interrupt and have the code in the Interrupt Service Routine (ISR) save the value of micros() and change a variable (maybe called newValue) to true so the rest of the program knows the interrupt has happened.
If the main program sees newValue == true it can get the saved value of micros() and compare it to the previous value to figure out the time between pulses.
we are currently using the analog pin A0 for our proximity sensor and I am just confused on who i would use the attachinterrupt() to trigger the interrupt since I still haven't learned much about interrupts and I am lost on how I would use this for my project. Any advice will help and i have posted a picture of the motor simulator as well.
You're not polling with analogRead are you?
If your flywheel is doing 60Hz and there are 22 screws, that's 1320Hz to detect, with a function that takes around 100us to complete - you could easily miss events.
loginkid:
we are currently using the analog pin A0 for our proximity sensor
Unless your engine is running VERY slowly (hand cranking) it will not be possible to detect pulses reliably with analog input. Arrange your detector so it works with the external interrupt on pins 2 or 3 (on an Uno).