/
Example1_BasicLightning_SPI.ino
87 lines (74 loc) · 2.67 KB
/
Example1_BasicLightning_SPI.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
This example demonstrates how to detect lightning! It has a few basic
settings to help with rejecting noise or "disturbers" (false lightning events).
It uses the onboard interrupt hardware pin, so in addition to attaching to
it data lines you'll need to connnect to the interrupt pin labled "INT".
By: Elias Santistevan
SparkFun Electronics
Date: May, 2019
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
*/
#include <SPI.h>
#include <Wire.h>
#include "SparkFun_AS3935.h"
#define INDOOR 0x12
#define OUTDOOR 0xE
#define LIGHTNING_INT 0x08
#define DISTURBER_INT 0x04
#define NOISE_INT 0x01
SparkFun_AS3935 lightning;
// Interrupt pin for lightning detection
const int lightningInt = 4;
int spiCS = 10; //SPI chip select pin
// This variable holds the number representing the lightning or non-lightning
// event issued by the lightning detector.
int intVal = 0;
int noise = 2; // Value between 1-7
int disturber = 2; // Value between 1-10
void setup()
{
// When lightning is detected the interrupt pin goes HIGH.
pinMode(lightningInt, INPUT);
Serial.begin(115200);
Serial.println("AS3935 Franklin Lightning Detector");
SPI.begin();
if( !lightning.beginSPI(spiCS) ){
Serial.println ("Lightning Detector did not start up, freezing!");
while(1);
}
else
Serial.println("Schmow-ZoW, Lightning Detector Ready!");
// The lightning detector defaults to an indoor setting at
// the cost of less sensitivity, if you plan on using this outdoors
// uncomment the following line:
//lightning.setIndoorOutdoor(OUTDOOR);
}
void loop()
{
// Hardware has alerted us to an event, now we read the interrupt register
if(digitalRead(lightningInt) == HIGH){
intVal = lightning.readInterruptReg();
if(intVal == NOISE_INT){
Serial.println("Noise.");
// Too much noise? Uncomment the code below, a higher number means better
// noise rejection.
//lightning.setNoiseLevel(noise);
}
else if(intVal == DISTURBER_INT){
Serial.println("Disturber.");
// Too many disturbers? Uncomment the code below, a higher number means better
// disturber rejection.
//lightning.watchdogThreshold(disturber);
}
else if(intVal == LIGHTNING_INT){
Serial.println("Lightning Strike Detected!");
// Lightning! Now how far away is it? Distance estimation takes into
// account any previously seen events in the last 15 seconds.
byte distance = lightning.distanceToStorm();
Serial.print("Approximately: ");
Serial.print(distance);
Serial.println("km away!");
}
}
delay(100); // Slow it down.
}