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.
main.cpp
00001 /* Jason Schilling 00002 jschilling22@my.whitworth.edu 00003 1/15/18 00004 Mini Project 6 00005 This program senses one or two taps in sucession and turns on an led if it was one or two taps. 00006 */ 00007 #include <mbed.h> 00008 00009 DigitalOut ss(p14); 00010 SPI acc(p11, p12, p13); //MOSI, MISO, and SCK 00011 void tapsHappened(void); 00012 DigitalOut led1(LED1); 00013 DigitalOut led2(LED2); 00014 DigitalOut led4(LED4); 00015 InterruptIn tapInterrupt(p5); 00016 Timeout singleTimeout; 00017 Timeout doubleTimeout; 00018 void singleOff(void); 00019 void doubleOff(void); 00020 00021 int main() { 00022 ss = 1; // turns off the accelerometer(ADXL345) while setting format and frequency 00023 acc.format(8,3); // 8 bit words, mode 3 00024 acc.frequency(2000000); // 2 MHz clock 00025 // the ADXL345 is turned on for each transmission (and off after each transmission) to adjust its settings 00026 /* for all of the setting adjustments, the first line is the address of the register. 00027 The second line adjusts the setting to the desired value */ 00028 ss = 0; 00029 acc.write(0x1D); // sets tap threshold to 5g 00030 acc.write(80); 00031 ss = 1; 00032 00033 ss = 0; 00034 acc.write(0x21); // sets tap duration to 00035 acc.write(0x10); // 10 ms 00036 ss = 1; 00037 00038 ss = 0; 00039 acc.write(0x22); // sets double tap gap to 00040 acc.write(0x05); // 6 ms 00041 ss = 1; 00042 00043 ss = 0; 00044 acc.write(0x23); // sets double tap window to 00045 acc.write(0xFF); // 318.75 ms 00046 ss = 1; 00047 00048 ss = 0; 00049 acc.write(0x2A); // sets tap axes to enable the 00050 acc.write(0x07); // x, y, and z axes for tap detection 00051 ss = 1; 00052 00053 ss = 0; 00054 acc.write(0x2E); // sets interrupt enable just to 00055 acc.write(0x60); // single/double taps 00056 ss = 1; 00057 00058 ss = 0; 00059 acc.write(0x2F); // sets interrupt map to set INT2 high if either a 00060 acc.write(0x60); // single or double tap interrupt is detected 00061 ss = 1; 00062 00063 ss = 0; 00064 acc.write(0x2D); // sets power control to 00065 acc.write(0x08); // measure mode 00066 ss = 1; 00067 00068 wait(0.5); 00069 tapInterrupt.rise(&tapsHappened); 00070 tapsHappened(); // the interrupt is high before getting to this line and will remain high before it is read again, 00071 // this line reads it and sets it back to low allowing the program to continue working 00072 while (true) { 00073 led4 = !led4; // led4 flashes to show prgram is running 00074 wait(0.3); 00075 } 00076 } 00077 00078 void tapsHappened(void) { 00079 char tapByte; 00080 ss = 0; 00081 acc.write(0x30 | 0x80); // 0x30 is the address, 0x80 means we are reading it 00082 tapByte = acc.write(0); // write zero to get a read response (could send anything). tapByte is set equal to this response 00083 ss = 1; 00084 00085 if (tapByte & 0x40) { 00086 /* 0x40 converted to binary is a single tap. If there is a single tap, 00087 it turns on LED 1 and activates the singleOff function one second later */ 00088 led1 = 1; 00089 singleTimeout.attach(&singleOff, 1.0); 00090 } 00091 if (tapByte & 0x20) { /* 0x20 converted to binary is a double tap. If there is a double tap, 00092 it turns on LED 2 and activates the doubleOff function one second later. 00093 This will turn on LED 1 and 2 because every double tap starts with a single tap */ 00094 led2 = 1; 00095 doubleTimeout.attach(&doubleOff, 1.0); 00096 } 00097 } 00098 void singleOff(void) { 00099 // first LED off when this function is called by the timeout 00100 led1 = 0; 00101 } 00102 void doubleOff(void) { 00103 // first LED off when this function is called by the timeout 00104 led2 = 0; 00105 }
Generated on Thu Jul 28 2022 10:11:51 by
1.7.2