Skip to content

Commit

Permalink
2nd falvor of freq meas
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Oberstein committed Aug 29, 2015
1 parent b5a51d9 commit f86f6c0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions wolke/fan1/fan1.ino
@@ -1,3 +1,5 @@
// Frequency Measurement on multiple PINs using counting

#define FAN_COUNT 1
#define ITER_DELAY 200
#define REPORT_EVERY_ITERS 8000
Expand Down
41 changes: 41 additions & 0 deletions wolke/fan2/fan2.ino
@@ -0,0 +1,41 @@
// Frequency Measurement on 1 PIN using hardware interrupts
// and measuring actual duratations (period lengths)

static const int PIN_FAN1 = 2; // D3 (Yun) = Interrupt 0 / O5 (Tinkershield)

unsigned long last_time = 0;
unsigned long last_period = 0;

void handler () {
unsigned long current_time = micros();

if (last_time == 0) {
last_time = micros();
return;
}

last_period = current_time - last_time;
last_time = current_time;
}

void setup () {
Serial.begin(115200);
pinMode(PIN_FAN1, INPUT_PULLUP);

// https://www.arduino.cc/en/Reference/AttachInterrupt
attachInterrupt(0, handler, RISING);
}

void loop () {

int rpm = 0;

if (last_period != 0) {
rpm = (int) round(60. * (1000000. / ((double)(last_period))) / 2.);
}

Serial.print(rpm);
Serial.println();

delay(500);
}

0 comments on commit f86f6c0

Please sign in to comment.