Timing issue with reading thermometer (I2C) and a set of buttons

This is my first posting in the Forum. If something is wrong or not done properly, please let me know so that I can improve.

I am new to the Arduino world, I started to develop a pretty complex application (for me it is). My project is to control a Sugar Shack, eventually.

I brooked down my project in sub projects so that I can learn of to used and program the Arduino.

My first sub-project was to set a bunch of thermometers using I2C and read the different thermometers (DS18B20). I have 6 of them on the breadboard. I developped a function that read all of them is sequence, another function will print the different temperatures and a few more functions. I also added to my project a RTC DS1307, I will eventually develop a data logger function using a SD (this is for later on). This first sub-project works like charm on his own.

Second, I develop another sub-project consisting of 7 buttons: 6 of them are used to turn on/off 3 different relays, the 7th will be used to display the different temperature in rotation. I also have a set of LED that represent the state of the relays, these are controlled with a 74HC595. For this sub project, I used the "Example code for multi-button checker with debouncing " which could be found at http://www.adafruit.com/blog/2009/10/20/example-code-for-multi-button-checker-with-debouncing/. I used the second example with the Interrupt with Timer2.

I am now trying to merge the 2 sub-projects into one. I cannot get the buttons to respond on timely basis if at all. I tried to deactivate the interrupt, changing my subroutine to read the buttons states in the main loop after the function that read the temperature. However the respond time is too slow when a button is pressed, I have to hold to the button for quite a while.

My question is : does the the interrupt (Timer2) can coexist with the reading of the thermometers on I2C. ( I think that there is critical timing between the Arduino and the sensors). It it can coexist, any suggestion on how to make it works. If not, any suggestion, how I can solve my problem. The temperature must be read regularly and I must be able to press a button and react to it quickly. A quick push on the button, I don't want to have to hold the button down for a lon period of time.

Which arduino do you have?

I've looked over the atmega328P and atmega328 datasheet and there shouldn't be any issues. The TWI/I2C bus doesn't seem to have any conflicting interaction with any GPIO pins.

What are you using to read the temperatures? The DS18B20 needs nearly a second to return temperatures at 12 bit resolution, so if you are reading six of them synchronously, that might account for the lack of response to the buttons. As an alternative, you can issue an asynchronous request to each device to do the read and use millis to tell you when to go back and collect the temps. Meanwhile, you can poll your switches much more frequently.

Following is my function that read every thermometers :
void get_Temperature()
sensors.requestTemperatures(); // call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
for (byte i=0; i <= nb_Thermometer-1; i++){
float tempC = sensors.getTempC(temperature_Address*);*

  • //Serial.println(tempC);*
  • if (tempC == -127.00) {*
  • Serial.print(“Error getting temperature”);*
  • delay(5000);*
  • } else*
    temperature_Value = tempC;
    if (temperature_Value != previous_temperature_Value ){
    temperature_Change = true;
    * delay(1);*
    * }*
    * }*
    Then my interrupt settup is as follow:
    * // Run timer2 interrupt every 15 ms*
    * TCCR2A = 0;*
    * TCCR2B = 1<<CS22 | 1<<CS21 | 1<<CS20;*

* //Timer2 Overflow Interrupt Enable*
* TIMSK2 |= 1<<TOIE2; *

Sometime, I get an almost instantaneous reaction to the button being pressed, other time I have to hold the button much longer to get a reaction.
Thank you for your feedback_