Hey there, I've been having some trouble using these both modules on my Arduino Nano.
First of all, I must clarify that I have read a lot trying to solve this but still can't find a solution. My project consists in a 2 channel relay controller. The relays are activated together during a certain amount of time when a button is pressed. Every time button is pressed, my code adds 1 to a counter variable.This project requires to energize/de-energize my Arduino Nano, so, in order to keep track of how many times the relays have been activated, I'm using a micro SD module.
Until this point my code works just fine, but the thing is that I'm not the one who is going to use this circuit, there's going to be some other guys who aren't as careful as me, and they need to know how many times the buttons has been pressed. To avoid them touching anything inside the box where I'll be placing the Arduino, relays and SD module is the reason why I want to add a display to the circuit.
I have this display, which uses I2C communication

This is the SD module, which uses SPI communication

I've tryied all the example codes of the Adafruit SSD1306 library they all work just fine with the display, but the problem begins when I try to add this to my relay controller code. When I add the SSD1306 display commands, the SD module stops working, so It's impossible to know outside the serial monitor how many times the button has been pressed.
I've been doing a lot of research about this topic, but I still am not sure if it's a software or hardware problem. This is my code
//Libraries
#include <SD.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//Variables
const int SDchipSelect = 4;
const String ID = "AO3_001";
const int button = 7;
const int relay_gen = 6;
const int relay_bom = 5;
const long rl_gen_on = 6000;
const long rl_bom_on = 5000;
String logFile = "LOG.txt";
int counter;
//Display
Adafruit_SSD1306 display(-1);
void setup(){
Serial.begin(9600);
//Start SD card
if (!SD.begin(SDchipSelect)) {
Serial.println("SD card: Not found :(");
while (1);
}
else {
Serial.println("SD card: OK!");
}
//Define IO pins
pinMode(button, INPUT);
pinMode(relay_gen, OUTPUT);
pinMode(relay_bom, OUTPUT);
digitalWrite(relay_gen, HIGH);
digitalWrite(relay_bom, HIGH);
//Display init
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
}
void loop(){
//Wait for button pressing
if(digitalRead(button) == true){
//set counter new value
counter = SD_counter(logFile,true);
Serial.println(counter);
//display new counter value
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Cicles: " + String(counter));
display.display();
//activate relays
relay_control(rl_gen_on, rl_bom_on, relay_gen, relay_bom);
}
else{
//if button not pressed just show actual value
counter = SD_counter(logFile,false);
Serial.println(counter);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Cicles: " + String(counter));
display.display();
}
}
void relay_control (long ON_TIME_GEN, long ON_TIME_BOM, int relayPin, int relayPin2){
/* just some timers to control relays. This doesn't mess with anything*/
}
int SD_counter(String fileName, bool buttonPressed){
//Initial variables
String count_str;
int count_int;
//fetch the value stored in the SD card
File readFile = SD.open(fileName, FILE_READ);
if (readFile){
while (readFile.available()) {
char ltr = readFile.read();
count_str += ltr;
}
readFile.close();
}
else{readFile.close();}
//value parsing
count_int = count_str.toInt();
//what to do when the button is pressed
if (buttonPressed == false){
//if the button isn't pressed, just return the value stored in the SD card
return count_int;
}
if (buttonPressed == true){
//if the button is pressed, add 1 to the counter variable
count_int += 1;
//remove logFile. This is because I had some trouble overwriting the existing file,
//so now I delete it and save a new one
SD.remove(fileName);
File writeFile = SD.open(fileName, FILE_WRITE);
if (writeFile){
writeFile.print(String(count_int));
writeFile.close();
}
else{writeFile.close();}
//return new value
return count_int;
}
}
The SD counter works just fine, if I delete all the display code lines, it refreshes the value of the counter without further problems, but when I add them the Arduino becomes unable to read anything inside the SD module and just shows me 0 (if button not pressed) and 1 (if button pressed) in the serial monitor.
I'm not sure if there's a conflict between SPI and I2C, I mean, there shouldn't be any problem but here I am without knowing why the heck the OLED can't function alongside the SD module.