How do i save memory in this code.

Hey, i got a problem with this code. It's using too much RAM and i have no clue how to make it use less of it.

Here is the code:

int on = HIGH;
int off = LOW;
int led1 = 9;
int led2 = 8;
int led3 = 7;
int led4 = 6;
int led5 = 5;
int led6 = 4;
int led7 = 3;
int button = 2;
int buttonState = 0;
long randNumber;;
int lastButtonState = 0;
int buttonMode = 0;


void setup() {
  Serial.begin(9600);
  for (int pinNumber = 3; pinNumber < 10; pinNumber++) {
    pinMode(pinNumber, OUTPUT);
  }
  pinMode(button, INPUT);
  randomSeed(analogRead(0));
}

void loop() {
  buttonState = digitalRead(button);
  int numStart = 0;
  int numEnd = 1;
  if (buttonState == on && numStart != numEnd) {
    Serial.println("Ravistetaan");
    numStart++;
  }
    if(buttonState == on){
    buttonMode++;
    digitalWrite(led1, on);
    delay(50);
    digitalWrite(led1, off);
    digitalWrite(led2, on);
    delay(50);
    digitalWrite(led2, off);
    digitalWrite(led3, on);
    delay(50);
    digitalWrite(led3, off);
    digitalWrite(led4, on);
    delay(50);
    digitalWrite(led4, off);
    digitalWrite(led5, on);
    delay(50);
    digitalWrite(led5, off);
    digitalWrite(led6, on);
    delay(50);
    digitalWrite(led6, off);
    digitalWrite(led7, on);
    delay(50);
    digitalWrite(led7, off);
    delay(100);
    Serial.println(randNumber);
  }
  else (buttonMode == 1 && buttonState == off);{
    buttonMode--;
    randNumber == random(8);
    digitalWrite(led1, off);
    digitalWrite(led2, off);
    digitalWrite(led3, off);
    digitalWrite(led4, off);
    digitalWrite(led5, off);
    digitalWrite(led6, off);
    digitalWrite(led7, off);
  }
}

Thx.

Use defines instead of declaring integer variables for your pins and states.

Blackfin:
Use defines instead of declaring integer variables for your pins and states.

Can you give me an example?

Use byte instead of int for pin numbers. And make the const as they will not change.

What kind of processor?

  else (buttonMode == 1 && buttonState == off);
    randNumber == random(8);

After you fix problems like the ones above don't use int variables when byte would be big enough and declare variables as const if they are never going to change, such as pin numbers and write a function to turn the pairs of LEDs on and off instead of repeating code

   Serial.println("Ravistetaan");

use

   Serial.println(F("Ravistetaan"));

instead

Given that it's such a tiny piece of code, what's the driver for RAM saving at this stage?

Got it working thanks for all the help!

Possibly a processor that only has 128 bytes of memory :wink:

Just_Joe:
Got it working thanks for all the help!

I would be interested in seeing the revised code

@UKHeliBob could you expand on

 Serial.println(F("Ravistetaan"));

Do you have a function F that is doing something clever with a char array? Or are you declaring it implicitly on the fly? I am amazed (but not disbelieving) that this would be more efficient because it would suggest to me that the compiler should just do whatever F does all the time...

DaleSchultz:
@UKHeliBob could you expand on

 Serial.println(F("Ravistetaan"));

Do you have a function F that is doing something clever with a char array? Or are you declaring it implicitly on the fly? I am amazed (but not disbelieving) that this would be more efficient because it would suggest to me that the compiler should just do whatever F does all the time...

F(string) is a compiler macro introduced in arduino version 1.0. It tells the compiler to leave the string (array of chars) in flash memory instead of allocating space in the sram region.

Do you have a function F

As you have seen it is not my function nor is it even a function but it does work for the reasons explained in the previous post

a ha! thanks, I found it documented under PROGMEM ! handy.