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.
Fork of QW-Motiondetection by
main.cpp
00001 /* This program demonstrates how to use the VCNL4010 for motion detection. 00002 * Open a serial console to the board to see extra info. 00003 */ 00004 00005 #include "mbed.h" 00006 #include "VCNL4010.h" 00007 00008 #define SER_BUFFER_SIZE 32 00009 00010 /* The 4 onboard LEDs */ 00011 DigitalOut LED_0 (PB_6); 00012 DigitalOut LED_1 (PA_7); 00013 DigitalOut LED_2 (PA_6); 00014 DigitalOut LED_3 (PA_5); 00015 00016 /* The 2 user buttons */ 00017 InterruptIn SW1(PA_8); 00018 InterruptIn SW2(PB_10); 00019 00020 /* Proximity and ambient light sensor*/ 00021 VCNL40x0 VCNL4010(PB_9, PB_8, VCNL40x0_ADDRESS); // SDA, SCL pin and I2C address 00022 00023 /* Function prototypes */ 00024 void sw1interrupt(); 00025 void sw2interrupt(); 00026 void sertmout(); 00027 bool modem_command_check_ok(char * command); 00028 void modem_setup(); 00029 00030 bool ser_timeout = false; 00031 00032 /* Serial port over USB */ 00033 Serial pc(USBTX, USBRX); 00034 00035 /* Serial connection to sigfox modem */ 00036 Serial modem(PA_9, PA_10); 00037 00038 int main() 00039 { 00040 /* Storage for VCNL4010 readout */ 00041 unsigned char ID=0, Current=0; 00042 unsigned int ProxiValue=0; 00043 00044 /* Setup TD120x */ 00045 wait(3); 00046 modem_setup(); 00047 00048 /* Turn off all LED */ 00049 LED_0 = 1; 00050 LED_1 = 1; 00051 LED_2 = 1; 00052 LED_3 = 1; 00053 00054 /* Setup button interrupts */ 00055 SW1.fall(&sw1interrupt); 00056 SW2.fall(&sw2interrupt); 00057 00058 00059 /* Read VCNL40x0 product ID revision register */ 00060 VCNL4010.ReadID (&ID); 00061 pc.printf("\nVCNL4010 Product ID Revision Register: %d", ID); 00062 00063 VCNL4010.SetCurrent (20); // Set current to 200mA 00064 VCNL4010.ReadCurrent (&Current); // Read back IR LED current 00065 pc.printf("\nVCNL4010 IR LED Current: %d\n\n", Current); 00066 00067 wait_ms(3000); // Wait 3s (only for display) 00068 00069 while(1) 00070 { 00071 /* VCNL4010 reading */ 00072 VCNL4010.ReadProxiOnDemand (&ProxiValue); // read prox value on demand 00073 00074 /* If motion is detected, do a sigfox transmission */ 00075 if (ProxiValue > 2500){ // threshold can be changed to personal preference (determines the maximum distance from sensor for detection) 00076 LED_0 = 0; // turn on LED_0 to indicate motion detection and Sigfox transmission 00077 pc.printf("\rMotion detected!\n"); 00078 char command[SER_BUFFER_SIZE]; 00079 sprintf(command, "AT$SF=03%04x,2,0\n", (int) ProxiValue ); 00080 pc.printf("Sending proximity value %5.0i over Sigfox using modem command: %s\n", ProxiValue , command); 00081 //pc.printf("using modem command: %s", command); 00082 modem_command_check_ok(command); 00083 LED_0 = 1; // turn off LED_0 00084 } 00085 00086 wait_ms(100); 00087 } 00088 } 00089 00090 void modem_setup() 00091 { 00092 /* Reset to factory defaults */ 00093 if(modem_command_check_ok("AT&F")) 00094 { 00095 pc.printf("Factory reset succesfull\r\n"); 00096 } 00097 else 00098 { 00099 pc.printf("Factory reset TD120x failed\r\n"); 00100 } 00101 /* Disable local echo */ 00102 modem.printf("ATE0\n"); 00103 if(modem_command_check_ok("ATE0")) 00104 { 00105 pc.printf("Local echo disabled\r\n"); 00106 } 00107 /* Write to mem */ 00108 if(modem_command_check_ok("AT&W")) 00109 { 00110 pc.printf("Settings saved!\r\n"); 00111 } 00112 } 00113 00114 bool modem_command_check_ok(char * command) 00115 { 00116 /* first clear serial data buffers */ 00117 while(modem.readable()) modem.getc(); 00118 /* Timeout for response of the modem */ 00119 Timeout tmout; 00120 ser_timeout = false; 00121 /* Buffer for incoming data */ 00122 char responsebuffer[6]; 00123 /* Flag to set when we get 'OK' response */ 00124 bool ok = false; 00125 bool error = false; 00126 /* Print command to TD120x */ 00127 modem.printf(command); 00128 /* Newline to activate command */ 00129 modem.printf("\n"); 00130 /* Wait untill serial feedback, min 7 seconds before timeout */ 00131 tmout.attach(&sertmout, 7.0); 00132 while(!modem.readable()&& ser_timeout == false); 00133 while(!ok && !ser_timeout && !error) 00134 { 00135 if(modem.readable()) 00136 { 00137 for(int i = 0; i < 5; i++) 00138 { 00139 responsebuffer[i] = responsebuffer[i+1]; 00140 } 00141 responsebuffer[5] = modem.getc(); 00142 if(responsebuffer[0] == '\r' && responsebuffer[1] == '\n' && responsebuffer[2] == 'O' && responsebuffer[3] == 'K' && responsebuffer[4] == '\r' && responsebuffer[5] == '\n' ) 00143 { 00144 ok = true; 00145 } 00146 else if(responsebuffer[0] == '\r' && responsebuffer[1] == '\n' && responsebuffer[2] == 'E' && responsebuffer[3] == 'R' && responsebuffer[4] == 'R' && responsebuffer[5] == 'O' ) 00147 { 00148 error = true; 00149 } 00150 } 00151 } 00152 tmout.detach(); 00153 return ok; 00154 } 00155 00156 /* Button 1 ISR */ 00157 void sw1interrupt() 00158 { 00159 pc.printf("\n\rButton 1 pressed\n\r"); 00160 } 00161 00162 /* Button 2 ISR */ 00163 void sw2interrupt() 00164 { 00165 pc.printf("\n\rButton 2 pressed\n\r"); 00166 } 00167 00168 /* ISR for serial timeout */ 00169 void sertmout() 00170 { 00171 ser_timeout = true; 00172 } 00173 00174 00175
Generated on Tue Jul 26 2022 10:04:38 by
1.7.2
