UPDATE:
I tried the following code, and got the following response. Can any one tell me if my understanding of the attachInterrupt(0,xxx,RISING) call is what I need. And it seems that in the function xxx, when i tried to disable interrupts to finish some calculations, the program stopped running. (note after the calculation I ussue the Interrupts() command to re-enable them. What am I doing wrong here?
Help is appreciated…
Code follows…
/*
RPM counter for Non-Symetrical Signal:
Turns on an LED on for one second, then off for one second, to indicate code is running
Want to Capture the pulses shown below, and measure the time/duration of the pulses.
Due to the non symettrical nature of the pulses, a simple time measurement won't work.
I'm thinkig if I triger on the Low to High pulses with an interrupt, I would then be
getting two pulses/revolution. A short pulse, followed by a long pulse. I could then
compare the prev pulse against current pulse, and if it is shorter throw the data out, but
if it is longer save the data to be averaged.
Then periodically stop the interrupt, allowning time to calculate an average for the
captured pulses, and calculate an "RPM" value on that averaged value. After the calculation
allow the interrupts to continue, and start all over...
Any ideas on this? Does the code below look feasible. I am new to Arduino, and esp the
use of the interrupts. Open to suggestions.
ltetzner 3/6/2014
Signal as seen on an Oscilloscope
[tt] +5V_______ _____ _______________________ _____ _______________________
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
gnd ---- ------- ---- -------
^ ^ ^ ^
Low-> Hi | | | | [/tt]
*/
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
int led2 = 12;
static long counter; // Holds # of times interrupt was called
// the setup routine runs once when you press reset: Target board is an Arduino UNO
[b]void setup()[/b] {
// initialize the digital pin as an output.
Serial.begin(9600);
pinMode(led, OUTPUT); //flash every second or so
pinMode(led2,OUTPUT); // Flash when input changes
pinMode(0,INPUT); // Input is pin 2, which used interrupt 0.
pinMode(0,HIGH); // Enable the internal pull-up resistor
Serial.print("Arduino Tachometer .. 3/6/2014\n\n");
attachInterrupt(0,rpmfan,RISING); //trigger low to high change
}
// the loop routine runs over and over again forever:
[b]void loop() [/b]{
// Main loop just blinks led 13 to indicate code is running, real work done in interrupt.
Blink();
}
/* Called by attachInterrupt(0,rpmfan,RISING);
*/
[b]void rpmfan() {[/b]
long nww; // Time of this interrupt call
int k;
static int j=0; // Pointer into array holding all Pulse Widths
static long oldnw; // store last time interrupt was called
static long PulseWidth[100]; // array to hold pulse widths
counter++; //global val counting # times interrupt called
nww = millis();
//each 100 time pulse detected, blink another LIED
if ( counter == 50 || counter == 100) {
digitalWrite(led2,HIGH);
} else {
digitalWrite(led2,LOW);
}
if (counter >= 100) { counter=0;} // Reset counter
PulseWidth[j]= nww - oldnw; // Save width of pulse was called
j++;
if (j == 100) {
//noInterrupts(); // disable all interrupts
/// do other stuff
Serial.print("Filled up 100 items in the array\n");
// print out all the items in the array
/* for (k=1;k<=100;k++) {
Serial.print(k);
Serial.print("\t");
if (k % 20== 0) {
Serial.println(".");
}
}
*/
j=0;
Serial.println(".");
// interrupts(); // enable interrupts
}
oldnw = nww;
}
[b]void Blink()[/b]
{
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
const int ledPin = 13; // Blink LED pin
static int ledState=LOW;
static long previousMillis=0;
static int count=0;
if(currentMillis - previousMillis > 1000) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
ledState = !ledState;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
if (ledState) {
Serial.print(currentMillis);
Serial.print("\t ON ");
if (count==5){
Serial.println();
count=1;
} else {
count++;
}
} //endif ledState
} //endif CurrentMillis
} //end sub Blink
====================================================================
This is what I was seeing on the Serial Monitor
Arduino Tachometer … 3/6/2014
1001 ON 3003 ON 5005 ON 7007 ON 9009 ON 11011 ON
13014 ON 15016 ON 17018 ON 19020 ON Filled up 100 items in the array
.
21022 ON
23024 ON Filled up 100 items in the array
.
25026 ON 27028 ON 29030 ON Filled up 100 items in the array
.
31032 ON
33034 ON 35036 ON Filled up 100 items in the array
.
37038 ON 39040 ON Filled up 100 items in the array
.
41042 ON
43044 ON 45046 ON Filled up 100 items in the array
.
47048 ON 49050 ON 51052 ON
53055 ON 55057 ON 57059 ON 59061 ON 61063 ON
63065 ON 65067 ON 67069 ON 69071 ON 71073 ON
73075 ON 75077 ON 77079 ON 79081 ON 81083 ON
83085 ON 85087 ON 87089 ON 89091 ON 91094 ON