Having a bit of a problem with Adafruit SSD1306 library not being able to initialise I2C OLED display when I include while (CAN_OK != CAN.begin(CAN_500KBPS)) line for initialising MCP2515 CAN transceiver in the setup() function.
#include <stdio.h> // for function sprintf
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "mcp_can.h"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
long unsigned int rxId;
unsigned long rcvTime;
const int SPI_CS_PIN = 10;
MCP_CAN CAN(SPI_CS_PIN);
void setup() {
//Serial.begin(9600);
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // on error loop forever
}
// show splash screen initialised by Adafruit library
display.display();
delay(2000);
ClearDisplayBufferAndInitText();
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
ClearDisplayBufferAndInitText();
display.println("CAN BUS module failed to initialized");
display.println("retrying....");
display.display();
delay(200);
}
ClearDisplayBufferAndInitText();
display.println("CAN BUS module initialized!");
display.display();
delay(2000);
} // end of setup()
void loop() {
ClearDisplayBufferAndInitText();
display.println("read & display CAN bus massages");
delay(2000);
} // end of loop()
void ClearDisplayBufferAndInitText() {
display.clearDisplay(); // clear display buffer
// init text properties
display.setTextSize(1); // normal 1:1 pixel scale
display.setTextColor(WHITE); // draw white text
display.setCursor(0, 0); // start at top-left corner
display.cp437(true); // use full 256 char 'Code Page 437' font
}
I have confirmed that the OLED display can initialise and display stuff.
I have confirmed that the CAN transceiver can initialise and print stuff to serial monitor.
But when I try to put them together I get the error that OLED failed to initialise and the code goes into an infinite loop.
Can someone throw their eye over the code and let me know if some obvious mistake that I fail to see.
Just to add to the confusion I am using Arduino Nano connecting modules as follows:
OLED pin | Nano pin |
---|---|
GND | GND |
VDD | 3.3V |
SCK | A5 |
SDA | A4 |
MCP2515 pin | Nano pin |
---|---|
VCC | 5V |
GND | GND |
CS | D10 |
CS | D11 |
SO | D12 |
SI | D13 |
INT | NC |