-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPIO'4'Cars_v1.4_no_pwm
72 lines (59 loc) · 2.14 KB
/
GPIO'4'Cars_v1.4_no_pwm
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
/*
GPIO'4'Cars NO PWM
ATtiny85 Pin map
+-\/-+
NC PB5 1|O |8 VCC
NC PB3 2| |7 PB2 A1 input_5v
ouput_power 4 PB4 3| |6 PB1 1 input_12v
GND 4| |5 PB0 NC
+----+
*/
const int pin_input_5v = A1;
const int pin_input_12v = 1;
const int pin_output_power = 4;
const int analog_5v_value = 615; // When N2 powered is of, 5v drops to 2.1V, so we compare to around 3V with the 615 analog value (0 to 1023 range)
int input_5v = 0;
bool input_12v = false;
bool power_output = false;
bool old_power_output = false;
void setup() {
pinMode(pin_input_5v, INPUT);
pinMode(pin_input_12v, INPUT);
pinMode(pin_output_power, OUTPUT);
clock_low();
}
void loop() { // Set output_power pin HIGH if input_12v OR input_5v are HIGH
input_5v = analogRead(pin_input_5v);
input_12v = digitalRead(pin_input_12v);
if ((input_12v == true) || (input_5v > analog_5v_value)) { // input_12v OR input_5v are HIGH
power_output = true;
} else {
delay(100); // DEBOUNCING
input_5v = analogRead(pin_input_5v);
input_12v = digitalRead(pin_input_12v);
if ((input_12v == true) || (input_5v > analog_5v_value)) {
power_output = true;
} else {
power_output = false;
}
}
if (power_output != old_power_output) {
if (power_output == true) {
// clock_high();
digitalWrite(pin_output_power, HIGH); // ENABLE POWER
} else {
delay(1000); // DELAY BEFORE SHUTDOWN
digitalWrite(pin_output_power, LOW); // DISABLE POWER
// clock_low();
}
old_power_output = power_output;
}
}
// void clock_high() {
// CLKPR = (1<<CLKPCE); // Prescaler enable
// CLKPR = ((0<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) | (1<<CLKPS0)); // Clock division factor by 2 (0001)
// }
void clock_low() {
CLKPR = (1<<CLKPCE); // Prescaler enable
CLKPR = ((0<<CLKPS3) | (1<<CLKPS2) | (0<<CLKPS1) | (0<<CLKPS0)); // Clock division factor by 16 (0100)
}