Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:be447af0921d, committed 2011-11-25
- Comitter:
- daveTshave
- Date:
- Fri Nov 25 14:32:50 2011 +0000
- Commit message:
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/counter/counter.cpp Fri Nov 25 14:32:50 2011 +0000
@@ -0,0 +1,31 @@
+/*
+ * Based on Aaron Berk's library for a QEI (http://mbed.org/users/aberk/libraries/QEI/le4bkf), this mainly involved deleting most of the functionality
+ *
+ * Includes
+ */
+#include "counter.h"
+
+counter::counter(PinName channelA) : channelA_(channelA){
+
+ pulses_ = 0;
+ channelA_.rise(this, &counter::encode);
+
+}
+
+void counter::reset(void) {
+
+ pulses_ = 0;
+
+}
+
+int counter::getPulses(void) {
+
+ return pulses_;
+
+}
+
+void counter::encode(void) {
+
+ pulses_++;
+
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/counter/counter.h Fri Nov 25 14:32:50 2011 +0000
@@ -0,0 +1,57 @@
+#ifndef counter_H
+#define counter_H
+
+/**
+ * Includes
+ */
+#include "mbed.h"
+
+/**
+ * Simple counter
+ */
+class counter {
+
+public:
+
+
+ /**
+ * Constructor.
+ *
+ * Attaches the encode function to the rise/fall edge of
+ * channels A.
+ *
+ * @param channelA mbed pin for counter input.
+ */
+ counter(PinName channelA);
+
+ /**
+ * Reset the encoder.
+ *
+ * Sets the pulses and revolutions count to zero.
+ */
+ void reset(void);
+
+ /**
+ * Read the number of pulses recorded by the encoder.
+ *
+ * @return Number of pulses which have occured.
+ */
+ int getPulses(void);
+
+private:
+
+ /**
+ * Update the pulse count.
+ *
+ * Called on every rising edge of channels A.
+ *
+ */
+ void encode(void);
+
+ InterruptIn channelA_;
+
+ volatile int pulses_;
+
+};
+
+#endif /* counter_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Fri Nov 25 14:32:50 2011 +0000
@@ -0,0 +1,52 @@
+#include "counter.h"
+
+//todo: implement 2k sample buffer and store results until client connects, implement real time clock
+
+Serial pc(USBTX, USBRX);
+counter SF800 (p29);
+Timer t_a, t_b;
+
+int main() {
+
+ t_a.start();
+ t_b.start();
+ int a_init_pulses = 0;
+ int b_init_pulses = 0;
+ float flowrate = 0;
+ int time_window = 10000;
+
+ bool a_b_flip_flop = true; // if true use window a
+
+ pc.printf("\n\r*************\n\rMBED restarted\n\r*************\n");
+
+ while (1) {
+ wait(0.1);
+
+ if (a_b_flip_flop) {
+ flowrate = (float)(SF800.getPulses() - a_init_pulses)/(6000 * t_a.read_ms() / 60000);
+ } else {
+ flowrate = (float)(SF800.getPulses() - b_init_pulses)/(6000 * t_b.read_ms() / 60000);
+ }
+
+ if (t_a.read_ms() > time_window) { // switch to window B
+
+ pc.printf("\rswitching to window B = %i pulses detected in window A\n", SF800.getPulses() - a_init_pulses);
+
+ t_a.reset();
+ a_b_flip_flop = false;
+ a_init_pulses = SF800.getPulses();
+ }
+
+ if (t_a.read_ms() > time_window/2 && !a_b_flip_flop) { // switch to window A
+
+ pc.printf("\rswitching to window A = %i pulses detected in window B\n", SF800.getPulses() - b_init_pulses);
+
+ t_b.reset();
+ a_b_flip_flop = true;
+ b_init_pulses = SF800.getPulses();
+ }
+
+ pc.printf("\rflowrate = %f l/min", flowrate);
+ }
+
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Nov 25 14:32:50 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/9114680c05da