I2C 1306 library conflict with RCSwitch library?

Hi there!

I’ve got a problem with my “fan controll” project.
I had my PC fan connected to Arduino through mosfet, simple DT temp sensor and a few led indicating status of the fan, and RF receiver to manualy turn the fan and whole PC on/off. Last week I thought about expanding my projest with simple OLED display to show the temp.

I had everything working OK, but after I added the “display part”, the project kept restarting. I’ve checked my code for mistakes etc. but with no avail.
Tested my display, works fine and after a few bangs with my head against table, I tried to disable my “remote control” part.
Commented every line that had something to do with RF and the display started working. However I need the remote control too and I just can’t find a way to make it work together.

Whole code here: (working display, no RF control)

#include <SPI.h>
#include <Wire.h>
#include <OneWire.h>
#include <RCSwitch.h>
#include <DallasTemperature.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>
#include <splash.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1

Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);

#define ONE_WIRE_BUS 12 //Temp sensor conected to Pin 12

RCSwitch mySwitch = RCSwitch();
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// Serial outmut mode selection
//... 0 Off, 1 Standard Serial, 2 Python script
int serialMode = 1;

// Remote button codes
unsigned long remPowerButton = 1414;
//... for powering on the PC
unsigned long remManualOn = 13159848;
//... for manual mode ON
unsigned long remManualOff = 13159842;
//... for manual mode OFF

// Temp, serial and delay setting
const float onTemp = 37;    // When to turn on...
const float halfTemp = 36.7;
const float offTemp = 36;  // ...and when to turn off
const int interval = 10;   // Delay between Serial printout in sec.
const long manualDelay = 150; //While PC is OFF, delay "auto manual mode OFF" (sec)...
const int manModeExitTemp  = 33;  //... and until Temp drops below this point


// Pin Connections
int out = 3;
// PC case button (in my case active LOW)
//  not sure if it is te same for other motherboards
//  if needed, can be adjusted below (Ctrl+F)

int pwr = 4;        // PC power sense pin
int Rled = 5;       // Red output led
int Bled = 6;       // Blue output led
int butt = 8;       // PC button sense, active LOW
int Wled = 9;       // White output led
int fan = 10;       // Fan mosfet input


// Variables
long manTime;       // manualDelay countdown
int ledLock = 0;    // Lock LED state var
int over = 0;       // Fan mode override
int State = 0;      // State var depending on Temp settings
int buttLastState = 0;
int x;
float tempC;
unsigned long received;
unsigned long Time;
unsigned long lastTime = -14500;
unsigned long lastTime2 = 0;
unsigned long lastTime3 = 0;


void setup() {


  //TCCR1B = TCCR1B & B11111000 | B00000001;    // set timer 1 divisor to     1 for PWM frequency of 31372.55 Hz
  //TCCR1B = TCCR1B & B11111000 | B00000010;    // set timer 1 divisor to     8 for PWM frequency of  3921.16 Hz
  //TCCR1B = TCCR1B & B11111000 | B00000011;    // set timer 1 divisor to    64 for PWM frequency of   490.20 Hz
  //TCCR1B = TCCR1B & B11111000 | B00000100;    // set timer 1 divisor to   256 for PWM frequency of   122.55 Hz
  TCCR1B = TCCR1B & B11111000 | B00000101;    // set timer 1 divisor to  1024 for PWM frequency of    30.64 Hz



  //Setup display
  delay(1500);  // This delay is needed to let the display to initialize
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // Initialize display with the I2C address of 0x3C
  display.display();
  delay(1000);
  display.clearDisplay();  // Clear the buffer


  //Setup pins, etc.
  if (serialMode != 0) Serial.begin(115200);
  sensors.begin();
  analogWrite(out, 168);
  pinMode(butt, INPUT_PULLUP);
  pinMode(pwr, INPUT);
  pinMode(out, OUTPUT);
  pinMode(Rled, OUTPUT);
  pinMode(Wled, OUTPUT);
  pinMode(Bled, OUTPUT);
  pinMode(fan, OUTPUT);


  //  mySwitch.enableReceive(0);  // Receiver on interrupt 0 => that is pin #2



  //Read temp
  sensors.requestTemperatures(); // Send the command to get temperatures
  tempC = sensors.getTempCByIndex(0);
  if (tempC != DEVICE_DISCONNECTED_C) {
    if (serialMode == 1) Serial.println("Ready");
  }
  else {
    Serial.println("Temp sensor not connected!");
  }

  if (tempC <= offTemp) State = 0;
  else if (tempC >= onTemp) State = 2;
  else if (tempC >= halfTemp) State = 1;
}

void loop() {
  //Fan control

  if (over == 1) {
    digitalWrite(fan, HIGH);
    if (ledLock == 0) {
      if (Time - lastTime3 > 500) {
        lastTime3 = Time;
        if (x == 1) {
          analogWrite(Rled, 64);
          analogWrite(Bled, 32);
          x = 0;
        }

        else {
          analogWrite(Rled, LOW);
          analogWrite(Bled, LOW);
          x = 1;
        }
      }
    }
  }
  else if (State == 2) {
    digitalWrite(fan, HIGH);
    if (ledLock == 0) {
      analogWrite(Rled, 128);
      analogWrite(Bled, LOW);
    }
  }
  else if (State == 1) {
    analogWrite(fan, 128);
    if (ledLock == 0) {
      analogWrite(Rled, 16);
      analogWrite(Bled, 5);
    }

  }
  else if (digitalRead(pwr) == 1) {
    analogWrite(fan, LOW);
    if (ledLock == 0) {
      analogWrite(Rled, LOW);
      analogWrite(Bled, 5);
    }
  }
  else {
    digitalWrite (fan, LOW);
    if (ledLock == 0) {
      analogWrite(Rled, LOW);
      analogWrite(Bled, LOW);
    }
  }
  if (over == 1 && tempC < manModeExitTemp && Time - lastTime2 > manualDelay * 1000) over = 0;

  //Serial Output & temp check
  Time = millis();
  if (Time - lastTime >= interval * 1000) {
    sensors.requestTemperatures();
    tempC = sensors.getTempCByIndex(0);
    if (!over) {
      if (tempC > onTemp) State = 2;
      else if (State == 2 && tempC < halfTemp) State = 1;
      else if (State == 0 && tempC > halfTemp) State = 1;
      else if (tempC <= offTemp) State = 0;
    }
    if (serialMode == 1) serialClassic();
    if (serialMode == 2) serialPython();

    lastTime = Time;

    display.clearDisplay();
    display.setTextColor(SSD1306_WHITE);
    if (digitalRead(pwr) == 0) {
      display.setTextSize(2);
      display.setCursor(42, 32);
      display.println(tempC, 1);
    }
    else {
      display.setTextSize(5); // Draw 2X-scale text
      display.setCursor(0, 20);
      display.println(tempC, 1);
    }
    display.display();
  }




  /*
    //Remote check
    if (mySwitch.available()) {
    received = mySwitch.getReceivedValue();
    mySwitch.resetAvailable();
    digitalWrite(Wled, 64);
    delay(2);
    digitalWrite(Wled, LOW);
    }
    if (received == remPowerButton) {
    digitalWrite(out, LOW);
    digitalWrite(Wled, 255);
    received = 1;
    ledLock = 1;
    mySwitch.resetAvailable();
    lastTime2 = Time;
    }
    if (Time - lastTime2 > 800 && received != remPowerButton) {
    if (received == 1) {
      received = 0;
      ledLock = 0;
      digitalWrite(out, HIGH);
      digitalWrite(Wled, LOW);
      lastTime = Time - (interval * 990);
    }
    }
    if (received == remManualOn && Time - lastTime2 > 600) {
    State++;
    over = 1;
    if (State >= 2) {
      State = 2;
    }
    lastTime2 = Time;
    received = 0;
    mySwitch.resetAvailable();
    lastTime = Time - (interval * 990);
    }
    else if (received == remManualOff && Time - lastTime2 > 600) {
    State--;
    if (State <= 0) {
      over = 0;
      State = 0;
    }
    lastTime2 = Time;
    received = 0;
    mySwitch.resetAvailable();
    lastTime = Time - (interval * 990);
    }
  */



  //Button check
  if (digitalRead(butt) == LOW) {
    digitalWrite(out, LOW);
    digitalWrite(Wled, 255);
    ledLock = 1;
    buttLastState = 1;
  }
  else if (buttLastState == 1) {
    analogWrite(out, HIGH);
    digitalWrite(Wled, LOW);
    buttLastState = 0;
    ledLock = 0;
    lastTime = Time - (interval * 900);
  }
}

Functions “serialClassic” and “serialPython” are just simple printout functions. If needed, will paste them here later.

Has anyone had similar problems or is something wrong with my code/libraries ?

Thanks for help!

How much free dynamic memory does the Arduino IDE tell you is available for local variables when you compile the non-working code? The display libraries tend to be very high users of dynamic memory and if you don't leave enough to spare for your local variables that could cause the restarts.

It is wise to enable Verbose Compile from File->Prefences
Then copy-paste only the Library + Memory usage lines to your message e.g.

Using library SPI at version 1.0 in folder: C:\Program Files (x86)\Arduino-1.8.9\hardware\arduino\avr\libraries\SPI 
Using library Wire at version 1.0 in folder: C:\Program Files (x86)\Arduino-1.8.9\hardware\arduino\avr\libraries\Wire 
Using library Adafruit_GFX_Library at version 1.6.1 in folder: C:\Users\David Prentice\Documents\Arduino\libraries\Adafruit_GFX_Library 
Using library Adafruit_SSD1306 at version 2.0.1 in folder: C:\Users\David Prentice\Documents\Arduino\libraries\Adafruit_SSD1306 
Using library U8g2_for_Adafruit_GFX at version 1.5.2 in folder: C:\Users\David Prentice\Documents\Arduino\libraries\U8g2_for_Adafruit_GFX 
"C:\\Program Files (x86)\\Arduino-1.8.9\\hardware\\tools\\avr/bin/avr-size" -A "C:\\Users\\DAVIDP~1\\AppData\\Local\\Temp\\arduino_build_112490/UFT8_progs.ino.elf"
Sketch uses 23112 bytes (71%) of program storage space. Maximum is 32256 bytes.
Global variables use 452 bytes (22%) of dynamic memory, leaving 1596 bytes for local variables. Maximum is 2048 bytes.

Like pert, I suspect you are short of dynamic memory. The Adafruit_SSD1306 library allocates 1024 bytes at runtime. So my example only has 1596 - 1024 = 572 bytes for local variables.

David.

Thanks for reply!

I’m using RaspberryPi so my output is a little bit different - its too long for one reply(over 9000chars) … here’s the end of verbose output:

...
Sketch uses 20330 bytes (66%) of program storage space. Maximum is 30720 bytes.
Global variables use 793 bytes (38%) of dynamic memory, leaving 1255 bytes for local variables. Maximum is 2048 bytes.

So if I get it right, I have about 221 bytes for variables (1255 - 1024). shouldn’t that be enough?

If not - how would I solve it ?

You managed to copy-paste the Memory usage lines.
Surely you could copy-paste the Library usage lines too.

I showed you an example.

Yes, 221 bytes SRAM might be safe enough. But your symptoms suggest otherwise.

David.

Yes, I can but as I stated above - the text is too long for this forum.
I’m attaching it as an .txt

output.txt (37.1 KB)

Go on. This took me 30 secs:

Multiple libraries were found for "SPI.h"
 Used: /home/pi/Desktop/Arduino-install/arduino-1.8.10/hardware/arduino/avr/libraries/SPI
Multiple libraries were found for "splash.h"
 Used: /home/pi/Arduino/libraries/Adafruit_SSD1306
Multiple libraries were found for "Wire.h"
 Used: /home/pi/Desktop/Arduino-install/arduino-1.8.10/hardware/arduino/avr/libraries/Wire
Multiple libraries were found for "RCSwitch.h"
 Used: /home/pi/Arduino/libraries/rc-switch
Multiple libraries were found for "OneWire.h"
 Used: /home/pi/Arduino/libraries/OneWireNoResistor-1.0
Multiple libraries were found for "DallasTemperature.h"
 Used: /home/pi/Arduino/libraries/Arduino-Temperature-Control-Library-master
Multiple libraries were found for "Adafruit_SPITFT.h"
 Used: /home/pi/Arduino/libraries/Adafruit_GFX_Library
Using library Adafruit_GFX_Library at version 1.5.6 in folder: /home/pi/Arduino/libraries/Adafruit_GFX_Library 
Using library SPI at version 1.0 in folder: /home/pi/Desktop/Arduino-install/arduino-1.8.10/hardware/arduino/avr/libraries/SPI 
Using library Adafruit_SSD1306 at version 2.0.0 in folder: /home/pi/Arduino/libraries/Adafruit_SSD1306 
Using library Wire at version 1.0 in folder: /home/pi/Desktop/Arduino-install/arduino-1.8.10/hardware/arduino/avr/libraries/Wire 
Using library rc-switch at version 2.6.2 in folder: /home/pi/Arduino/libraries/rc-switch 
Using library OneWireNoResistor-1.0 at version 1.1 in folder: /home/pi/Arduino/libraries/OneWireNoResistor-1.0 
Using library Arduino-Temperature-Control-Library-master at version 3.8.0 in folder: /home/pi/Arduino/libraries/Arduino-Temperature-Control-Library-master 
/home/pi/Desktop/Arduino-install/arduino-1.8.10/hardware/tools/avr/bin/avr-size -A /tmp/arduino_build_319265/PC_Remote_Python-ready_OLED2.ino.elf
Sketch uses 20330 bytes (66%) of program storage space. Maximum is 30720 bytes.
Global variables use 793 bytes (38%) of dynamic memory, leaving 1255 bytes for local variables. Maximum is 2048 bytes.

David.

OK, Ill try again, but I absolutely dont understand what do you want from me. I already posted the whole output here as an TXT because it is too long.
But OK - Here’s stripped down version:

Multiple libraries were found for "splash.h"
 Used: /home/pi/Arduino/libraries/Adafruit_SSD1306
Multiple libraries were found for "Wire.h"
 Used: /home/pi/Desktop/Arduino-install/arduino-1.8.10/hardware/arduino/avr/libraries/Wire
Multiple libraries were found for "RCSwitch.h"
 Used: /home/pi/Arduino/libraries/rc-switch
Multiple libraries were found for "OneWire.h"
 Used: /home/pi/Arduino/libraries/OneWireNoResistor-1.0
Multiple libraries were found for "DallasTemperature.h"
 Used: /home/pi/Arduino/libraries/Arduino-Temperature-Control-Library-master
Multiple libraries were found for "Adafruit_SPITFT.h"
 Used: /home/pi/Arduino/libraries/Adafruit_GFX_Library
Multiple libraries were found for "SPI.h"
 Used: /home/pi/Desktop/Arduino-install/arduino-1.8.10/hardware/arduino/avr/libraries/SPI
Using library Adafruit_GFX_Library at version 1.5.6 in folder: /home/pi/Arduino/libraries/Adafruit_GFX_Library 
Using library SPI at version 1.0 in folder: /home/pi/Desktop/Arduino-install/arduino-1.8.10/hardware/arduino/avr/libraries/SPI 
Using library Adafruit_SSD1306 at version 2.0.0 in folder: /home/pi/Arduino/libraries/Adafruit_SSD1306 
Using library Wire at version 1.0 in folder: /home/pi/Desktop/Arduino-install/arduino-1.8.10/hardware/arduino/avr/libraries/Wire 
Using library rc-switch at version 2.6.2 in folder: /home/pi/Arduino/libraries/rc-switch 
Using library OneWireNoResistor-1.0 at version 1.1 in folder: /home/pi/Arduino/libraries/OneWireNoResistor-1.0 
Using library Arduino-Temperature-Control-Library-master at version 3.8.0 in folder: /home/pi/Arduino/libraries/Arduino-Temperature-Control-Library-master 
/home/pi/Desktop/Arduino-install/arduino-1.8.10/hardware/tools/avr/bin/avr-size -A /tmp/arduino_build_319265/PC_Remote_Python-ready_OLED2.ino.elf
Sketch uses 20330 bytes (66%) of program storage space. Maximum is 30720 bytes.
Global variables use 793 bytes (38%) of dynamic memory, leaving 1255 bytes for local variables. Maximum is 2048 bytes.
/home/pi/Desktop/Arduino-install/arduino-1.8.10/hardware/tools/avr/bin/avrdude -C/home/pi/Desktop/Arduino-install/arduino-1.8.10/hardware/tools/avr/etc/avrdude.conf -v -patmega328p -carduino -P/dev/ttyUSB0 -b57600 -D -Uflash:w:/tmp/arduino_build_319265/PC_Remote_Python-ready_OLED2.ino.hex:i 

avrdude: Version 6.3-20190619
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/home/pi/Desktop/Arduino-install/arduino-1.8.10/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/home/pi/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyUSB0
         Using Programmer              : arduino
         Overriding Baud Rate          : 57600
         AVR Part                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : Arduino
         Description     : Arduino
         Hardware Version: 2
         Firmware Version: 1.16
         Vtarget         : 0.0 V
         Varef           : 0.0 V
         Oscillator      : Off
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: reading input file "/tmp/arduino_build_319265/PC_Remote_Python-ready_OLED2.ino.hex"
avrdude: writing flash (20330 bytes):

Writing | ################################################## | 100% 6.23s

avrdude: 20330 bytes of flash written
avrdude: verifying flash memory against /tmp/arduino_build_319265/PC_Remote_Python-ready_OLED2.ino.hex:
avrdude: load data flash data from input file /tmp/arduino_build_319265/PC_Remote_Python-ready_OLED2.ino.hex:
avrdude: input file /tmp/arduino_build_319265/PC_Remote_Python-ready_OLED2.ino.hex contains 20330 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 4.81s

avrdude: verifying ...
avrdude: 20330 bytes of flash verified

avrdude done.  Thank you.

I give up. You are deliberately wasting reader's time and patience.

david_prentice:
I give up. You are deliberately wasting reader’s time and patience.

Really? Are you kidding me now? I did exactly what you asked for… but sure - mabye there’s someone else who could actually help me