Do you need to code switches between SPI and I2C?

Hi all,

I’m writing a code where I switch between a I2C OLED and a SPI BLE, but can only get the OLED to start. Then the code either stops doing anything or loops back on itself to print the pixel on the OLED repeatedly. I’ve done quite a bit of digging on the forum and haven’t really seen anything that covers this.

Here’s a bare bones version of the code I’d like to have run:

//All OLED libraries
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

//BLE libraries
#include <boards.h>
#include <RBL_nRF8001.h>

//defining the display
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);


void setup () {
  Serial.begin (9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  delay(2000);
  display.clearDisplay();
  display.drawPixel(10, 10, WHITE); // simple draw pixel on the OLED
  display.display();
  delay(2000);
  display.clearDisplay();
  
  Serial.end();
  ble_set_name("MyBLE"); //Name the BLE
  ble_begin(); // initializes functions of the RBL setup file
  Serial.begin(57600);
}

void loop (){// straight from the RBL example
   if ( ble_available() )
  {
    while ( ble_available() )
      Serial.write(ble_read());
      
    Serial.println();
  }
  
  if ( Serial.available() )
  {
    delay(5);
    
    while ( Serial.available() )
        ble_write( Serial.read() );
  }
  
  ble_do_events();
  
}

I am using an arduino Uno and links to the two devices are:
BLE: http://redbearlab.com/bleshield/
OLED: http://www.amazon.com/Huhushop-TM-Serial-Display-Arduino/dp/B00JM88A94
(couldn’t seem to locate a datasheet for this one… it’s very similar to the adafruit OLED but only has 4 pins)

OLED Wiring is:
VCC->5V
GND → GND (analog)
SCL->A5
SDA->A4

Any suggestions of what’s going on?

You can use SPI and I2C in the same sketch no problem. They're completely separate hardware.

You must have an issue elsewhere in the code. Post the whole code so we can see.

The code I posted already doesn't function properly, which is why I was wondering what the problem was with the structure of the base code. It runs to the print pixel section and then either fails or loops the draw pixel step.

I’m writing a code where I switch between a I2C OLED and a SPI BLE, …

I don’t see SPI.begin() or Wire.begin() in your code.

Maybe the libraries do that.

Does the BLE shield work on its own? Try one of their examples.

Maybe you are running out of memory.

Try displaying how much free RAM you have. There are various ways of doing that, one is:

http://andybrown.me.uk/wk/2011/01/01/debugging-avr-dynamic-memory-allocation/

Hey!

The BLE works perfectly on its own, which is why I'm very confused. I was thinking it would either be a library or memory conflict too, but nothing is giving me an obvious hint that it's a library problem (as I have had a few of those trying to work the BLE and an SD reader together, and it would usually give some indication that something was wrong).

When the program compiles it looks like it's using about 19,500 kb out of the Uno's 32,500. The problem might be more that I'm not sure the code will run through a debug test as it usually just loops back on the draw pixel step.

Would the test still achieve the same goal if I just ran it on an empty code that just includes the #include statements? Or does it need to be run with the program code too?

Thank you so much for your help!

michaela5: When the program compiles it looks like it's using about 19,500 kb out of the Uno's 32,500.

The question wasn't about the program size. The question was about how much of the 2K of RAM is being used.

Ah, yeah I got that, I was just putting that out there while I tested the dynamic memory usage. I used the MemoryFree library and it's spitting out a value of -311 and nothing else, which I take to mean that the OLED and BLE are overusing the dynamic memory of the Arduino?

That's kind of a bummer seeing as I was really hoping to get the two working together. But I suppose back to the drawing board if the simplest example I can build overuses the RAM of the Arduino.

Would switching to the example Nick suggested make a significant enough difference or is it just going to be impossible to get the OLED and BLE working at the same time, regardless of which method I use?

Jump to '1284P, 16K SRAM, twice that of a Mega. Boards available in multiple form factors and easy to add to the IDE. https://github.com/JChristensen/mighty-1284p Select Bobuino as board type. http://www.crossroadsfencing.com/BobuinoRev17/ |500x209

michaela5: Ah, yeah I got that, I was just putting that out there while I tested the dynamic memory usage. I used the MemoryFree library and it's spitting out a value of -311 and nothing else, which I take to mean that the OLED and BLE are overusing the dynamic memory of the Arduino?

Very likely, based on that figure. Probably the OLED stuff keeps a memory buffer to hold all those shapes. You could see if you can find how big it is. Like Crossroads says, perhaps a board with more RAM if you want to run all that at once.

I use that OLED in several projects. The OLED library you’re using has a 1K byte buffer, as well as other RAM requirements.

You can go the Mega route to increase RAM size.
You can also use a different graphics library that uses a lot less ram (search for “u8glib”).
I personally use a text-only library with that OLED that uses about 50 bytes of RAM.

Yep, yep, yep! Turns out the Adafruit OLED library was using ~1200 of the 2000 bytes of RAM and that was causing the problem. Long story short, I switched to the u8glib and after some troubleshooting I got the BLE and OLED up and running (and using a significantly smaller portion of RAM)!

Big thanks and +1's to you Nick, and everyone else who contributed! You all nailed it! Thank you so much!