I am running a program on the ATtiny and for some reason I am way over the 512 bytes of RAM that are available to me. This is confusing because when I did the math by hand I should have 432 bytes. Here is my code:
#include <SoftwareSerial.h>
#define FASTADC 1
// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
SoftwareSerial mySerial(0,1); //This uses 47 bytes of RAM
byte i = 0; This uses 1 byte of RAM
byte pixelsArray1[128]; //Arrays use 128 bytes of RAM each
byte pixelsArray2[128];
byte pixelsArray3[128];
void setup()
{
#if FASTADC
// set prescale to 16
sbi(ADCSRA,ADPS2);
cbi(ADCSRA,ADPS1);
cbi(ADCSRA,ADPS0);
#endif
pinMode(1,OUTPUT); //Setting the 1 pin to be used for output
pinMode(0,OUTPUT); //Setting the 0 pin to be used for output
pinMode(A3,INPUT); //Input for Camera 3
pinMode(A2,INPUT); //Input for Camera 2
pinMode(A1,INPUT); //Input for Camera 1
mySerial.begin(9600); //Setting the data transfer rate
}
It's telling me that I am using 510 Bytes of memory but my math gave me 432 bytes of RAM. Does anyone see what I'm missing?
-
You should ask avr-objdump.Edgar Bonet– Edgar Bonet06/14/2016 14:23:51Commented Jun 14, 2016 at 14:23
-
Out of interest how did you come up with your 432?Code Gorilla– Code Gorilla06/14/2016 14:30:58Commented Jun 14, 2016 at 14:30
-
3*128 Arrays + 47 SoftwareSerial + 1 byte variable = 432sgmm– sgmm06/14/2016 14:36:31Commented Jun 14, 2016 at 14:36
-
Which ATtiny are you using? Are you using SoftwareSerial for sending, receiving or both?Edgar Bonet– Edgar Bonet06/14/2016 14:40:21Commented Jun 14, 2016 at 14:40
-
I'm using the ATtiny85 which has 512 bytes of RAM. Also I'm using the ATtiny to program a camera so I'm assuming that SoftwareSerial is used for both. Sorry, but this is my first Arduino Project ever.sgmm– sgmm06/14/2016 14:42:55Commented Jun 14, 2016 at 14:42
1 Answer 1
There is a lot you are missing. Such as:
- SoftwareSerial is using RAM.
- The system stack is using RAM.
- The Arduino core software is using RAM.
There's plenty more going on than just your sketch. For instance, where do you think it stores the current millis()
and micros()
counts? Where do you think incoming serial data gets buffered? Where do you think your registers, PC, etc, get stashed when a function (such as setup()
) is called? All that takes RAM.
-
She already counted SoftwareSerial, and the stack is not in the 510 bytes reported by the compiler. So this leaves only the Arduino core.Edgar Bonet– Edgar Bonet06/14/2016 14:36:14Commented Jun 14, 2016 at 14:36
-
So is there any way I can reduce the amount of memory being used without scrapping my Arrays? For example, is there a way to NOT store the current
millis()
andmicros()
or is there an alternative to SoftwareSerial?sgmm– sgmm06/14/2016 14:38:22Commented Jun 14, 2016 at 14:38 -
You should use avr-objdump to examine all the section sizes and determine what can be removed - then remove it. That means manual editing of core code.Majenko– Majenko06/14/2016 14:41:19Commented Jun 14, 2016 at 14:41
-
How can I edit the avr-objdump? It's a .exe file and how do I know what I can remove. Again, I'm sorry but this is my first ever Arduino project.sgmm– sgmm06/14/2016 14:44:54Commented Jun 14, 2016 at 14:44
-
You don't edit avr-objdump - you use it to examine your .elf file. That will allow you, if used right, to list all the variables in your .elf file. You can then look to see what there is. You can then match those up to variables in the core software and determine if you need the functionality associated with those variables - and if not then delete it wholesale.Majenko– Majenko06/14/2016 14:46:29Commented Jun 14, 2016 at 14:46