-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattiny85simon.ino
130 lines (110 loc) · 2.87 KB
/
attiny85simon.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
#include <avr/sleep.h>
#define BODS 7 //BOD Sleep bit in MCUCR
#define BODSE 2 //BOD Sleep enable bit in MCUCR
int level = 0;
int pattern[100];
const int BUTTON_WAIT = 0;
const int BUTTON_DOWN = 1;
const int BUTTON_UP = 2;
const int BUTTONS_PIN = A3;
void setup() {
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(BUTTONS_PIN, INPUT);
randomSeed(analogRead(0));
//gameOver(5);
//delay(3000);
}
void loop() {
level ++;
// make pattern
for(int i=level-1; i<level; i++) {
pattern[i] = random(1);
flash(ledFor(pattern[i]));
}
// play
for(int i=0; i<level; i++) {
int button = getButtonPress();
if(button != pattern[i]) {
gameOver(level);
delay(3000);
level = 0;
break;
}
}
// next level!
delay(1000);
}
void gameOver(int level) {
for(int i=0; i<level; i++) {
on(ledFor(0));
on(ledFor(1));
on(ledFor(2));
delay(500);
off(ledFor(0));
off(ledFor(1));
off(ledFor(2));
delay(300);
}
}
void on(int pin) {
digitalWrite(pin, HIGH);
}
void off(int pin) {
digitalWrite(pin, LOW);
}
void flash(int pin) {
on(pin);
delay(200);
off(pin);
delay(300);
}
int ledFor(int buttonValue) {
return buttonValue;
}
int getButtonValue() {
goToSleep();
return (analogRead(BUTTONS_PIN)*3+512)/1024; // http://www.technoblogy.com/show?LSE
}
int getButtonPress() {
// read the state of the pushbutton value:
int buttonState = BUTTON_WAIT;
int buttonValue = 0;
while(buttonState != BUTTON_UP) {
if(buttonState == BUTTON_WAIT) {
if(buttonValue == 0) {
buttonValue = getButtonValue();
} else {
buttonState = BUTTON_DOWN;
on(ledFor(buttonValue));
}
} else if(buttonState == BUTTON_DOWN && getButtonValue() == 0) {
buttonState = BUTTON_UP;
off(ledFor(buttonValue));
}
}
buttonState = BUTTON_WAIT;
return buttonValue;
}
// power saving, from https://gist.github.com/JChristensen/5616922
void goToSleep(void)
{
GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts
PCMSK |= _BV(PCINT3); // Use PB3 as interrupt pin
ADCSRA &= ~_BV(ADEN); // ADC off
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // replaces above statement
sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
sei(); // Enable interrupts
sleep_cpu(); // sleep
cli(); // Disable interrupts
PCMSK &= ~_BV(PCINT3); // Turn off PB3 as interrupt pin
sleep_disable(); // Clear SE bit
ADCSRA |= _BV(ADEN); // ADC on
sei(); // Enable interrupts
}
//external interrupt 0 wakes the MCU
ISR(PCINT0_vect)
{
// yawn, drink tea
}