Heptasat

Dependencies:   HEPTA_CDH HEPTA_EPS HEPTA_SENSOR mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "HEPTA_EPS.h"
00003 #include "HEPTA_SENSOR.h"
00004 #include "HEPTA_CDH.h"
00005 
00006 HEPTA_CDH cdh(p5, p6, p7, p8, "sd");
00007  
00008 Serial pc(USBTX, USBRX, 9600);
00009 HEPTA_EPS eps(p16,p26);
00010 HEPTA_SENSOR sensor(p17,
00011                   p28,p27,0x19,0x69,0x13,
00012                   p13, p14,p25,p24);
00013 
00014 AnalogIn mic(p15); //microphone input
00015 
00016 DigitalOut led1(LED1); // following leds are used to visualy show amplitude of sound
00017 DigitalOut led2(LED2);
00018 DigitalOut led3(LED3);
00019 DigitalOut led4(LED4);
00020 
00021 Timer sattime;
00022 
00023 void record(float* sampleArr) {
00024     //record sound for 1 second
00025     for(int i=0; i<4000; i++) {
00026         sampleArr[i] = mic.read(); //put samples in array
00027         wait(0.000125f); //sample rate of 8000 Hz
00028     }  
00029 }
00030 
00031 void volume(){
00032     //the following lights up the onboard LEDs depending on the amplitude of sound
00033     led1 = (mic > 0.5f) ? 1 : 0;
00034     led2 = (mic > 0.6f) ? 1 : 0;
00035     led3 = (mic > 0.7f) ? 1 : 0;
00036     led4 = (mic > 0.8f) ? 1 : 0;
00037 }
00038 
00039 int rcmd=1;
00040 
00041 int main() {
00042     float sampleArr[4000];
00043     sattime.start();
00044     float starttime=sattime.read();
00045     pc.printf("Start time: %f\r\n",starttime);
00046     pc.baud(9600);
00047     while(1){
00048         volume();
00049         if(rcmd == 1) {
00050             record(sampleArr);
00051             float endtime=sattime.read();
00052             pc.printf("The following data has been written:\r\n");
00053             for (int j=0; j<400; j++) {
00054                 if (j == 0){
00055                 pc.printf("%f = %f\r\n",starttime,sampleArr[j]);
00056                 }
00057                 else {
00058                 pc.printf("%f = %f\r\n",endtime,sampleArr[j]);
00059                 }
00060              }
00061             FILE *fp = fopen("/sd/mydir/test2.txt","w");  
00062             if(fp == NULL) {
00063                 error("Could not open file for write\r\n");
00064             }
00065             for (int j=0; j<400; j++) {
00066                 fprintf(fp,"%f\r\n",sampleArr[j]);
00067             }
00068             fclose(fp);
00069             wait(5);
00070             pc.printf("Writing data complete\r\n");
00071         }
00072     }
00073 }