Program Memory

Hey all,

I'm writing a program for the ATTiny85 chip, that allows each attiny to talk to each other using my own method.

I was working on it today when I bumped into something interesting. As you may know, each attiny85 only has 8192 bytes for the program, so I need to be smart with the way I write the code. When I compile the program with this code snippet:

oid sendStartFlag() {
  for(int i=0;i<(sizeof(startFlag)/sizeof(startFlag[0]));i++)
  {
    digitalWrite(ALERT,HIGH);
    digitalWrite(TALK,startFlag[i]);
    delay(10);
    digitalWrite(ALERT,LOW);
    delay(10);
  }
}

the entire program compiles to 928 bytes. Whem I compile the prohram with this code snippet:

oid sendStartFlag() {
  for(int i=0;i<(sizeof(startFlag)/sizeof(startFlag[0]));i++)
  {
    digitalWrite(ALERT,HIGH);
    digitalWrite(TALK,startFlag[i]);
    delay(100);
    digitalWrite(ALERT,LOW);
    delay(100);
  }
}

the entire program compiles to 890 bytes. 38 WHOLE BYTES just dissapearing on me.

Can anyone explain what's going on here?

Not without posting the entire source.

int mode=1;
int startFlag[6]={1,0,1,0,1,0};
int endFlag[6]={1,0,0,1,0,0};
#define ALERT 0
#define IN 2
#define OUT 1
#define LISTEN 3
#define TALK 4
#define DELAY 10
void setup() {
  pinMode(ALERT,OUTPUT);
  pinMode(OUT,OUTPUT);
  pinMode(IN,INPUT);
  pinMode(LISTEN,INPUT);
  pinMode(TALK,OUTPUT);
}
void loop() {
  if(mode)
  {
    sendFlag();
  }
  else
  {
    
  }
}

void sendStartFlag() {
  for(int i=0;i<(sizeof(startFlag)/sizeof(startFlag[0]));i++)
  {
    digitalWrite(ALERT,HIGH);
    digitalWrite(TALK,startFlag[i]);
    delay(10);
    digitalWrite(ALERT,LOW);
    delay(10);
  }
}
void sendEndFlag()
{
  for(int i=0;i<(sizeof(endFlag)/sizeof(endFlag[0]));i++)
  {
    digitalWrite(ALERT,HIGH);
    digitalWrite(TALK,endFlag[i]);
    delay(DELAY*10);
    digitalWrite(ALERT,LOW);
    delay(DELAY*10);
  }
}

100 fits in a byte, 1000 doesn't?

The only difference is the length of the delays...
A delay of 10 seems to take up more space than a delay of 100

So, int8_t handling vs. int16_t handling.

Should be relatively easy to trace

I figured it out....
If all my delays are the same, the program takes up less space... When I was changing them to 100 vs 10, they wouldn't match the other two delays I have in loop().

I've deleted your other cross-post @ maxpo .

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

maxpo:
The only difference is the length of the delays...
A delay of 10 seems to take up more space than a delay of 100

10 can probably be represented as an immediate literal constant in an instruction, but 100 can't, so requires extra instructions to load and manipulate