Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

2
I2C OLED isn’t displaying like I expected
Post Flair (click to view more posts with a particular flair)
Post Body

To preface this, I’m very new to coding, and I have narrowed the issue down to a software error as on smaller simpler codes it displays just fine using the exact same wiring. This sketch uses a DHT22, arduino nano, ssd1306 i2c 128x64 OLED, and DS1307 RTC. Right now some of the functions are a bit empty but that’s because I’m moving through them one by one.

The issue is that I thought getting the switch case to call to each of the functions it would perform everything in the function and then switch to the next one after 3000ms, but the screen is just staying blank the whole time. I know the draw states are switching because of the serial monitor, and it's even executing the code within the drawHum function because it also writes to the serial monitor. It's just the oled lines that don't do anything, but I don't know why.

Here’s the code, any help would be much appreciated:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>

#define SCREEN_WIDTH 128 // OLED display width,  in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define DHTPIN 8     // Digital pin connected to the DHT sensor
#define DHTTYPE    DHT22     // DHT 22 (AM2302)

int draw_state = 0;
unsigned long previousMillis = 0;
long interval = 3000;

//DateTime now = rtc.now();
//char buf1[] = "hh:mm";
//char buf3[] = "DDD, MMM DD YYYY";
DHT dht(DHTPIN, DHTTYPE);

Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // // create SSD1306 display object connected to I2C
RTC_DS3231 rtc;

String time;

void setup() {
  Serial.begin(9600);
  dht.begin();
  oled.begin();
  // initialize OLED display with address 0x3C for 128x64
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    while (true);
  }

  //delay(2000);         // wait for initializing
  //oled.clearDisplay(); // clear display
  //oled.display();

  // SETUP RTC MODULE
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (true);
  }

  // automatically sets the RTC to the date & time on PC this sketch was compiled
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__))  TimeSpan(15));
  time.reserve(10); // to avoid fragmenting memory when using String
}

void loop() {

  //DateTime now = rtc.now();
  //char buf3[] = "DDD, MMM DD YYYY";
  //char buf1[] = "hh:mm";
  unsigned long currentMillis = millis();
   if (currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    switch (draw_state) {

    case 0: 
      oled.clearDisplay();
      drawInHum(); 
      oled.display();
      break;

    case 1: 
      oled.clearDisplay();
      drawInTemp(); 
      oled.display();
      break;

    case 2: 
      oled.clearDisplay();
      drawDay(); 
      oled.display();
      break;
      }
     draw_state  ;
     if (draw_state > 2) {
     draw_state = 0;
  }
 }
}
/*oled.clearDisplay();
oled.setCursor(0,0);
delay(1000);
 DateTime now = rtc.now();
char buf3[] = "DDD, MMM DD YYYY";
//oled.println(now.toString(buf3));
//oled.display();*/


void drawInHum(){
 DateTime now = rtc.now();
 char buf1[] = "hh:mm";
 float h = dht.readHumidity();
 Serial.print(F("Humidity: "));
 Serial.print(h);
 Serial.println(F("%"));
 Serial.println(now.toString(buf1));
 oled.setCursor(50,50);
 oled.setTextSize(1);
 oled.println(F("Hello World!"));
}

void drawInTemp(){
Serial.println(F("In Temp"));
}

void drawDay(){
Serial.println(F("Day"));
}

Author
Account Strength
40%
Account Age
7 months
Verified Email
Yes
Verified Flair
No
Total Karma
9
Link Karma
9
Comment Karma
n/a
Profile updated: 47 minutes ago
Posts updated: 3 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
6 months ago