TOO SMALL MEMORY

I have a genuino uno board trying to make an EMF detector. I tried to upload the code but i have an error which says Sketch uses 4,276 bytes (13%) of program storage space. Maximum is 32,256 bytes. Global variables use 256 bytes (12%) of dynamic memory, leaving 1,792 bytes for local variables. Maximum is 2,048 bytes.What can i do to reduce the size of the following code?Please help its for a school project.

// EMF Detector for 7-Segment LED Display v1.0
//
// original code/project by Aaron ALAI - aaronalai1@gmail.com
// modified for use w/ LED bargraph by Collin Cunningham - collin@makezine.com
// modified again by ComputerGeek for instructable & 7-segment display.

#define NUMREADINGS 12 // raise this number to increase data smoothing

int senseLimit = 100; // raise this number to decrease sensitivity (up to 1023 max)
int probePin = 5; // analog 5
int val = 0; // reading from probePin

int LED1 = 2;   // connections
int LED2 = 3;   // to
int LED3 = 4;   // 7-segment
int LED4 = 5;   // display
int LED5 = 6;
int LED6 = 7;
int LED7 = 8;
int LED8 = 9;
// variables for smoothing

int readings[NUMREADINGS];                // the readings from the analog input
int index = 0;                            // the index of the current reading
int total = 0;                            // the running total
int average = 0;                          // final average of the probe reading

//CHANGE THIS TO affect the speed of the updates for numbers. Lower the number the faster it updates.
int updateTime = 30;

void setup() {

 pinMode(2, OUTPUT);  // specify Display outputs
 pinMode(3, OUTPUT); 
 pinMode(4, OUTPUT); 
 pinMode(5, OUTPUT); 
 pinMode(6, OUTPUT); 
 pinMode(7, OUTPUT); 
 pinMode(8, OUTPUT); 
 pinMode(9, OUTPUT); 
 pinMode(10,OUTPUT);
 Serial.begin(9600);  // initiate serial connection for debugging/etc

 for (int i = 0; i < NUMREADINGS; i++)
   readings[i] = 0;                      // initialize all the readings to 0

  
}

void loop() {
 LEDlow();
 val = analogRead(probePin);  // take a reading from the probe

 if(val >= 1){                // if the reading isn't zero, proceed
   val = constrain(val, 1, senseLimit);  // turn any reading higher than the senseLimit value into the senseLimit value
   val = map(val, 1, senseLimit, 1, 1023);  // remap the constrained value within a 1 to 1023 range

   total -= readings[index];               // subtract the last reading
   readings[index] = val; // read from the sensor
   total += readings[index];               // add the reading to the total
   index = (index + 1);                    // advance to the next index

   if (index >= NUMREADINGS)               // if we're at the end of the array...
     index = 0;                            // ...wrap around to the beginning

   average = total / NUMREADINGS;          // calculate the average

   if (average > 50){                // if the average is over 50 ...
     showLED0();   // Show a 0

   }

   if (average > 150){               // and so on ...
     showLED1();   // Show a 1
   }

   if (average > 250){
     showLED2();   // Show a 2
   }

   if (average > 350){
     showLED3();   // Show a 3
   }


   if (average > 450){
     showLED4();   // Show a 4
   }

   if (average > 550){
     showLED5();   // Show a 5
   }

   if (average > 650){
     showLED6();   // Show a 6

   }

   if (average > 750){
     showLED7();   // Show a 7
   }

   if (average > 850){
     showLED8();   // Show a 8
   }

   if (average > 950){
     showLED9();   // Show a 9
   }
   Serial.println(average); // use output to aid in calibrating
   delay(updateTime);
 }

}
//Show the number 0
void showLED0(){
 LEDlow();
 digitalWrite(LED1, HIGH);
 digitalWrite(LED3, HIGH);
 digitalWrite(LED4, HIGH);
 digitalWrite(LED5, HIGH);
 digitalWrite(LED6, HIGH);
 digitalWrite(LED7, HIGH); 
}
//Show the number 1
void showLED1(){
 LEDlow();
 digitalWrite(LED5, HIGH);
 digitalWrite(LED6, HIGH);
}
//Show the number 2
void showLED2(){
 LEDlow();
 digitalWrite(LED2, HIGH);
 digitalWrite(LED3, HIGH);
 digitalWrite(LED4, HIGH);
 digitalWrite(LED6, HIGH);
 digitalWrite(LED7, HIGH); 
}
//Show the number 3
void showLED3(){
 LEDlow();
 digitalWrite(LED2, HIGH);
 digitalWrite(LED4, HIGH);
 digitalWrite(LED5, HIGH);
 digitalWrite(LED6, HIGH);
 digitalWrite(LED7, HIGH); 
}
//Show the number 4
void showLED4(){
 LEDlow();
 digitalWrite(LED1, HIGH);
 digitalWrite(LED2, HIGH);
 digitalWrite(LED5, HIGH);
 digitalWrite(LED6, HIGH);
}
//Show the number 5
void showLED5(){
 LEDlow();
 digitalWrite(LED1, HIGH);
 digitalWrite(LED2, HIGH);
 digitalWrite(LED4, HIGH);
 digitalWrite(LED5, HIGH);
 digitalWrite(LED7, HIGH); 
}
//Show the number 6
void showLED6(){
 LEDlow();
 digitalWrite(LED1, HIGH);
 digitalWrite(LED2, HIGH);
 digitalWrite(LED3, HIGH);
 digitalWrite(LED4, HIGH);
 digitalWrite(LED5, HIGH);
 digitalWrite(LED7, HIGH); 
}
//Show the number 7
void showLED7(){
 LEDlow();
 digitalWrite(LED5, HIGH);
 digitalWrite(LED6, HIGH);
 digitalWrite(LED7, HIGH); 
}
//Show the number 8
void showLED8(){
 LEDlow();
 digitalWrite(LED1, HIGH);
 digitalWrite(LED2, HIGH);
 digitalWrite(LED3, HIGH);
 digitalWrite(LED4, HIGH);
 digitalWrite(LED5, HIGH);
 digitalWrite(LED6, HIGH);
 digitalWrite(LED7, HIGH); 
}
//Show the number 9
void showLED9(){
 LEDlow();
 digitalWrite(LED1, HIGH);
 digitalWrite(LED2, HIGH);
 digitalWrite(LED4, HIGH);
 digitalWrite(LED5, HIGH);
 digitalWrite(LED6, HIGH);
 digitalWrite(LED7, HIGH); 
}
//Show the letter e
void showLEDe(){
 LEDlow();
 digitalWrite(LED1, HIGH);
 digitalWrite(LED2, HIGH);
 digitalWrite(LED3, HIGH);
 digitalWrite(LED4, HIGH);
 digitalWrite(LED7, HIGH);
}
//Show the letter f
void showLEDf(){
 LEDlow();
 digitalWrite(LED1, HIGH);
 digitalWrite(LED2, HIGH);
 digitalWrite(LED3, HIGH);
 digitalWrite(LED7, HIGH); 
}
//Show the letter d
void showLEDd(){
 LEDlow();
 digitalWrite(LED2, HIGH);
 digitalWrite(LED3, HIGH);
 digitalWrite(LED4, HIGH);
 digitalWrite(LED5, HIGH);
 digitalWrite(LED6, HIGH);
}
//Resets the display
void LEDlow(){
 digitalWrite(LED1, LOW);
 digitalWrite(LED2, LOW);
 digitalWrite(LED3, LOW);
 digitalWrite(LED4, LOW);
 digitalWrite(LED5, LOW);
 digitalWrite(LED6, LOW);
 digitalWrite(LED7, LOW); 
}

Please take time to read the posting instructions that will help you to edit your post and make it readable.

Why is this an installation issue?

I deleted the copy of this post.

It didn't have code tags either, so was equally illegible.

As AWOL says, you really should have read the How to use this forum - please read post at the top of the index page and How to use this forum before posting.

ie Your code and any error messages should always be placed between code tags. Posting it inline as you have done makes it much harder to read or copy and paste for diagnosis.

It's still not too late to edit your post and do this. You'll make potential helpers much happier. :)

(I was just putting this into the other thread when it disappeared. :) )

Edit: And this isn't an 'error' message:-

Sketch uses 4,276 bytes (13%) of program storage space. Maximum is 32,256 bytes.
Global variables use 256 bytes (12%) of dynamic memory, leaving 1,792 bytes for local variables. Maximum is 2,048 bytes.

It's just information - actually a 'success' message.

Sorry I am A COMPLETE NOOB first time doing projects with arduino . The only thing is that i cant make it work what should i do?

harisman: what should i do?

read up on arrays.

harisman: Sorry I am A COMPLETE NOOB first time doing projects with arduino . The only thing is that i cant make it work what should i do?

Well you could have posted the 'real' error message, for starters. You also could edit your post and place your code between code tags, as explained in "How to use this forum" :)

The real error message was about incompatible types. This:- .....removed because I was wrong..... :)

@OldSteve - that's why the italics are there ;)

(the readings are all already zero, so it was a waste of time anyway!)

AWOL: @OldSteve - that's why the italics are there ;)

(the readings are all already zero, so it was a waste of time anyway!)

I'm a bit slow sometimes. But it shows why code tags should be used. :)

This should be the same functionality (or dysfunctionality?), but in short form. (compiled but untested) 3474 / 235 byte

#define NUMREADINGS 12 // raise this number to increase data smoothing

const int senseLimit = 100; // raise this number to decrease sensitivity (up to 1023 max)
const byte probePin = 5; // analog 5
int val = 0; // reading from probePin

const byte LEDstart = 2;   // 2-9 to display (with resistor)
// variables for smoothing

int readings[NUMREADINGS];                // the readings from the analog input
byte index = 0;                            // the index of the current reading
int total = 0;                            // the running total
int average = 0;                          // final average of the probe reading

const int updateTime = 30;

const byte pattTab[10] PROGMEM = {
  0b01111101, 0b00110000, 0b01101110, 0b01111010, 0b00110011,
  0b01011011, 0b01011111, 0b01110000, 0b01111111, 0b01111011
};

void displayPattern(byte patt)
{
  for (byte pin = LEDstart, msk = 1; msk; msk <<= 1, pin++) {
    digitalWrite(pin, patt & msk);
  }
}

void setup()
{
  for (byte pin = LEDstart; pin < 10; pin++) {
    pinMode(pin, OUTPUT); // specify Display outputs
  }
  Serial.begin(115200); // initiate serial connection for debugging/etc
  displayPattern(0);
}

void loop() {
  val = analogRead(probePin);  // take a reading from the probe

  if (val) {              // if the reading isn't zero, proceed
    val = constrain(val, 1, senseLimit);  // turn any reading higher than the senseLimit value into the senseLimit value
    val = map(val, 1, senseLimit, 1, 1023);  // remap the constrained value within a 1 to 1023 range

    total -= readings[index];  // subtract the oldest reading
    readings[index++] = val;   // save reading
    total += val;

    if (index >= NUMREADINGS) {  // if we're at the end of the array...
      index = 0;                 // ...wrap around to the beginning
    }
    average = total / NUMREADINGS;  // calculate the average
    if (average > 50) // if the average is over 50 ...
    {
      displayPattern(pgm_read_byte(&pattTab[(average - 50) / 100]));
    }
    Serial.println(average); // use output to aid in calibrating
    delay(updateTime);
  }
}

Why do you want to reduce the size? For smaller proceser? like attiny? This is the output compiled for Attiny84. leaving the pin numbers aside.

Sketch uses 2,440 bytes (29%) of program storage space. Maximum is 8,192 bytes.
Global variables use 66 bytes of dynamic memory.

harisman: Sorry I am A COMPLETE NOOB first time doing projects with arduino . The only thing is that i cant make it work what should i do?

Can you please elaborate what "can't make it work" means?

harisman:
I have a genuino uno board trying to make an EMF detector. I tried to upload the code but i have an error which says Sketch uses 4,276 bytes (13%) of program storage space. Maximum is 32,256 bytes. Global variables use 256 bytes (12%) of dynamic memory, leaving 1,792 bytes for local variables. Maximum is 2,048 bytes.What can i do to reduce the size of the following code?Please help its for a school project.

// EMF Detector for 7-Segment LED Display v1.0

//
// original code/project by Aaron ALAI - aaronalai1@gmail.com
// modified for use w/ LED bargraph by Collin Cunningham - collin@makezine.com
// modified again by ComputerGeek for instructable & 7-segment display.

#define NUMREADINGS 12 // raise this number to increase data smoothing

int senseLimit = 100; // raise this number to decrease sensitivity (up to 1023 max)
int probePin = 5; // analog 5
int val = 0; // reading from probePin

int LED1 = 2;   // connections
int LED2 = 3;   // to
int LED3 = 4;   // 7-segment
int LED4 = 5;   // display
int LED5 = 6;
int LED6 = 7;
int LED7 = 8;
int LED8 = 9;
// variables for smoothing

int readings[NUMREADINGS];                // the readings from the analog input
int index = 0;                            // the index of the current reading
int total = 0;                            // the running total
int average = 0;                          // final average of the probe reading

//CHANGE THIS TO affect the speed of the updates for numbers. Lower the number the faster it updates.
int updateTime = 30;

void setup() {

pinMode(2, OUTPUT);  // specify Display outputs
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10,OUTPUT);
Serial.begin(9600);  // initiate serial connection for debugging/etc

for (int i = 0; i < NUMREADINGS; i++)
  readings[i] = 0;                      // initialize all the readings to 0

}

void loop() {
LEDlow();
val = analogRead(probePin);  // take a reading from the probe

if(val >= 1){                // if the reading isn’t zero, proceed
  val = constrain(val, 1, senseLimit);  // turn any reading higher than the senseLimit value into the senseLimit value
  val = map(val, 1, senseLimit, 1, 1023);  // remap the constrained value within a 1 to 1023 range

total -= readings[index];               // subtract the last reading
  readings[index] = val; // read from the sensor
  total += readings[index];               // add the reading to the total
  index = (index + 1);                    // advance to the next index

if (index >= NUMREADINGS)               // if we’re at the end of the array…
    index = 0;                            // …wrap around to the beginning

average = total / NUMREADINGS;          // calculate the average

if (average > 50){                // if the average is over 50 …
    showLED0();   // Show a 0

}

if (average > 150){               // and so on …
    showLED1();   // Show a 1
  }

if (average > 250){
    showLED2();   // Show a 2
  }

if (average > 350){
    showLED3();   // Show a 3
  }

if (average > 450){
    showLED4();   // Show a 4
  }

if (average > 550){
    showLED5();   // Show a 5
  }

if (average > 650){
    showLED6();   // Show a 6

}

if (average > 750){
    showLED7();   // Show a 7
  }

if (average > 850){
    showLED8();   // Show a 8
  }

if (average > 950){
    showLED9();   // Show a 9
  }
  Serial.println(average); // use output to aid in calibrating
  delay(updateTime);
}

}
//Show the number 0
void showLED0(){
LEDlow();
digitalWrite(LED1, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);
}
//Show the number 1
void showLED1(){
LEDlow();
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
}
//Show the number 2
void showLED2(){
LEDlow();
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);
}
//Show the number 3
void showLED3(){
LEDlow();
digitalWrite(LED2, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);
}
//Show the number 4
void showLED4(){
LEDlow();
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
}
//Show the number 5
void showLED5(){
LEDlow();
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED7, HIGH);
}
//Show the number 6
void showLED6(){
LEDlow();
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED7, HIGH);
}
//Show the number 7
void showLED7(){
LEDlow();
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);
}
//Show the number 8
void showLED8(){
LEDlow();
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);
}
//Show the number 9
void showLED9(){
LEDlow();
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);
}
//Show the letter e
void showLEDe(){
LEDlow();
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED7, HIGH);
}
//Show the letter f
void showLEDf(){
LEDlow();
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED7, HIGH);
}
//Show the letter d
void showLEDd(){
LEDlow();
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
}
//Resets the display
void LEDlow(){
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
}

For some so far unknown reason ( and for me irrelevant, I just changed index to iIndex ) my version of IDE does not line int index declaration.

You have asked how to make the code shorter and your reasoning why should be of no concern to the group.
It is your project , not the group project and you should pay attention to your teacher and not the group mostly misguided personal opinions. After-all he will grade you , not the group. Just because some feels they now better, they are not teachers by profession after all and it shows.

Keep that in mind and welcome to this forum anyway.

One of the easiest way to shorten the program footprint is to use “for()” loop construct.

You have defined your I/O pins in sequence so when you want to process a command for all of them you can do it by changing the pin number you pass to the command / function as a parameter.
That is one of the features of functions.

For example using pinMode( parameter pin number, parameter Mode) in for() loop

for( starting pin number ; ending pin number; advance pin number)
pinMode(pin number, OUTPUT)

You can apply similar to another function - digitalWrite.

Good luck with your school project.

Vaclav: For some so far unknown reason ( and for me irrelevant, I just changed index to iIndex ) my version of IDE does not line int index declaration.

Do you ever proof-read your posts, Vaclav?

AWOL: Do you ever proof-read your posts, Vaclav?

No, he's too busy trying to write about how dumb everyone else and making himself look like an idiot writing stupid nonsense.

harisman: I have a genuino uno board trying to make an EMF detector. I tried to upload the code but i have an error which says Sketch uses 4,276 bytes (13%) of program storage space. Maximum is 32,256 bytes. Global variables use 256 bytes (12%) of dynamic memory, leaving 1,792 bytes for local variables. Maximum is 2,048 bytes.What can i do to reduce the size of the following code?Please help its for a school project.

You only use a small amount of the uno memory but here is something to know,

Variables of type int use 2 bytes and can count from -32768 to +32767. Variables of type byte use 1 byte and can count from 0 to 255. Variables of type char use 1 byte and can count from -128 to +127. fixed a booboo

When you want to store and use pin numbers, which is better? 1 byte or 2 bytes to do the same thing?

Learn the variable types and how to use them, it will take practice but save more time on debugging. Debugging is where most time programming gets spent even though much is done to reduce that -- what usually happens is the better you get, the more/better/bigger you do.

Old programmer adage: if you want to make a program right, make two versions and throw the first away. That is because first time you make mistakes that give you the lessons to do much better on the next.

+1

Old programmer adage: if you want to make a program right, make two versions and throw the first away. That is because first time you make mistakes that give you the lessons to do much better on the next.

+10

On usage of variables of minimal size

Corollary to Murphy's law - any program will eventually exceed the available memory.

OF likes me comment the old code / version out, to have a paper trail of my misteakes to remind me.

Good luck.

Corollary to Murphy's law - any program will eventually exceed the available memory.

That would be Parkinson's Law.

+1

Old programmer adage: if you want to make a program right, make two versions and throw the first away. That is because first time you make mistakes that give you the lessons to do much better on the next.

+10

Still, not figured quote tags, huh?

Or proof-reading.

GoForSmoke: You only use a small amount of the uno memory but here is something to know,

Variables of type int use 2 bytes and can count from -32768 to +32767. Variables of type byte use 1 byte and can count from 0 to 255. Variables of type byte use 1 byte and can count from -128 to +127.

I think you meant: Variables of type char use 1 byte and can count from -128 to +127.

Whups! Too fast with the copy & paste, yup it's char, you so right! fixed!