I'm almost there..
I'm trying to make simon game with attiny 13a
Here is the code that I found on github.
The guy uses the same attiny13a but I don't know why it desont work with mine
Anyway to make it fit inside attiny 13a
#include <avr/sleep.h>
#include <util/delay_basic.h>
#include <avr/eeprom.h>
const uint8_t buttons[4] = {
0b00001010, 0b00000110, 0b00000011, 0b00010010
};
const uint8_t tones[4] = {
239, 179, 143, 119
};
uint8_t lastKey;
uint8_t lvl = 0;
uint8_t maxLvl;
uint16_t ctx;
uint16_t seed;
volatile uint8_t nrot = 8;
volatile uint16_t time;
void sleepNow() {
PORTB = 0b00000000;
cli();
WDTCR = 0;
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_cpu();
}
void play(uint8_t i, uint16_t t = 45000) {
PORTB = 0b00000000;
DDRB = buttons[i];
OCR0A = tones[i];
OCR0B = tones[i] >> 1;
TCCR0B = (1 << WGM02) | (1 << CS01);
_delay_loop_2(t);
TCCR0B = 0b00000000;
DDRB = 0b00000000;
PORTB = 0b00011101;
}
void gameOver() {
for (uint8_t i = 0; i < 4; i++) {
play(3 - i, 25000);
}
if (lvl > maxLvl) {
eeprom_write_byte((uint8_t*) 0, ~lvl);
eeprom_write_byte((uint8_t*) 1, (seed >> 8));
eeprom_write_byte((uint8_t*) 2, (seed & 0b11111111));
for (uint8_t i = 0; i < 3; i++) {
levelUp();
}
}
sleepNow();
}
void levelUp() {
for (uint8_t i = 0; i < 4; i++) {
play(i, 25000);
}
}
uint8_t simple_random4() {
ctx = 2053 * ctx + 13849;
uint8_t temp = ctx ^ (ctx >> 8);
temp ^= (temp >> 4);
return (temp ^ (temp >> 2)) & 0b00000011;
}
ISR(WDT_vect) {
time++;
if (nrot) {
nrot--;
seed = (seed << 1) ^ TCNT0;
}
}
void resetCtx() {
ctx = seed;
}
int main(void) {
PORTB = 0b00011101;
ADCSRA |= (1 << ADEN);
ADCSRA |= (1 << ADSC);
while (ADCSRA & (1 << ADSC));
seed = ADCL;
ADCSRA = 0b00000000;
WDTCR = (1 << WDTIE);
sei();
TCCR0B = (1 << CS00);
while (nrot);
TCCR0A = (1 << COM0B1) | (0 << COM0B0) | (0 << WGM01) | (1 << WGM00);
maxLvl = ~eeprom_read_byte((uint8_t*) 0);
switch (PINB & 0b00011101) {
case 0b00010101:
eeprom_write_byte((uint8_t*) 0, 255);
maxLvl = 0;
break;
case 0b00001101:
lvl = 255;
break;
case 0b00011001:
lvl = maxLvl;
case 0b00011100:
seed = (((uint16_t) eeprom_read_byte((uint8_t*) 1)) << 8) | eeprom_read_byte((uint8_t*) 2);
break;
}
while (1) {
resetCtx();
for (uint8_t cnt = 0; cnt <= lvl; cnt++) {
_delay_loop_2(4400 + 489088 / (8 + lvl));
play(simple_random4());
}
time = 0;
lastKey = 5;
resetCtx();
for (uint8_t cnt = 0; cnt <= lvl; cnt++) {
bool pressed = false;
while (!pressed) {
for (uint8_t i = 0; i < 4; i++) {
if (!(PINB & buttons[i] & 0b00011101)) {
if (time > 1 || i != lastKey) {
play(i);
pressed = true;
uint8_t correct = simple_random4();
if (i != correct) {
for (uint8_t j = 0; j < 3; j++) {
_delay_loop_2(10000);
play(correct, 20000);
}
_delay_loop_2(65536);
gameOver();
}
time = 0;
lastKey = i;
break;
}
time = 0;
}
}
if (time > 4000) {
sleepNow();
}
}
}
_delay_loop_2(65536);
if (lvl < 254) {
lvl++;
levelUp();
_delay_loop_2(45000);
}
else {
levelUp();
gameOver();
}
}
}