![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
basic lightning detector with gps and sd card logging
Dependencies: AS3935 AdafruitGPS SDFileSystem TSI mbed ConfigFile
Diff: main.cpp
- Revision:
- 3:e3974328d808
- Parent:
- 2:3edb129c60b2
- Child:
- 4:4d26ba1ae0f7
diff -r 3edb129c60b2 -r e3974328d808 main.cpp --- a/main.cpp Tue Jun 23 21:01:40 2015 +0000 +++ b/main.cpp Wed Jun 24 12:37:31 2015 +0000 @@ -5,7 +5,7 @@ #include "SDFileSystem.h" #include "AS3935.h" - // frdm-kl25z sd card connections for spi1 + // frdm-kl25z as3935 connections for spi1 // ------------------------------------------------ // Header -- kl25z -- SD/MMC // J2-20 -- PTE1 -- MOSI @@ -36,43 +36,21 @@ DigitalOut red(LED_RED); DigitalOut green(LED_GREEN); //DigitalOut blue(LED_BLUE); don't use the blue led, due to a board error, writing to the blue led kills spi -bool debug; +bool debug=false; int day, month,year,hour,minute,seconds; void writeLogFile(int interruptSource, int distance, long energy); char logName[]="lightning_data.csv"; int rdistance, rinterrupt; char directory[]="/sd/lightning_data"; +int OriginInt=-1; +int distance=-1; +long energy=-1; void DetectLightning() { - int OriginInt; - int distance=-1; - long energy =-1; - wait_ms(2); // OriginInt = ld.interruptSource(); - pc.printf("%02d/%02d/20%02d_%02d:%02d:%02d ",month,day,year,hour,minute,seconds); - switch (OriginInt) - { - case 1: - pc.printf("Noise level too high\r\n"); - break; - case 4: - pc.printf("Disturber\r\n"); - break; - case 8: - distance = ld.lightningDistanceKm(); - energy = ld.getEnergy(); - pc.printf("Lightning detection, distance=%dkm energy=%ld\r\n", distance, energy); - - ld.clearStats(); - break; - default: - pc.printf("Unknown interrupt %d\r\n", OriginInt); - - } - writeLogFile(OriginInt,distance, energy); - pc.printf("Resume wait for lightning detection...\r\n"); - + distance = ld.lightningDistanceKm(); + energy = ld.getEnergy(); } void writeLogFile(int interruptSource, int distance, long energy) @@ -86,7 +64,7 @@ fp = fopen(logFilePath, "a"); if(fp == NULL) { // retry - wait_ms(500); + wait_ms(200); fp = fopen(logFilePath, "a"); if (fp == NULL) { @@ -96,7 +74,8 @@ return; } } - pc.printf("Opened log file %s\r\n",logFilePath); + if (debug) + pc.printf("Opened log file %s\r\n",logFilePath); // write the log file header if (header == false) { @@ -116,12 +95,30 @@ f_sync((FIL*)fp); fclose(fp); sd.unmount(); - pc.printf("Closed log file %s\r\n",logFilePath); + pc.printf("Event: "); + switch (interruptSource) + { + case 1: + pc.printf("Noise level too high\r\n"); + break; + case 4: + pc.printf("Disturber\r\n"); + break; + case 8: + pc.printf("Lightning detection, distance=%dkm energy=%ld\r\n", distance, energy); + ld.clearStats(); + break; + default: + pc.printf("Unknown interrupt %d\r\n", OriginInt); + + } + + if (debug) + pc.printf("Closed log file %s\r\n",logFilePath); } int main() { - bool debug=false; char c; Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info? const int refresh_Time = 1000; //refresh time in ms @@ -144,7 +141,7 @@ gpsd.seconds=1; red = 1; green = 1; - pc.printf("Touch slider to start application\r\n"); + pc.printf("Touch slider to start lightning detector application\r\n"); while(1) { green = 1; // turn led off wait_ms(200); @@ -156,7 +153,7 @@ break; } - pc.printf("\r\nstart lightning detector\r\n"); + pc.printf("\r\nInitialize lightning detector\r\n"); //initializations for lightning detector ld.init(); ld.clearStats(); @@ -182,7 +179,7 @@ int SpikeRej = ld.getSpikeRejection(); int WatchDog = ld.getWatchdogThreshold(); - pc.printf(" Min wylad: %i", MinBlysk); + pc.printf("\r\n Min wylad: %i", MinBlysk); pc.printf("\r\n"); pc.printf(" Gain: 0x%02x\r\n",ld.getGain()); pc.printf(" Noise: %i", Noise); @@ -203,9 +200,20 @@ sd.unmount(); bool gpsFix=false; - pc.printf("Starting GPS App\r\n"); while (1) { + if (OriginInt != -1) + { + // the ld detector generated an interrupt, log the event + IntLightning.disable_irq(); + writeLogFile(OriginInt,distance, energy); + ld.clearStats(); + OriginInt = -1; + distance = -1; + energy = -1; + IntLightning.enable_irq(); + } + c = gpsd.read(); //queries the GPS if (debug) { @@ -252,6 +260,7 @@ } else { + gpsFix = false; pc.printf("Waiting for GPS FIX\r\n"); red = 0; // turn led on }