-
Notifications
You must be signed in to change notification settings - Fork 0
/
Master-Slave switch.ino
52 lines (45 loc) · 1.88 KB
/
Master-Slave switch.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
#include <Filters.h>
#include <RunningStatistics.h>
float testFrequency = 60; // test signal frequency (Hz)
float windowLength = 20.0/testFrequency; // how long to average the signal, for statistist
int sensorValue = 0;
float intercept = -0.1129; // to be adjusted based on calibration testing
float slope = 0.0405; // to be adjusted based on calibration testing
float current_amps; // estimated actual current in amps
const int relayPin = 2; // Digital pin for relay module
bool relayState = false; // Keeps track of relay state
unsigned long printPeriod = 1000; // in milliseconds
// Track time in milliseconds since last reading
unsigned long previousMillis = 0;
void setup() {
Serial.begin( 57600 ); // start the serial port
// Initialize the relay pin as an output
pinMode(relayPin, OUTPUT);
}
void loop() {
RunningStatistics inputStats; // create statistics to look at the raw test signal
inputStats.setWindowSecs( windowLength );
while( true ) {
sensorValue = analogRead(A0); // read the analog in value:
inputStats.input(sensorValue); // log to Stats function
if((unsigned long)(millis() - previousMillis) >= printPeriod) {
previousMillis = millis(); // update time
// display current values to the screen
Serial.print( "\n" );
// output sigma or variation values associated with the inputValue itsel
Serial.print( "\tsigma: " ); Serial.print( inputStats.sigma() );
// convert signal sigma value to current in amps
current_amps = intercept + slope * inputStats.sigma();
Serial.print( "\tamps: " ); Serial.print( current_amps );
}
// Check if current exceeds 0.1A (100mA)
if (current_amps > 0.1) {
digitalWrite(relayPin, HIGH);
relayState = true;
}
else {
digitalWrite(relayPin, LOW);
relayState = false;
}
}
}