-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.ino
79 lines (68 loc) · 1.59 KB
/
sketch.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
#include <TimerOne.h>
#define STOPPED 0
#define RUNNING 1
const unsigned int duty_cycle = 983; // over 1023
const int powerPin = 9;
const int stopLedPin = 3;
const int switchPin = 2;
const int voltPin = A0;
const int tempPin = A1;
const unsigned long periode = 125000;
float voltMaxThreshold = 1.70;
float voltMinThreshold = 1.20;
float tempThreshold = 40.0;
float voltBattery = 1.5;
float tempBattery = 20.0;
int switchState = 0;
int state = STOPPED;
unsigned long time;
void start_power() {
state = RUNNING;
Timer1.pwm(powerPin, duty_cycle);
digitalWrite(stopLedPin, LOW);
}
void stop_power() {
state = STOPPED;
Timer1.pwm(powerPin, 0);
digitalWrite(stopLedPin, HIGH);
}
void setup()
{
Serial.begin(9600);
pinMode(powerPin, OUTPUT);
pinMode(stopLedPin, OUTPUT);
pinMode(switchPin, INPUT);
Timer1.initialize(periode);
digitalWrite(stopLedPin, HIGH);
}
void loop()
{
voltBattery = analogRead(voltPin) / 1024.0 * 5.0;
tempBattery = analogRead(tempPin) / 1024.0 * 5.0;
tempBattery = (tempBattery - 0.5) * 100;
switchState = digitalRead(switchPin);
time = millis();
if (voltBattery > voltMaxThreshold || voltBattery < voltMinThreshold || tempBattery > tempThreshold) {
stop_power();
}
if (switchState == HIGH) {
if (state == STOPPED) {
start_power();
} else {
stop_power();
}
}
if (state == STOPPED) {
Serial.print(STOPPED);
} else {
Serial.print(RUNNING);
}
Serial.print("; ");
Serial.print(time);
Serial.print("; ");
Serial.print(voltBattery);
Serial.print("; ");
Serial.print(tempBattery);
Serial.println();
delay(250);
}