I am searched over the internet and found out a few similar questions, but non of them were helpfull, so I decided to open a new quetion.
I tried both SD and Adafruit_SSD1306 libraries and they both worked fine when they were separated - in different codes. The error came when I used them in one code.
In my programme I try to init SD card, write to it and then init dispaly (everything works fine). After that I do another write cycle into the SD, but I get an error in opening a file:
error opening test.txt
In other words I cant write anything into the SD card after oled display is initialized (display works then without any problems).
Here is my code:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <SD.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
TwoWire Wire1(PB9, PB8);// Use STM32 I2C2
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire1, -1);
const int chipSelect = PA15;
void setup() {
Serial1.begin(9600);
if (!SD.begin(chipSelect)) {
Serial1.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial1.println("card initialized.");
for (int i = 0; i < 10; i++)
{
File dataFile = SD.open("test.txt", FILE_WRITE);
if (dataFile) {
dataFile.println("Test");
dataFile.close();
Serial1.println("Test");
}
else {
Serial1.println("error opening test.txt");
}
delay(30);
}
delay(2000);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial1.println(F("SSD1306 allocation failed"));
}
delay(500);
for (int i = 0; i < 10; i++)
{
File dataFile = SD.open("dat_test.txt", FILE_WRITE);
if (dataFile) {
dataFile.println("Test");
dataFile.close();
Serial1.println("Test");
}
else {
Serial1.println("error opening test.txt"); //here is the error I get
}
delay(30);
}
}
void loop() {
}
Here is the output:
card initialized.
Test
Test
Test
Test
Test
Test
Test
Test
Test
Test
error opening test.txt
error opening test.txt
error opening test.txt
error opening test.txt
error opening test.txt
error opening test.txt
error opening test.txt
error opening test.txt
error opening test.txt
error opening test.txt
I am using STM32F103CB.