Arduino:当包含 SD 库时 Oled 不工作

问题描述

我目前正在尝试将数据记录器与 OLED 屏幕组合在一起,但在将 SD 库添加到脚本时遇到了问题。我已经看到其他人在结合与 RAM 相关的 Oled 和 SD 卡时收到错误消息。虽然在我的情况下,代码运行,没有错误消息,但屏幕没有显示任何内容。下面的代码显示了“OLED 代码”,当我添加“#include ”时,Oled 显示器不会显示任何内容。另外,由于我对此很陌生,因此也欢迎对代码本身提出一些建设性的批评

谢谢!

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Adafruit_MCP9808.h"
#include <Adafruit_BME280.h>

Adafruit_BME280 bme; // use I2C interface
Adafruit_Sensor *bme_temp = bme.getTemperatureSensor();
Adafruit_Sensor *bme_pressure = bme.getPressureSensor();
Adafruit_Sensor *bme_humidity = bme.getHumiditySensor();

//oled deFinitions
#define SCREEN_WIDTH 128 // OLED display width,in pixels
#define SCREEN_HEIGHT 64 // OLED display height,in pixels

// Declaration for an SSD1306 display connected to I2C (SDA,SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH,SCREEN_HEIGHT,&Wire,OLED_RESET);

// Create the MCP9808 temperature sensor object
Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();



void setup() {
   Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC,0x3D);  // Address 0x3D for 128x64
// Clear the buffer
  display.display();
  delay(2000);
  display.cleardisplay();
  display.setTextSize(1);
  display.setCursor(0,0);
  display.setTextColor(WHITE);


while (!Serial);
Serial.println("Loading");
 if (!tempsensor.begin(0x18)) {
    Serial.println("Couldn't find MCP9808! Check your connections and verify the address is correct.");
    while (1);
  }
    
   Serial.println(F("Found MCP9808!"));
 tempsensor.setResolution(3); // sets the resolution mode of reading,the modes are defined in the table bellow:
  // Mode Resolution SampleTime
  //  0    0.5°C       30 ms
  //  1    0.25°C      65 ms
  //  2    0.125°C     130 ms
  //  3    0.0625°C    250 ms


if (!bme.begin()) {
    Serial.println(F("Could not find a valid BME280 sensor,check wiring!"));
    while (1) delay(10);
  }
  
  bme_temp->printSensorDetails();
  bme_pressure->printSensorDetails();
  bme_humidity->printSensorDetails();



}

void loop() {
  float temp = bme.readTemperature();    // get temperature in degree Celsius
  float humi = bme.readHumidity();       // get humidity in rH%
  float pres = bme.readPressure();       // get pressure in Pa

  tempsensor.wake();
  //Serial.println (tempsensor.getResolution());
  float c = tempsensor.readTempC();
  float f = tempsensor.readTempF();

  
  //float sensor=analogRead(A4);
  display.setCursor(0,0);
  display.print(F("Temp_1: "));
  display.print(c,2);display.println(" \tC ");
  display.println("");
  display.print(F("Temp_2: "));
  display.print(temp,2);display.println(" \tC ");
  display.println("");
  display.print(F("Humidity: "));
  display.print(humi,2);display.println(" %rH ");
  display.println("");
  display.print(F("Pressure: "));
  display.print(pres/100,2);display.println(" kPa ");

  
  display.display(); 
  delay(1000);
  display.cleardisplay();

解决方法

这显然是内存问题。

我现在连接了 Arduino Mega 而不是 Nano,并且代码运行良好。我还添加了记录传感器读数所需的剩余代码。

根据我在其他论坛上看到的内容,SD 和 SPI 库占用了相当多的内存。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...