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
| // ATMEL ATTINY13a μC
//
// +-\/-+
// RESET--ACD0--5/A0--PCINT5--PB5 1| |8 VCC
// CLKI--ACD3--3/A3--PCINT3--PB3 2| |7 PB2--PCINT2--2/A1--SCK--ADC1
// ACD2--4/A2--PCINT4--PB4 3| |6 PB1--PCINT1---1---MISO--OCOB--INT0*
// GND 4| |5 PB0--PCINT0---0---MOSI--OCOA*
// +----+
// * indicates PWM port
#include <avr/interrupt.h> // for interrupt routines
#include <avr/sleep.h> // the sleep routines
#define InterruptPin PB4 // A2 interrupt pin
volatile uint16_t lastpins = 0b00000000; // store last lights so they are not the same
volatile uint16_t whichpins = 0b00001111; // storage for current lights
volatile uint16_t myrand = 2901; // happy birthday
// generate a "random" number between small and big
uint16_t gimmerand(uint16_t small, uint16_t big) {
myrand ^= (myrand << 13);
myrand ^= (myrand >> 9);
myrand ^= (myrand << 7);
return abs(myrand)%23*(big-small)/23+small;
}
// gets a new set of random lights, then displays and stores them
void flickthem() {
while (lastpins == whichpins) {
whichpins = gimmerand(1, 15);
}
PORTB = whichpins;
lastpins = whichpins;
delay(50);
}
// waits for button input, then grabs the new lights
void waitforswitch(void) {
sei(); // enable global interrupts
set_sleep_mode(SLEEP_MODE_IDLE); // sleep little one
sleep_enable(); // enable sleep mode
sleep_mode(); // system sleeps here
sleep_disable(); // ISR routine returns here so wake up
delay(50); // settle time
flickthem(); // change lights please
}
// Interrupt
ISR(PCINT0_vect)
{
// wake up little one...
}
void setup() {
GIMSK |= (1<<PCIE); // activate Pin change interrupts
PCMSK |= (1<<InterruptPin); // sets the Pin change interrupt mask
DDRB = 0b00000000; // make all Pins input
// read three pins and add for randomseed
myrand = analogRead(A0)+analogRead(A1)+analogRead(A2);
DDRB = 0b00001111; // set output pins for light
flickthem(); // alive! all pins on to start with (setup check)
}
void loop() {
// put your main code here, to run repeatedly:
waitforswitch(); // sleep and then wait for a button
}
|
No comments:
Post a Comment