Optimizing Code

Hello,

I am trying to make a Simon Says Memory Game and most of it is done. Now I am trying to speed up my program as I am using an ATTINY85 using a coin cell so ideally I want it running at 1MHz. The schematic I am using is this.

Can anyone make suggestions on how this program can be written more efficiently?

#define adc_disable() (ADCSRA &= ~(1<<ADEN))
const int pins[] = {0, 3, 4, 1};
const int pinsLens = 3;
const int ledPin = 2;
const int RandPin = 2;
const int RedPin = 1;
const int GreenPin = 3;

int ran;
int ans[8];
int in[8];

unsigned long timestart;
bool wait;
int buttonState[pinsLens + 1];
int lastButtonState[pinsLens + 1];
unsigned long lastDebounceTime[pinsLens + 1];
int reading[pinsLens + 1];

int i;
int j;
bool fail;
int PassFailpin; 

unsigned long debounceDelay = 15;
int level = 3;
float DelayMulti = 3;


void setup()
{
  randomSeed(analogRead(RandPin));
  adc_disable();
  startup();
}

void loop()
{
  delay(DelayMulti * 100);
  
  startround();
  
  delay(DelayMulti * 100);

  for (i = 0; i <= level; i++) {
    ran = random(0, (pinsLens + 1));
    ans[i] = ran;
    digitalWrite(pins[ran], LOW);
    pinMode(pins[ran], OUTPUT);
    delay(DelayMulti * 125);
    pinMode(pins[ran], INPUT_PULLUP);
    delay(DelayMulti * 100);
  }

  delay(DelayMulti * 300);
  for (i = 0; i <= pinsLens; i++) {
    pinMode(pins[i], INPUT_PULLUP);
  }


  for (int i = 0;i <= pinsLens; i++) {
    buttonState[i] = HIGH;
    lastButtonState[i] = HIGH;
    lastDebounceTime[i] = 0;
  }
  
  wait = true;
  timestart = millis();
  
  while (((millis() - timestart) <= 100000)) {
    for (j = 0; j <= level; j++) {
      wait = true;
      while (wait == true) {
      timestart = millis();
      
        for (i = 0; i <= pinsLens; i++) {
          reading[i] = digitalRead(pins[i]);
          
          if (reading[i] != lastButtonState[i]) {
            lastDebounceTime[i] = millis();
          }
          
          if ((millis() - lastDebounceTime[i]) > debounceDelay) {
            
            if (reading[i] != buttonState[i]) {
              buttonState[i] = reading[i];
              
              if (buttonState[i] == LOW) {
                in[j] = i;
                wait = false;
              }
            }
          }
          lastButtonState[i] = reading[i];
        }
      }    
    }
    break;
  }
  
  delay(DelayMulti * 100);

  fail = false;
  for (i = 0; i < 8; i++) {
    if (in[i] != ans[i]) {
      fail = true;
      break;
    }
  }


  if (fail == false) {
    for (i = 0; i < 3; i++) {
    endround(GreenPin);
    }
    if (level != 8) {
      level++;
    }
  }

  else if (fail == true) {
    for (i = 0; i < 3; i++) {
    endround(RedPin);
    }
    level = 3;
  }

  for (i = 0; i <= pinsLens; i++) {
    pinMode(pins[i], INPUT_PULLUP);
  }

}

void startup()
{
  for (i = 0; i < 4; i++) {
    digitalWrite(pins[i], LOW);
    pinMode(pins[i], OUTPUT);
    delay(DelayMulti * 75);
  }
  for (i = 4; i >= 1; --i) {
    pinMode(pins[i - 1], INPUT_PULLUP);
    delay(DelayMulti * 75);
  }
  for (i = 0; i < 4; i++) {
    digitalWrite(pins[3 - i], LOW);
    pinMode(pins[3 - i], OUTPUT);
    delay(DelayMulti * 75);
  }
  for (i = 0; i < 4; i++) {
    pinMode(pins[i], INPUT_PULLUP);
    delay(DelayMulti * 50);
  }
}
void startround()
{
  for (i = 0; i < 4; i++) {
    digitalWrite(pins[i], LOW);
    pinMode(pins[i], OUTPUT);
  }
  delay(DelayMulti * 300);
  for (i = 0; i < 4; i++) {
    pinMode(pins[i], INPUT_PULLUP);
  }
  for (i = 0; i < 9; i++) {
    ans[i] = 0;
    in[i] = 0;
  }
  fail = false;

}

void endround(int PassFailpin){
  
      digitalWrite(PassFailpin, LOW);
      pinMode(PassFailpin, OUTPUT);
      delay(DelayMulti * 100);
      pinMode(PassFailpin, INPUT_PULLUP);
      delay(PassFailpin * 50);
}

Never looked at a ATtiny but to my knowledge the below should also apply to the ATtiny

  for (i = 0; i < 4; i++) {
    digitalWrite(pins[i], LOW);
    pinMode(pins[i], OUTPUT);
  }

could be reduced to two instructions if the pins are on the same port. It’s called port manipulation and will be worth an investigation.

//Edit
Just looked at the datasheet and it only has one port so it should speed up your code.