This repository has been archived by the owner on May 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fireflies.c
227 lines (196 loc) · 4.84 KB
/
fireflies.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
Fireflies v.1
Tiny85
7/9/09
Kyle Anderson
*/
#include<avr/io.h>
#include<avr/interrupt.h>
#include<stdlib.h>
#include <stdint.h>
#include <avr/wdt.h>
#include <stdbool.h>
#include <avr/sleep.h>
#define NumberOfFlys 12
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
typedef unsigned char u08;
int temp;
void setrandom(void);
int makedecision(int fly);
void randomizepersonality(void);
void randomplay(void);
void alloff(void);
void allon(void);
int analogRead(void);
int lightness_counter = 0;
void testpattern(void);
void delay(int var) //delay of 10ms for var=1
{
unsigned char il, jl, kl;
for (il=0; il<var; il++)
for (jl=0; jl<32;jl++)
for (kl=0; kl<249; kl++)
asm("NOP");
}
volatile u08 j=1;
volatile u08 l;
volatile u08 anode[NumberOfFlys]={0,0,0,1,1,1,3,3,3,4,4,4};
volatile u08 cathode[NumberOfFlys]={1,3,4,0,3,4,0,1,4,0,1,3};
volatile int flystatus[NumberOfFlys]={0,0,0,0,0,0,0,0,0,0,0,0};
volatile int currentstate[NumberOfFlys]={0,0,0,0,0,0,0,0,0,0,0,0};
volatile int brightness[NumberOfFlys]={0,0,0,0,0,0,0,0,0,0,0,0};
volatile long activity = -1000;
long lightvalue=0;
volatile long tick=0;
int main(void);
volatile u08 pwm[12];
u08 call(u08 n);
uint8_t mcusr_mirror __attribute__ ((section (".noinit")));
void get_mcusr(void) \
__attribute__((naked)) \
__attribute__((section(".init3")));
void get_mcusr(void)
{
mcusr_mirror = MCUSR;
MCUSR = 0;
wdt_disable();
}
void check_if_sleepy(void);
int main()
{
get_mcusr(); //Why did we wake up?
ADCSRA=0b10000000; //ADC Enable,ADC Start Conversion,division factor 128,Auto trigger enable
sbi(MCUCR, PUD);
// PB0, PB1, PB3, PB4 are output
// PB2 is the sensor
//setup
TCCR0A = 0x00; //Timer/Counter Control Register A (normal operation 0)
TCCR0B = 0x05; //Timer/Counter Control Register B (2 = clkI/O/8 (From prescaler))
TIMSK = 0x02; //Timer/Counter Interrupt Mask Register (2 = Timer/Counter1 Overflow Interrupt Enable)
DDRB = 0x1b; //Port B Data Direction Register
//1e = DDB4 DDB3 DDB2 DDB1 are outputs?
PORTB = 0x1b; //Port B Data Register
//1e = DDB4 DDB3 DDB2 DDB1 are inputs?
//DD PO IO Pullup Comment
//1 1 Output No Output High (Source) (page 57)
sei(); //Set Interrupst enabled
// testpattern();
wdt_enable(WDTO_8S);
for(;;)
{
check_if_sleepy();
for(int j = 0; j < NumberOfFlys; j++)
{
if (flystatus[j] == 1)
{
if (brightness[j] < 500) brightness[j]+=10;
}
else //wantedstate=0
{
if (brightness[j] > 0) brightness[j]-=10;
}
if (brightness[j] > (rand() % 512))
{
currentstate[j]=1;
}
else
{
currentstate[j]=0;
}
DDRB = 0;
PORTB = 1<<anode[j]|~currentstate[j]<<cathode[j];
DDRB = 1<<anode[j]|1<<cathode[j];
}
wdt_reset();
}//End Loop
}//End Main
//Our Timer Interrupt
ISR(TIM0_OVF_vect)
{
tick++;
if (tick % 5== 0)
{
if ( analogRead() < 300 ) //This may need to be adjusted for your particular sensor
{
lightness_counter=0;
//activity = activity + 100;
activity = activity + ( rand() % 10 );
if (activity >= 100)
activity=( 100 - rand () % 1000);
}
else
{
if (lightness_counter < 100)
lightness_counter++;
activity -= 100;
if (activity > -1000)
activity=-1000;
}
randomplay();
}
}
void alloff(){
for (int fly = 0; fly < NumberOfFlys; fly++)
{
flystatus[fly]=0;
}
}
void allon(){
for (int fly = 0; fly < NumberOfFlys; fly++)
{
flystatus[fly]=1;
}
}
void randomplay(){
long decide = 0;
for (int fly = 0; fly < NumberOfFlys; fly++)
{
//decide = ((rand() % 1200) - ( rand() % abs(1000-activity)) + activity );
decide = ((rand() % 1000 ) + activity );
// decide = 900;
// int decide = random(20) + personality[fly];
if (decide < 800 )
flystatus[fly]=0;
if (decide > 500 )
flystatus[fly]=1;
}
}
int analogRead(void)
{
uint8_t low, high;
ADMUX = 0b00000001;
sbi(ADCSRA, ADSC);
while (bit_is_set(ADCSRA, ADSC));
low = ADCL;
high = ADCH;
return (high << 8) | low;
}
void check_if_sleepy(){
bool readyforbed = true;
for(int j = 0; j < NumberOfFlys; j++)
{
if ( flystatus[j] == 1 )
readyforbed=false;
}
if ( lightness_counter < 10 )
readyforbed=false;
cli();
if ( readyforbed==true )
{
DDRB = 0;
// PORTB = 1<<anode[0]|0<<cathode[0];
// DDRB = 1<<anode[0]|1<<cathode[0];
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_mode();
sleep_enable();
sei();
sleep_cpu();
sleep_disable();
}
sei();
}