-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsensor_head.ino
157 lines (133 loc) · 2.89 KB
/
sensor_head.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
#include <Wire.h>
#include <avr/sleep.h>
/*
Make Sure to Burn Bootloader before trying program a new board!
*/
#define I2C_ADDRESS 1
#define LED A0
#define DIODE A1
#define LASER 4
int state = 0;
int counter = 0;
uint8_t data[10];
void setup() {
//set pinmodes
pinMode(LED, OUTPUT);
pinMode(LASER, OUTPUT);
pinMode(DIODE, INPUT);
digitalWrite(LED, HIGH);
//setup i2c
Wire.begin(I2C_ADDRESS);
Wire.onRequest(requestEvent);
Wire.onReceive(receiveEvent);
delay(1000);
digitalWrite(LED, LOW);
}
/**
* State Machine
*
* State 0 -> go to deep sleep
* State 1 -> sample ADC
* State 2 -> turn on laser
* State 3 -> turn off laser
* State 4 -> turn on green led
* State 5 -> turn off green led
*
*/
void loop() {
switch (state) {
case 1:
takeSingleSample();
//request ADC data to leave state 1
break;
case 2:
counter = 0;
digitalWrite(LASER, HIGH);
state = 0;
break;
case 3:
counter = 0;
digitalWrite(LASER, LOW);
state = 0;
break;
case 4:
counter = 0;
digitalWrite(LED, HIGH);
state = 0;
break;
case 5:
counter = 0;
digitalWrite(LED, LOW);
state = 0;
break;
default:
if(counter > 2000){
//go to sleep
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable();
digitalWrite(LED, LOW);
digitalWrite(LASER, LOW);
sleep_cpu();
//wake up
sleep_disable();
counter = 0;
TWCR = bit(TWEN) | bit(TWIE) | bit(TWEA) | bit(TWINT);// release I2C
Wire.begin(I2C_ADDRESS);
counter = 0;
}
delay(1);
counter ++;
break;
}
}
void requestEvent() {
if (state == 1) {
Wire.write(data, 2);
state = 0;
}
}
void receiveEvent(int howMany) {
if (howMany == 1) {
state = Wire.read();
}
}
void takeSample(int samples) {
uint32_t sample_val = 0;
for (int i = 0; i < samples; i ++) {
delayMicroseconds(100);
sample_val += analogRead(DIODE);
}
sample_val /= samples;
data[0] = (sample_val >> 8) & 0xFF;
data[1] = sample_val & 0xFF;
}
void takeSingleSample(){
uint32_t sample_val = analogRead(DIODE);
data[0] = (sample_val >> 8) & 0xFF;
data[1] = sample_val & 0xFF;
}
//void takeSample(int samples) {
// uint32_t local_off = 0;
// uint32_t local_on = 0;
//
// digitalWrite(LASER, LOW);
// digitalWrite(LED, LOW);
// for (int i = 0; i < samples; i ++) {
// local_off += analogRead(DIODE);
// }
// digitalWrite(LASER, HIGH);
// digitalWrite(LED, HIGH);
// delay(500);
// for (int i = 0; i < samples; i ++) {
// local_on += analogRead(DIODE);
// }
// digitalWrite(LASER, LOW);
// digitalWrite(LED, LOW);
// local_off /= samples;
// local_on /= samples;
//
// data[0] = (local_off >> 8) & 0xFF;
// data[1] = local_off & 0xFF;
// data[2] = (local_on >> 8) & 0xFF;
// data[3] = local_on & 0xFF;
//}