Sketch won't start from the beginning

Hi all,
I have an Arduino Duemilanove (CB9604) and a Centipede board. I have my sketch about 12254 bytes.
When I try to upload the code everything looks fine, when I try to run it, it runs the setup() commands, but then it goes almost to the end of the sketch !!! when the program finish and goes to loop it starts from a different point :o
When I cut a piece of the program and left it to 10702 bytes it starts from the beginning.
Is that the whole memory available and not 30720 bytes as it says when compiling?
Any ideas? :frowning:

Any ideas?

Without code?

Too much RAM usage, null pointer dereference, function call via null pointer.

Yes you are right, but the code is just opening and closing leds, nothing more. I removed all the Serial.println ("..") and the code gone to 9682 and is working so I supose the memory is the problem. But why on the compilation it says 30720 bytes available? Thanks.

Program memory != RAM

::slight_smile:
so program memory is about 11 kbytes?
"Binary sketch size: 9688 bytes (of a 30720 byte maximum)"
RAM=30720

Great thanks Groove

We can debate all day.
Or you can post your code.

Can I send it to you? PM?

OK , send it with email.
Thanks

Have you noticed that when other people post code, it comes in a neat grey box?
Go back to your last post, click 'modify', highlight the code, then click on the # icon on the toolbar, then click on 'post'.

It’s too big :’(
Can not paste it here at once

This place http://www.arduino.cc/en/Tutorial/Memory explains about the memory of an Arduino 168. A 328 has 32K of flash memory of which 30720 is free iso 16K (incl bootloader).

See also http://arduino.cc/en/Main/Hardware for other models

yes I have this ATMEGA328O-PU

Can paste it here at once

Then use one posting for your setup and one for loop or more if you need.

Or maybe its better to strip your code to a minimal version that still reproduces the error. Also a desciption of what is intended with the code, what you expected it to do and what it did is helpfull.

The program is very simple, just open and close I/O ports with LEDs at a specific time nothing more.

That could have gone better.
have you ever considered arrays?

Just for debugging and to follow the flow of the execution ...

arrays !!!
No I have not. I'm going to check it now.

What is a CS, and how much RAM does it use?
I think the Wire library has a fairly large RAM buffer - that with the remaining prints may be breaking thing.
It is polite when posting code to delete anything that isn't part of the code.

CS. is the Centipede object

About array’s http://www.arduino.cc/en/Reference/Array
Also check structs. http://www.arduino.cc/playground/Code/Struct

looking at your code I see these events.

struct Event {
long time; // at what time (in millis)
byte port; // needs this port
byte flag; // be set to this value LOW/HIGH
};

After you have defined an array of these events , it could be processed in a simple loop. (in fact you could read these events one by one from an SD card) In pseudocode:

while (not handled all events)
{
- get next event from array 
- wait until event.time arrived
- set pin to flag value
}

Finaly…
The whole code :slight_smile:

#include <Wire.h>
#include <Centipede.h>
//#include <MP3Trigger.h>

const int resetPin = 10;     // the number of the pushbutton pin
const int ledPin1 =  5;      // the number of the LED pin
const int ledPin2=  6;
//MP3Trigger trigger;  


int buttonState = 0;         // variable for reading the pushbutton status
Centipede CS; // create Centipede object

void setup() {
  delay (5000);
  Serial.begin(9600);
  pinMode(ledPin1, OUTPUT);      
  pinMode(resetPin, INPUT);  
  // trigger.setup();

  Wire.begin(); // start I2C
  CS.initialize(); // set all registers to default
  CS.portMode(0, 0b0000000000000000); // set all pins on port 0 to output
  CS.portMode(1, 0b0000000000000000); // set all pins on port 1 to output
  CS.portMode(2, 0b0000000000000000); // set all pins on port 2 to output
  CS.portMode(3, 0b0000000000000000); // set all pins on port 3 to output

}

void loop(){
 
  buttonState = digitalRead(resetPin);
 

  delay(5000);

  CS.digitalWrite(1, HIGH); //
  delay(1000);  
  CS.digitalWrite(1, LOW); //
  delay(50);  
 Serial.println("LOOP STARTED!");

  

  //Serial.write('t');
  //Serial.write(1);
  //00:27
  delay (20000); //000                         
  CS.digitalWrite(37, HIGH); //
  delay(30000);  
  CS.digitalWrite(37, LOW); //
  CS.digitalWrite(36, HIGH); //
  delay(35000);  
  CS.digitalWrite(36, LOW); //
  delay(10);  

  // delay (3000); //000                         
  CS.digitalWrite(40, HIGH); //
  delay(30000);  
  CS.digitalWrite(40, LOW); //
  delay(10);  

  CS.digitalWrite(36, HIGH); //
  CS.digitalWrite(37, HIGH); //
  delay(35000);  
  CS.digitalWrite(36, LOW); //
  CS.digitalWrite(37, HIGH); //
  delay(10);  

  for(int i=0;i<6; i++){
    CS.digitalWrite(11, HIGH); //
    CS.digitalWrite(12, HIGH); //
    CS.digitalWrite(13, HIGH); //
    CS.digitalWrite(10, HIGH); //

    delay (500);
    CS.digitalWrite(11, LOW); //
    CS.digitalWrite(12, LOW); //
    CS.digitalWrite(13, LOW); //
    CS.digitalWrite(10, LOW); //
    delay (200);
  }
  CS.digitalWrite(11, HIGH); //
  CS.digitalWrite(12, HIGH); //
  CS.digitalWrite(13, HIGH); //
  CS.digitalWrite(10, HIGH); //

  delay (4000);
  for(int i=0;i<6; i++){
    CS.digitalWrite(7, HIGH); //
    delay (300);
    CS.digitalWrite(7, LOW); //  
    delay (200);
  }
  CS.digitalWrite(7, HIGH); //
  for(int i=0;i<6; i++){
    CS.digitalWrite(5, HIGH); //
    delay (300);
    CS.digitalWrite(5, LOW); //  
    delay (200);
  }
  CS.digitalWrite(5, HIGH); //
  delay (6000);
  for(int i=0;i<5; i++){
    CS.digitalWrite(4, HIGH); //
    delay (300);
    CS.digitalWrite(4, LOW); //  
    delay (200);
  }
  CS.digitalWrite(4, HIGH); //
  for(int i=0;i<6; i++){
    CS.digitalWrite(3, HIGH); //
    delay (300);
    CS.digitalWrite(3, LOW); //  
    delay (200);
  }
  CS.digitalWrite(3, HIGH); //
  for(int i=0;i<6; i++){
    CS.digitalWrite(2, HIGH); //
    delay (300);
    CS.digitalWrite(2, LOW); //  
    delay (200);
  }
  CS.digitalWrite(2, HIGH); //
  delay(1000);
  for(int i=0;i<6; i++){
    CS.digitalWrite(6, HIGH); //
    delay (300);
    CS.digitalWrite(6, LOW); //  
    delay (200);
  }
  CS.digitalWrite(6, HIGH); //
  delay(5000);
  for(int i=0;i<6; i++){
    CS.digitalWrite(0, HIGH); //
    delay (300);
    CS.digitalWrite(0, LOW); //  
    delay (200);
  }
  CS.digitalWrite(0, HIGH); //
  delay(1000);
  for(int i=0;i<6; i++){
    CS.digitalWrite(1, HIGH); //
    delay (300);
    CS.digitalWrite(1, LOW); //  
    delay (200);
  }
  CS.digitalWrite(1, HIGH); //
  for(int i=0;i<6; i++){
    CS.digitalWrite(15, HIGH); //
    delay (300);
    CS.digitalWrite(15, LOW); //  
    delay (200);
  }
  CS.digitalWrite(15, HIGH); //
  delay(2000);
  for(int i=0;i<6; i++){
    CS.digitalWrite(11, HIGH); //
    CS.digitalWrite(12, HIGH); //
    CS.digitalWrite(13, HIGH); //
    CS.digitalWrite(10, HIGH); //

    delay (500);
    CS.digitalWrite(11, LOW); //
    CS.digitalWrite(12, LOW); //
    CS.digitalWrite(13, LOW); //
    CS.digitalWrite(10, LOW); //
    delay (200);
  }
  CS.digitalWrite(11, HIGH); //
  CS.digitalWrite(12, HIGH); //
  CS.digitalWrite(13, HIGH); //
  CS.digitalWrite(10, HIGH); //

  delay (4000);
  CS.digitalWrite(11, LOW); //
  CS.digitalWrite(12, LOW); //
  CS.digitalWrite(13, LOW); //
  CS.digitalWrite(10, LOW); //

  for(int i=0;i<6; i++){
    CS.digitalWrite(11, HIGH); //
    CS.digitalWrite(12, HIGH); //
    CS.digitalWrite(13, HIGH); //
    CS.digitalWrite(10, HIGH); //

    delay (700);
    CS.digitalWrite(11, LOW); //
    CS.digitalWrite(12, LOW); //
    CS.digitalWrite(13, LOW); //
    CS.digitalWrite(10, LOW); //
    delay (300);
  }
  CS.digitalWrite(11, HIGH); //
  CS.digitalWrite(12, HIGH); //
  CS.digitalWrite(13, HIGH); //
  CS.digitalWrite(10, HIGH); //

  delay (4000);
  CS.digitalWrite(37, HIGH); //
  delay (15000);
  CS.digitalWrite(37, LOW); //
  CS.digitalWrite(40, HIGH); //
  delay (15000);
  CS.digitalWrite(40, LOW); //
  CS.digitalWrite(36, HIGH); //
  delay (15000);
  CS.digitalWrite(36, LOW); //
  CS.digitalWrite(39, HIGH); //
  delay (15000);
  CS.digitalWrite(39, LOW); //
  for(int i=0;i<6; i++){
    CS.digitalWrite(11, HIGH); //
    CS.digitalWrite(12, HIGH); //
    CS.digitalWrite(13, HIGH); //
    CS.digitalWrite(10, HIGH); //

    delay (700);
    CS.digitalWrite(11, LOW); //
    CS.digitalWrite(12, LOW); //
    CS.digitalWrite(13, LOW); //
    CS.digitalWrite(10, LOW); //
    delay (300);
  }
  CS.digitalWrite(11, HIGH); //
  CS.digitalWrite(12, HIGH); //
  CS.digitalWrite(13, HIGH); //
  CS.digitalWrite(10, HIGH); //

  delay (15000);
  // ta sbhno ola plin tourkovn

  for (int i=15; i<64; i++){
    CS.digitalWrite(i, LOW);
  }
  for (int i=0; i<10; i++){
    CS.digitalWrite(i, LOW);
  }


  for(int i=0;i<6; i++){
    CS.digitalWrite(5, HIGH); //
    delay (300);
    CS.digitalWrite(5, LOW); //  
    delay (200);
  }
  CS.digitalWrite(5, HIGH); //


  CS.digitalWrite(22, HIGH); //
  delay (300);
  CS.digitalWrite(27, HIGH); //
  delay (4000);
  for(int i=0;i<6; i++){
    CS.digitalWrite(7, HIGH); //
    delay (300);
    CS.digitalWrite(7, LOW); //  
    delay (200);
  }
  CS.digitalWrite(7, HIGH); //
  CS.digitalWrite(30, HIGH); //
  delay (3500);
  for(int i=0;i<6; i++){
    CS.digitalWrite(15, HIGH); //
    delay (300);
    CS.digitalWrite(15, LOW); //  
    delay (200);
  }
  CS.digitalWrite(15, HIGH); //
  delay(500);

  CS.digitalWrite(29, HIGH); //
  delay(200);  
  CS.digitalWrite(26, HIGH); //
  delay(2800);  
  for(int i=0;i<5; i++){
    CS.digitalWrite(4, HIGH); //
    delay (300);
    CS.digitalWrite(4, LOW); //  
    delay (200);
  }
  CS.digitalWrite(4, HIGH); //
  CS.digitalWrite(21, HIGH); //
  delay(500);  

  for(int i=0;i<6; i++){
    CS.digitalWrite(6, HIGH); //
    delay (300);
    CS.digitalWrite(6, LOW); //  
    delay (200);
  }
  CS.digitalWrite(6, HIGH); //
  delay(3000); 

  for(int i=0;i<6; i++){
    CS.digitalWrite(3, HIGH); //
    delay (300);
    CS.digitalWrite(3, LOW); //  
    delay (200);
  }
  CS.digitalWrite(3, HIGH); //
  delay(1000);
  CS.digitalWrite(19, HIGH); //
  delay(4000);  
  for(int i=0;i<6; i++){
    CS.digitalWrite(2, HIGH); //
    delay (300);
    CS.digitalWrite(2, LOW); //  
    delay (200);
  }
  CS.digitalWrite(2, HIGH); //
  delay (500);
  CS.digitalWrite(25, HIGH); //
  delay(1000);

  for(int i=0;i<6; i++){
    CS.digitalWrite(1, HIGH); //
    delay (300);
    CS.digitalWrite(1, LOW); //  
    delay (200);
  }
  CS.digitalWrite(1, HIGH); //  
  delay (1000);
  CS.digitalWrite(24, HIGH); //
  delay(500);  
  delay(2000);

  for(int i=0;i<6; i++){
    CS.digitalWrite(0, HIGH); //
    delay (300);
    CS.digitalWrite(0, LOW); //  
    delay (200);
  }
  CS.digitalWrite(0, HIGH); //
  delay(1000);

  CS.digitalWrite(16, HIGH); //
  delay(1500);  
  delay (2000);
  delay (10000);
  delay (36000); 
  
  for(int i=0;i<6; i++){
    CS.digitalWrite(15, HIGH); //
    delay (300);
    CS.digitalWrite(15, LOW); //  
    delay (200);
  }
  CS.digitalWrite(15, HIGH); //
  delay(2000);

  CS.digitalWrite(29, HIGH); //
  delay(11000);  
   for(int i=0;i<6; i++){
    CS.digitalWrite(7, HIGH); //
    CS.digitalWrite(5, HIGH); //
    CS.digitalWrite(4, HIGH); //
    delay (300);
    CS.digitalWrite(7, LOW); // 
    CS.digitalWrite(5, LOW); //
    CS.digitalWrite(4, LOW); //  
    delay (200);
  }
  CS.digitalWrite(7, HIGH); //
  CS.digitalWrite(5, HIGH); //
  CS.digitalWrite(4, HIGH); //
  delay (2000);
    CS.digitalWrite(30, HIGH); //

    CS.digitalWrite(22, HIGH); //
  
  delay (50000);
  CS.digitalWrite(11, LOW); // tourkoi
  CS.digitalWrite(11, HIGH); //
  CS.digitalWrite(11, LOW); //
  CS.digitalWrite(11, HIGH); //
  delay(11000);

  CS.digitalWrite(4, LOW); //
  delay (500);
  CS.digitalWrite(4, HIGH); //
  delay (500);
  CS.digitalWrite(4, LOW); //
  delay (500);
  CS.digitalWrite(4, HIGH); //
  delay (500);
  CS.digitalWrite(21, LOW); //
  delay (20000);
  CS.digitalWrite(20, HIGH); //

  delay(33000);
  CS.digitalWrite(7, HIGH); //
  delay(27000);
  delay (14000);
...
  delay (24000);
  delay(43000);
  delay(73000);
  delay (35000);
  delay (2000);

  delay(128000);
  delay(7000);
  delay(5000);
  delay (2000);
  delay (38000);

  for(int i=0;i<60;i++){
    CS.digitalWrite(i, LOW); //
    delay (10);
  }  
 
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);   
  

}