Sketch uses 1040 bytes (101%) of program storage space. Maximum is 1024 bytes

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(); 
    }
  }
}

There probably is, but i a few things first. This has probably been written with a different core in mind.
2nd.
The compiler complains about

_delay_loop_2(65536);

and rightly so, it takes a 16-bit unsigned integer, and the maximum value is 65535. 65536 comes down to 0
There is also a fall thru within the switch case, but that may be intentional.

I would start by re-writing it to the normal Arduino dialect, then see if the util/delay_basic.h is actually more consuming than using delay();

The other obvious thing to do first is to remove the high-score & seed eeprom retaining, that should free up plenty. a more common way to get a seed for the random generator is to read the ADC on a floating pin.
(tried that already and it fits with ease then.)

You could give this one a try.

It compiles to 950 bytes in microcore.

Thanks so much, it works but it act weirdly, check the video below:

I'm using this schematic that I found, But using 220ohm resisters and all LED is red
Maybe the buttons are floating or something? but How he did it without making the buttons flouting?

Here us the schematic and pics of the board:



1847461481642150420

Thanks so much, it works but it act weirdly, check the video below:

I'm using this schematic that I found, But using 220ohm resisters and all LED is red
Maybe the buttons are floating or something? but How he did it without making the buttons flouting?

Here us the schematic and pics of the board:



1847461481642150420

Here is the article about this circuit

I see there are precompiled hex files on that page, so just load those in the Attiny13.

The code is working, I removed the cabasittor and magically everything working fine :thinking:

Thanks so much dude

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.