-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpower.ino
More file actions
127 lines (111 loc) · 3.68 KB
/
power.ino
File metadata and controls
127 lines (111 loc) · 3.68 KB
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
const float current_sense_coef = 1.0;
const unsigned int poll_length = 5;
unsigned long accumulator[NUM_PORTS];
unsigned int step_num[NUM_PORTS];
float power_correction_factor = -20;
const int analog_current_sense_pin[NUM_PORTS] = {A0,A1};
const int relay_control_pin[NUM_PORTS] = {2,5};
const int relay_led_pin[NUM_PORTS] = {3,6};
const int socket_button_pin[NUM_PORTS] = {4,7};
const int led_control_pin[NUM_LEDS] = {8,9};
byte relay_status[NUM_PORTS];
byte led_status[NUM_PORTS];
long last_debounce_time[NUM_PORTS] = {0}; // the last time the output pin was toggled
int last_button_state[NUM_PORTS] = {LOW};
const long debounceDelay = 50;
void setup_relay_output() {
for (int i=0; i<NUM_PORTS; i++) {
pinMode(relay_control_pin[i], OUTPUT);
pinMode(relay_led_pin[i],OUTPUT);
pinMode(socket_button_pin[i], INPUT);
digitalWrite(relay_control_pin[i], LOW);
digitalWrite(relay_led_pin[i],LOW);
relay_status[i] = 0;
}
}
void setup_led_output() {
for (int i=0; i<NUM_PORTS; i++) {
pinMode(led_control_pin[i], OUTPUT);
digitalWrite(led_control_pin[i], LOW);
led_status[i] = 0;
}
}
int get_relay(int socket) {
if (socket >= NUM_PORTS) {
return -1;
}
return relay_status[socket];
}
int set_relay(int socket, int status) {
if (socket >= NUM_PORTS) {
return -1;
}
if (status == 0) {
digitalWrite(relay_control_pin[socket],LOW);
digitalWrite(relay_led_pin[socket],LOW);
relay_status[socket] = 0;
} else {
digitalWrite(relay_control_pin[socket],HIGH);
digitalWrite(relay_led_pin[socket],HIGH);
relay_status[socket] = 1;
}
return 0;
}
int get_led(int led) {
if (led >= NUM_LEDS) {
return -1;
}
return led_status[led];
}
int set_led(int led, int status) {
if (led >= NUM_LEDS) {
return -1;
}
if (status == 0) {
digitalWrite(led_control_pin[led],LOW);
led_status[led] = 0;
} else {
digitalWrite(led_control_pin[led],HIGH);
led_status[led] = 1;
}
return 0;
}
float measure_power(int socket) {
float power;
power = accumulator[socket]/step_num[socket];
power = 512 - power; //move down half
power = power*power_correction_factor;
accumulator[socket] = 0; //reset the accumulator
step_num[socket] = 0;
return power;
}
void calibrate_voltage() {
//TODO: get voltage from analog pin 1 and set voltage correction gain
}
void poll_current_sense(int socket) {
//TODO: set mux chip to correct socket (0-3)
for (int i=0; i<poll_length; i++) //poll the socket for enough time to capture a few periods
{
if (step_num[socket] != 65536) {
accumulator[socket] += analogRead(analog_current_sense_pin[socket]); //read new analog value
step_num[socket] += 1;
delay(2);
} else { //accumulator has reached step limit, reset
step_num[socket] = 0;
accumulator[socket]=0;
return;
}
}
}
void poll_relay_button(int socket) {
int state = digitalRead(socket_button_pin[socket]);
if (state == HIGH && last_button_state[socket] == LOW && millis() - last_debounce_time[socket] > debounceDelay) {
if (relay_status[socket] == 1) {
set_relay(socket,0);
} else {
set_relay(socket,1);
}
last_debounce_time[socket] = millis();
}
last_button_state[socket] = state;
}