/
main.c
189 lines (159 loc) · 3.86 KB
/
main.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* -----------------------------------------------------------------------
* Title: Social LED Bug
* Author: PINY
* Inspired by: Alexander Weber <alexander.weber.0 at gmail.com>
* Hardware: ATtiny13v
*/
// define as -D switch
//#define F_CPU 1200000 // Taktfrequenz: 1.2MHz, internal oscillator
#include <avr/io.h>
#include <util/delay.h>
#define TRUE 1
#define FALSE 0
#define ON 1
#define OFF 0
// use PB2 for led, pin 7
#define LED_BIT 2
// use PB3 to enable ldr, pin 2
#define ADC_ENA_BIT 3
// pause
#define PAUSE 25
// select ADC2, PB4, pin 3
#define CHANNEL 2
// values over threshold are recorded as "on"
//#define THRESHOLD 100
// storage for recorded values
#define MAX 400
static uint8_t values[MAX / 8];
/*
* get_adc
* Return the 10bit value of the selected adc channel.
*/
uint16_t get_adc(uint8_t channel) {
// enable voltage for adc
PORTB |= (1 << ADC_ENA_BIT);
// ADC setup
ADCSRA =
(1 << ADEN) | // enable ADC
(1 << ADPS1) | (1 << ADPS0); // set prescaler to 8
// select channel
ADMUX = channel;
// select reference voltage
// ADMUX |= (1 << REFS0); // use internal reference
// warm up the ADC, discard the first conversion
ADCSRA |= (1 << ADSC);
while (ADCSRA & (1 << ADSC));
ADCSRA |= (1 << ADSC); // start single conversion
while (ADCSRA & (1 << ADSC)); // wait until conversion is done
PORTB &= ~(1 << ADC_ENA_BIT); // disable voltage for adc
return ADCW;
}
/*
void turnOnLED(uint8_t delay_time) {
PORTB |= (1 << LED_BIT);
_delay_ms(delay_time);
}
void turnOffLED(uint8_t delay_time) {
PORTB &= ~(1 << LED_BIT);
_delay_ms(delay_time);
}
*/
int main(void) {
uint16_t i = 0;
uint16_t debug = 0;
uint16_t count = 0;
uint8_t light = OFF;
uint8_t last_light = OFF;
uint16_t last_time = 0;
uint8_t programming = TRUE;
uint16_t threshold = 0;
// define LED and ADC enable as outputs
DDRB |= (1 << LED_BIT) |
(1 <<ADC_ENA_BIT);
// compute threshold
for (i = 0; i < 4; i++) {
threshold += get_adc(CHANNEL);
_delay_ms(200);
_delay_ms(200);
_delay_ms(200);
_delay_ms(200);
_delay_ms(200);
}
threshold = threshold >> 2;
threshold += 40;
// intro
debug = (get_adc(CHANNEL) < threshold) ? 3 : 6;
for (i = 0; i < debug; i++) {
PORTB |= (1 << LED_BIT);
_delay_ms(200);
//turnOnLED(200);
PORTB &= ~(1 << LED_BIT);
_delay_ms(200);
//turnOffLED(200);
}
for (i = 0; i < 5; i++) {
_delay_ms(100);
}
while (1) {
if (programming) {
// signal that we are ready to program
for (i = 0; i < 5; i++) {
PORTB |= (1 << LED_BIT);
_delay_ms(40);
PORTB &= ~(1 << LED_BIT);
_delay_ms(40);
// turnOnLED(40);
// turnOffLED(40);
}
// now read the ldr and store it
for (i = 0; i < MAX; i++) {
if (get_adc(CHANNEL) > threshold) {
values[i / 8] |= (1 << (i % 8));
PORTB |= (1 << LED_BIT);
}
else {
values[i / 8] &= ~(1 << (i % 8));
PORTB &= ~(1 << LED_BIT);
}
_delay_ms(PAUSE);
}
// signal that we are finished with programming
for (i = 0; i < 7; i++) {
PORTB |= (1 << LED_BIT);
_delay_ms(40);
PORTB &= ~(1 << LED_BIT);
_delay_ms(40);
}
// switch to playback
programming = FALSE;
count = 0;
}
else {
// have we detected a change?
light = (get_adc(CHANNEL) > threshold) ? ON : OFF;
if (light != last_light) {
// when was the last change?
if ((count - last_time) == 10) {
programming = TRUE; // switch to programming mode
}
else {
last_time = count;
}
}
last_light = light;
// replay recorded lights
if (values[count / 8] & (1 << (count % 8))) {
PORTB |= (1 << LED_BIT);
}
else {
PORTB &= ~(1 << LED_BIT);
}
_delay_ms(PAUSE);
count++;
if (count == MAX) {
count = 0;
}
}
}
return 0;
}