Piezo Vibration Sensor demo
Dependencies: PCA9622_LED8x8 mbed
Revision 0:2f98d8d6ef4b, committed 2015-07-26
- Comitter:
- MACRUM
- Date:
- Sun Jul 26 15:46:51 2015 +0000
- Child:
- 1:bcf6ab734086
- Commit message:
- Initial commit
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PCA9622_LED8x8.lib Sun Jul 26 15:46:51 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/nxp_ip/code/PCA9622_LED8x8/#fb23972da0f3
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/font3x5.h Sun Jul 26 15:46:51 2015 +0000
@@ -0,0 +1,27 @@
+/**
+ * 3x5 bitmap font (decimal numbers from '0' to '9')
+ *
+ * @author Toyomasa WATARAI
+ * @version 1.0
+ * @date 25-July-2015
+ *
+ *
+ */
+
+#ifndef FONT3X5_H
+#define FONT3X5_H
+
+const unsigned char font3x5[] = {
+0x1f, 0x11, 0x1f, // 0
+0x00, 0x1f, 0x00, // 1
+0x17, 0x15, 0x1d, // 2
+0x15, 0x15, 0x1f, // 3
+0x1c, 0x04, 0x1f, // 4
+0x1d, 0x15, 0x17, // 5
+0x1f, 0x15, 0x17, // 6
+0x1c, 0x10, 0x1f, // 7
+0x1f, 0x15, 0x1f, // 8
+0x1d, 0x15, 0x1f, // 9
+};
+
+#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Sun Jul 26 15:46:51 2015 +0000
@@ -0,0 +1,116 @@
+/**
+ * MFT2015 demo : shake-shake-machine
+ *
+ * @author Toyomasa WATARAI
+ * @version 1.0
+ * @date 24-July-2015
+ *
+ *
+ */
+
+#include "mbed.h"
+#include "PCA9622_LED8x8.h"
+#include "font3x5.h"
+#include "tone_table.h"
+
+PCA9622_LED8x8 matrix(D14, D15);
+AnalogIn ain(A0);
+PwmOut out(D4);
+Serial pc(USBTX, USBRX);
+
+#define DURATION_TIME (5 * 1000) // n * milisecond
+
+void draw_number(int num);
+
+void draw_number(int num)
+{
+ float image[8][8];
+ num %= 100;
+
+ for (int x=0; x<8; x++)
+ for (int y=0; y<8; y++)
+ image[x][y] = 0.0f;
+
+ for (int i=0; i<5; i++) {
+ for (int j=0; j<3; j++) {
+ if (((font3x5[((num/10)*3) + j]) & (1<<(4-i))) != 0) {
+ image[i+1][j+1] = (float)0.5f;
+ } else {
+ image[i+1][j+1] = (float)0.0f;
+ }
+ }
+ }
+
+ for (int i=0; i<5; i++) {
+ for (int j=0; j<3; j++) {
+ if (((font3x5[((num%10)*3) + j]) & (1<<(4-i))) != 0) {
+ image[i+1][j+5] = (float)0.5f;
+ } else {
+ image[i+1][j+5] = (float)0.0f;
+ }
+ }
+ }
+
+ matrix.set_data( image );
+}
+
+
+void count_down(int cnt)
+{
+ for(int i=cnt; i>0; i--) {
+ draw_number(i);
+ out.period_us(tone_table_us[1]);
+ out.write(0.5f);
+ wait(0.5);
+ out.period(0);
+ wait(0.5);
+ }
+ draw_number(0);
+ out.period_us(tone_table_us[13]);
+ out.write(0.5f);
+}
+
+int main()
+{
+ int count = 0;
+ float f = 0;
+ float vv;
+ Timer timer;
+
+ timer.reset();
+ matrix.start();
+
+ count_down(3);
+
+ timer.start();
+ while(1) {
+ if ( timer.read_ms() > 1000) {
+ out.period(0);
+ }
+ vv = ain.read();
+
+ if ( abs(f - vv) >= 0.015f) {
+ count++;
+ pc.printf("*");
+ draw_number(count);
+ }
+ f = vv;
+ wait(0.1);
+ if ( timer.read_ms() > DURATION_TIME) {
+ pc.printf("count = %d\n", count);
+ draw_number(count);
+ break;
+ }
+ }
+
+ out.period_us(tone_table_us[13]);
+ out.write(0.5f);
+ wait(1);
+ out.period(0);
+
+ while(1) {
+ draw_number(count);
+ wait(1);
+
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Jul 26 15:46:51 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/bad568076d81 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tone_table.h Sun Jul 26 15:46:51 2015 +0000
@@ -0,0 +1,31 @@
+/**
+ * PWM tone table : cycle (us) of PWMed tone
+ *
+ * @author Toyomasa WATARAI
+ * @version 1.0
+ * @date 25-July-2015
+ *
+ *
+ */
+
+#ifndef TONE_TABLE_H
+#define TONE_TABLE_H
+
+const int tone_table_us[] = {
+ 0, // No tone 0
+ 3820, // C 1
+ 3610, // C# 2
+ 3410, // D 3
+ 3210, // D# 4
+ 3030, // E 5
+ 2860, // F 6
+ 2700, // F# 7
+ 2550, // G 8
+ 2410, // G# 9
+ 2270, // A 10
+ 2150, // A# 11
+ 2020, // B 12
+ 1910 // C 13
+};
+
+#endif
Toyomasa Watarai