I²C + SDcard (SPI) + Ethernet (SPI) + Timer1 interruption, ArduinoEthernet


I’m trying to use an arduinoEthernet (the board, not the shield) to read some I²C sensors, log the data in a SDCard and send/receive information through internet. I’m also using the serial to send some debug messages.

My idea is let the loop function to handle the Ethernet communication and use timer interruption to read the sensors and store data on SDCard.

To do the timer interruption I’m using the Library Timer1. But when I call Timer1.attatchInterrupt the arduino freezes, stop working… As far I understand Timer1 is only used by the servo library, so it should be “free” to use for other proposes.

I also tried to use the Timer1 just to blink the LED on pin 9, however it still freezing.

My doubts are:
1 - Is ArduinoEthernet able to handle all these things at the same time?
2 - Is Timer1 really free?

Post your code, perhaps?

How to use this forum

Sorry for the extremely late replay, I had some problems and could not work on this project for a while.

The system changed a bit:

  • Arduino Nano;
  • 4 Sensors at I²C bus;
  • SPI Slave, interrupt driven using pin 2;
  • pin 2 connected to SS (pin 10);

But the problem is the same, the arduino keeps freezing during the setup (with the same output)…

My setup() code is:

void setup()
    pinMode(DEBUG_LED, OUTPUT);
    digitalWrite(DEBUG_LED, LOW);
     * A huge delay to the Hub
     * do it's setup and do not
     * mess with the satellites'
     * interruptions
    for(bufferPos = 0; bufferPos <10; bufferPos++)
        inBuffer[bufferPos] = '\0';
    bufferPos = 0;

    //Initialize serial port
//    Serial.begin(9600, SERIAL_8E1);

    Serial.println("Serial initialized.");

    //Initialize I2C bus

    Serial.println("I2C initialized.");

     * Initialize accelerometer 0
    accelerometer0 = ADXL345(0x1D);
        Serial.println("Connected to ADXL345 0.");
        acel0 = true;
        Serial.println("Could not connect to ADXL345 0.");
        acel0 = false;

    //Set the range to -4g/+4g with 10 bits resolution
    accelerometer0.SetRange(4, false);
    //Enable accelerometer mensurements

     * Initialize accelerometer 1
    accelerometer1 = ADXL345(0x53);
        acel1 = true;

        Serial.println("Connected to ADXL345 1.");
        acel1 = false;

        Serial.println("Could not connect to ADXL345 1.");

    //Set the range to -4g/+4g with 10 bits resolution
    accelerometer1.SetRange(4, false);
    //Enable accelerometer mensurements

    /* Initialize SPI */
    // have to send on master in, *slave out*
    pinMode(MISO, OUTPUT);

    //pin to interrupt
    pinMode(2, INPUT_PULLUP);

    // turn on SPI in slave mode
    SPCR |= _BV(SPE);

    // turn on interrupts
    SPCR |= _BV(SPIE);

    // now turn on interrupts
    // interrupt for SS falling edge, pin D2
    attachInterrupt (0, ss_falling, FALLING);
    /* end SPI */

      Serial.println("Initialization Done!");
      state = IDLE;

      digitalWrite(DEBUG_LED, HIGH);
      digitalWrite(DEBUG_LED, LOW);

      digitalWrite(DEBUG_LED, HIGH);
      digitalWrite(DEBUG_LED, LOW);
      digitalWrite(DEBUG_LED, HIGH);

When I start the arduino I receive this serial output:

Serial initialized.
I2C in

As far as I understand the SIP interrupt and external interrupt are only activated in the end of the setup, after most of the debug messages, which means that the state of pins 2 and 10 shouldn’t
interfere before the interrupts be activated. However when pin 2 and 10 are connected to the ground or +5V it works, but when it’s “floating” it does not.

I tried to usenoInterrupts()/interrupts() and cli()/sei() but they make the whole system stop working…

Does anyone have any idea about what is going on?

PS: I wasn’t sure whether I should or not create a new post, as the problem looks the same.