oled display not working in this sketch ?

Im making a data logger with an OLED display I2c on pins 12,13 on a MEGA 2560 the code runs fine I get everything on the serial mon. but NO display ?? WTH am I missing ? I've gone over it many times and cannot find the errors , I tried running the examples and the display works just fine BTW its a 128x64 version. I read that this display used the 1106 chip

/*
  SD card read/write

  This example shows how to read and write data to and from an SD card file
  The circuit:
  SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
*/
//#include "HX711.h"

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <max6675.h>
#include <TimerOne.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#define OLED_ADDRESS   0x3C
#define OLED_RESET -1
Adafruit_SH1106 display(OLED_RESET);
int RX = 12, TX = 13; // gps pins

TinyGPSPlus gps;
SoftwareSerial gpssoft(RX, TX);

float tempsen;

#define FILE_BASE_NAME "Data"
File file;
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[] = FILE_BASE_NAME "00.csv";
const int chipSelect = 4;

int thermoDO = 8;
int thermoCS = 9;
int thermoCLK = 5;
int displayspeed();

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

//rpm/interrupt variables

byte numCount = 5; //rising edge count give numCount - 1 intervals to measure
volatile byte count;
volatile unsigned long startTime;
volatile unsigned long endTime;
volatile boolean finishCount = false;
unsigned long period;

float rpm;

//switch state indexes
#define readHall 1
#define readspeed 2  //readScale 2
#define writeSD 3
#define readTemp 4


byte hallPin = 2;
byte state = readHall;
byte timerTestOutputPin = 10;

void isrCount()
{
  if (count == 0)//first entry to isr
  {
    startTime = micros();
  }
  if (count == numCount)
  {
    endTime = micros();
    detachInterrupt(digitalPinToInterrupt(hallPin));
    finishCount = true;
  }
  count++; //increment after test for numCount
}

void setup() {

  Timer1.initialize(50000); //50 ms pulse length 20 Hz = 1200 rpm
  Serial.begin(115200);
  gpssoft.begin(9600);
  display.begin(SH1106_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.display();

  //test output jumper pin 10 to pin 2
  pinMode(10, OUTPUT);

  if (!SD.begin(chipSelect)) {
    Serial.println(F("begin failed"));
    return;
  }
  while (SD.exists(fileName)) {
    if (fileName[BASE_NAME_SIZE + 1] != '9') {
      fileName[BASE_NAME_SIZE + 1]++;
    } else if (fileName[BASE_NAME_SIZE] != '9') {
      fileName[BASE_NAME_SIZE + 1] = '0';
      fileName[BASE_NAME_SIZE]++;
    } else {
      Serial.println(F("Can't create file name"));
      return;
    }
  }
  //make the hall pin an input:
  pinMode(hallPin, INPUT);//maybe should be input pullup?

  //test signal
  // pinMode(10, OUTPUT);
  EIFR = bit (INTF0);//clear any interrupt flag
  attachInterrupt(digitalPinToInterrupt(hallPin), isrCount, RISING);//interrupt on pin 2
  Timer1.pwm(timerTestOutputPin, 5);//short pulse aprox .5% duty cycle emulate magnet
}

void loop() {
  switch (state) {
    case readHall:
      if (finishCount == true)
      {
        finishCount = false;//reset flag
        count = 0;
        period = (endTime - startTime);// micros
        float avgPeriod = period / numCount; //5 intervals in measurement
        // Serial.println(avgPeriod, 1); //float
        rpm = 60.0 * (1000000 / avgPeriod);
        Serial.print("RPM = ");
        Serial.println(rpm, 1);
        display.setTextSize(2);
        display.setTextColor(WHITE);
        display.setCursor(6, 0);
        display.print("         ");//9 spaces
        display.setCursor(6, 0);
        display.print(rpm);
        state = readspeed;
      }
      break;
      while (gpssoft.available() > 0)
        if (gps.encode(gpssoft.read()))

        case readspeed:
      Serial.print("speed MPH ");
      Serial.println(gps.speed.mph());
      Serial.println(gps.location.lat(), 6);
      display.setTextColor(WHITE);
      display.clearDisplay();
      display.setTextSize(1);
      display.setCursor(35, 20);
      display.print("SPEED(MPH)");
      display.print(gps.speed.mph());
      display.display();
      delay (100);
      state = readTemp; //writeSD;
      break;

    case writeSD:
      Serial.println("write SD");
      EIFR = bit (INTF0);//clear interrupt flag
      attachInterrupt(digitalPinToInterrupt(hallPin), isrCount, RISING);//interrupt on pin2
      file.print(rpm);
      file.print(",");
      //    file.print(scaleReading, 1);
      file.print(",");
      file.println(tempsen);
      file.close();
      state = readHall;
      break;

    case readTemp:
      tempsen = thermocouple.readFahrenheit();
      Serial.println("readTemp");
      Serial.println(tempsen);
      display.setCursor(12, 1);
      display.print(tempsen);
      state = writeSD;
      break;

  }
}

Is it I2C? If so, why don't you use the I2C pins on the Mega?

If it's SPI, pins 12 and 13 are not SPI pins on a Mega.

Correct you are !! that fixed it , I was using a Nano but ran out of memory and missed the pin changes

THANKS !

Well Ive got another problem , maybe I should make a new post ?
the SD card is not being written to , I modified the code from another working sketch that wrote to the SD card fine and implemented the name each time , now I get no files being written , it also bypasses the error message
" Can't create file name " line 110 ?
I compared all the code that relates to this function but it all seems to be ok ?? maybe it is being blocked ?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.