-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSEN-39002_Lightning_Emulator.ino
168 lines (156 loc) · 6.35 KB
/
SEN-39002_Lightning_Emulator.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/***************************************************************************
* File Name: SEN-39002_Lightning_Emulator.ino
* Processor/Platform: Arduino Uno R3 (tested)
* Development Environment: Arduino 1.6.5
*
* Designed for use with with Playing With Fusion Lightning "Emulator"
* (SEN-39002), which can be used with AMS Lightning Sensor AS3935 to test
* and develop your application.
*
* SEN-39002 (universal applications)
* ---> https://www.playingwithfusion.com/productview.php?pdid=55
* SEN-39001 (universal applications)
* ---> http://playingwithfusion.com/productview.php?pdid=22
* SEN-39003 (qwiic lightning sensor)
* ---> https://www.playingwithfusion.com/productview.php?pdid=135
*
* Copyright © 2017 Playing With Fusion, Inc.
* SOFTWARE LICENSE AGREEMENT: This code is released under the MIT License.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
* **************************************************************************
* REVISION HISTORY:
* Author Date Comments
* J. Steinlage 2017Jan17 Original release
* J. Steinlage 2024Feb25 Update to support additional I2C addresses
*
* Playing With Fusion, Inc. invests time and resources developing open-source
* code. Please support Playing With Fusion and continued open-source
* development by buying products from Playing With Fusion!
*
* **************************************************************************
* APPLICATION SPECIFIC NOTES (READ THIS!!!):
* - This code configures an Arduino to interface with SEN-39002
* - Configure Arduino (I2C, UART, etc)
* - Perform initial verification and configuration of the DAC
* - I2C specific: This example uses the I2C interface via the Wire library
*
* Circuit:
* Arduino Uno Arduino Mega --> SEN-39002
* SDA: SDA SDA --> SDA
* SCL: SCL SCL --> SCL
* GND: GND '' --> GND
* 5V: 5V '' --> VDD
* note1: VDD should match voltage of IO, and can be between 3.3 and 5V
* note2: this board has been tested with 5V Arduinos
* note3: download PWFusion_MCP4725 library from www.github.com/playingwithfusion to use with this code
**************************************************************************/
// I2C library - standard Wire lib
#include "Wire.h"
// include Playing With Fusion MCP4725 lib (https://github.com/PlayingWithFusion/PWFusion_MCP4725)
#include "PWFusion_MCP4725_12DAC.h"
// declare sensor object
#define DAC_ADD 0x62 // uses A1 version
#define DAC_ADD_ALT 0x64 // uses A2 version
PWFusion_MCP4725 dac0(DAC_ADD);
PWFusion_MCP4725 dac1(DAC_ADD_ALT);
void setup()
{
Serial.begin(115200);
Serial.println("Playing With Fusion: SEN-39002, Lightning Emulator Shield");
// set up DAC
dac0.begin();
dac1.begin();
// setup DAC, set DAC to 'off' and pulled low
// DAC output / write to NV memory / power down DAC, pull to gnd
dac0.setOutput(0,false,true);
dac1.setOutput(0,false,true);
// give the Arduino time to start up
delay(100);
pinMode(9, INPUT_PULLUP); // FAR PB
pinMode(8, OUTPUT); // FAR
digitalWrite(8, HIGH);
pinMode(7, INPUT_PULLUP); // MID PB
pinMode(6, OUTPUT); // MID
digitalWrite(6, HIGH);
pinMode(5, INPUT_PULLUP); // CLO PB
pinMode(4, OUTPUT); // CLO
digitalWrite(4, HIGH);
uint8_t su_tim = 50;
volatile uint8_t su_x = 4;
volatile uint8_t su_cnt;
// startup LED sequence
for(su_cnt = 0; su_cnt < su_x; su_cnt++){
digitalWrite(4, LOW);
delay(su_tim);
digitalWrite(4, HIGH);
digitalWrite(6, LOW);
delay(su_tim);
digitalWrite(6, HIGH);
digitalWrite(8, LOW);
delay(su_tim);
digitalWrite(8, HIGH);
digitalWrite(6, LOW);
delay(su_tim);
digitalWrite(6, HIGH);
}
digitalWrite(4, LOW);
delay(su_tim);
digitalWrite(4, HIGH);
}
// as calibrated, this profile works from around 7cm and 15cm from inductor to inductor
uint16_t out_array[20] = { 103, 73, 52, 37, 27, 20, 15, 11, 9, 7, 6, 5, 4, 4, 4, 3, 3, 3, 3, 3};
volatile uint8_t i, j, j_cnt;
void loop()
{
// close mid far
while(digitalRead(5) & digitalRead(7) & digitalRead(9)); // wait for any button push
if(!digitalRead(5)){ // close-range strike simulation requested
j_cnt = 3;
digitalWrite(4, LOW);
}
else if(!digitalRead(7)){ // mid-range strike simulation requested
j_cnt = 2;
digitalWrite(6, LOW);
}
else{ // far-range strike simulation requested
j_cnt = 1;
digitalWrite(8, LOW);
}
// now run emulation profile
for(j = 0; j < j_cnt; j++)
{
for(i = 0; i < 19; i++)
{
dac0.setOutput(out_array[i], false, false); // set new command value
dac1.setOutput(out_array[i], false, false); // set new command value
delayMicroseconds(30); // change drive every 30 microseconds
}
}
delay(20); // end by delaying 4 milliseconds
// turn off DAC
dac0.setOutput(0, true, true);
dac1.setOutput(0, true, true);
// hold state for one sec - give sensor time to pick up, report signal
delay(1000);
// turn off all LEDs
digitalWrite(4,HIGH);
digitalWrite(6,HIGH);
digitalWrite(8,HIGH);
}