General programming question trying to merge two programs

Hey everyone, I’m a bit new to arduino and c++, and am trying to combine two programs to make a dht11 temperature sensor display to an 0.96” OLED screen, using the adafruit unified sensor and u8g2 libraries respectively (using an Arduino Uno).

I’ve successfully gotten the temperature readout and hello world example programs to run separately, but how do I merge the two? I know c++ is strictured in a few parts, as far as defining variables then executing functions and loops, but I’m not sure how to put the two together. Thanks!

Read this:

http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

.

Oh, thanks a lot!

Hi,
Here's the approach I find best for merging sketches:

http://arduino-info.wikispaces.com/CombiningArduinoSketches

Thanks for the help! Well, I've at least gotten it to successfully compile and upload if I copy and paste things from their respective locations in the code, but since I'm trying to output part of it I need to insert it into where it displays "hello world". I'd imagine I would need to execute parts before and in that part, but not sure how to tell what should be where

When you run into a problem, show us your sketch and tell us what’s not working.

Always show us your ‘current’ compete sketch.
Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.
[code] Paste sketch here. [/code]

.

Oh yeah, of course, sorry. Ive attached the two separate files and the mess ive made (humidity_test).

DHT_Unified_Sensor.ino (3.1 KB)

humidity_test.ino (15.8 KB)

HelloWorld.ino (12.7 KB)

Humble bump

Assuming humidity_test.ino is the merged sketch, it's too big to post inline.

//Edit
OK, if OP removes all those commented constructors in the beginning of the code, it goes down to roughly 5kB and can be posted inline :wink:

Just to give the idea

void loop(void)
{
  u8x8.setFont(u8x8_font_chroma48medium8_r);
  //u8x8.drawString(0,0,"Hello World!");
  //u8x8.refreshDisplay();    // only required for SSD1606/7  
  delay(2000);


  // Delay between measurements.
  delay(delayMS);
  // Get temperature event and print its value.
  sensors_event_t event;  
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    Serial.println("Error reading temperature!");

    u8x8.drawString(0,0,"Error reading temperature!");
    u8x8.refreshDisplay();    // only required for SSD1606/7  

  }
  ...
  ...
}

I'm not familiar with the U8x8lib library so I'm not sure what you can pass as parameters to drawString (e.g. an integer) or if there is a drawInteger or ...

Yeah, I’ve fought with posit g code before that was too long, so I just went right to attachments. Will go ahead and read the forum guidelines real quick though

what works? orwhic one is the code you were combining i was gonna do it for you but they all look like example code

i dont have this hardware but try this ... \

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Arduino.h>
#include <U8x8lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif


U8X8_SSD1306_128X64_NONAME_4W_SW_SPI u8x8(13, 11, 10, 9, 8);
#define DHTPIN            2         // Pin which is connected to the DHT sensor.
#define DHTTYPE           DHT22     // DHT 22 (AM2302)
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
uint8_t DHT11Temp, DHT11Hum;
sensor_t sensor;
sensors_event_t event;

void setup(void)
{

  u8x8.begin();
  u8x8.setPowerSave(0);
  Serial.begin(9600);
  dht.begin();

  dht.temperature().getSensor(&sensor);
  dht.humidity().getSensor(&sensor);
  delayMS = sensor.min_delay / 1000;
}

void loop()
{
  u8x8.setFont(u8x8_font_chroma48medium8_r);
  u8x8.drawString(0, 0, "Hello World!");
  delay(2000);
  RunDHT();
  SetDisplay(0, 0, "Hi" );
}

void DebugMe() {
  RefreshDHT();
  Serial.println("------------------------------------");
  Serial.println("Temperature");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" *C");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" *C");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" *C");
  Serial.println("------------------------------------");
  Serial.println("------------------------------------");
  Serial.println("Humidity");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println("%");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println("%");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println("%");
  Serial.println("------------------------------------");
}
void RefreshDHT() {
  dht.temperature().getSensor(&sensor);
  dht.humidity().getSensor(&sensor);
  dht.temperature().getEvent(&event);
  dht.humidity().getEvent(&event);
  delayMS = sensor.min_delay / 1000;
}

void SetDisplay(byte x, byte y, String Msg ) {
  int MessageLen = Msg.length() + 1;
  char Message[MessageLen];
  Msg.toCharArray(Message, MessageLen);
  u8x8.drawString(x, y, Message);
}

void ShowDHT() {
  if (DHT11Temp != 0) {
    CreateDHTMessageT();
    CreateDHTMessageH();
  }
}
void RunDHT() {
  RefreshDHT();
  if (!isnan(event.relative_humidity)) {
    CreateDHTMessageT();
    CreateDHTMessageH();
  } else {
    for (int j = 0; j <= 7; j++)     // ByteRowLoop
    {
      delay(1000);
      ReadDHT();
      if (DHT11Temp != 0) {
        j = 10;
      }
    }
  }
}
void ReadDHT() {
  // start working...
  Serial.println("=================================");
  Serial.println("Sample DHT11...");

  if (isnan(event.temperature)) {
    //Serial.println("Error reading temperature!");
    return;
  }
  DHT11Temp = event.temperature * 1.8 + 32;
  DHT11Hum = event.relative_humidity;
  //Serial.print("Sample OK: ");
  //Serial.print((int)temperature); Serial.print(" *C, ");
  //Serial.print((int)humidity); Serial.println(" H");
}
void CreateDHTMessageT()
{
  String Msg;
  String stringDHT11T =  String(DHT11Temp, DEC);
  Msg =  String("     Temp: " + stringDHT11T + "`F     ");
  SetDisplay(0, 0, Msg );

}
void CreateDHTMessageH()
{
  String Msg;
  String stringDHT11H =  String(DHT11Hum, DEC);
  Msg =  String("   Humidity: " + stringDHT11H + "%    ");
    SetDisplay(0, 0, Msg );

}
void ErrorMsg(int error)
{
  String Msg;
  String stringError =  String(error, DEC);
  if (error == 1) {
    Msg = String("    DHT 11 Error     ");
  }
    SetDisplay(0, 0, Msg );


}

The dht unified sensor and hello world work, humidity test is mine. Yours compiled and uploaded okay, but now it displays "Hello World! 0%", and the serial monitor displays nothing.

i went ahead and bought the hardware that this code uses i am just waiting on the post man to walk from Hong kong, i think i know why it doesnt work but im not exactly certain. if you want drop me a pm and i will help you 1v1... otherwise when the hardware gets here i will code it and get back to you :slight_smile:

// Add libraries
  #include "U8glib.h"
  #include <SPI.h>
  #include <Wire.h>
  #include <dht.h> 
  
// setup u8g object
  U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);	// I2C 
//
  dht DHT;
  #define DHT11_PIN 2
  struct
 {
   uint32_t total;
   uint32_t ok;
   uint32_t crc_error;
   uint32_t time_out;
   uint32_t connect;
   uint32_t ack_l;
   uint32_t ack_h;
   uint32_t unknown;
 } stat = { 0,0,0,0,0,0,0,0};  
  
//
  int humidity;
  double temperature;
  double temperatureF;
  int dewPoint1;
  String thisTemp = "";
  String thisHumidity = "";
  String thisDewPoint = "";
//

void draw(void) {
  u8g.setFont(u8g_font_profont12);
  u8g.drawStr(29,10, "DHT11 Sensor");
  u8g.setFont(u8g_font_profont12); 
  // display Centigrade
  thisTemp = String(temperature) + "\260C";
  const char* newTempC = (const char*) thisTemp.c_str();
  u8g.drawStr(70,25, newTempC);
  // display Fahrenheit
  temperatureF = 1.8 * temperature + 32;
  thisTemp = String(temperatureF) + "\260F";
  const char* newTempF = (const char*) thisTemp.c_str();
  u8g.drawStr(15,25, newTempF);  
  
 // now display Humidity
  u8g.setFont(u8g_font_profont29); 
  thisHumidity = String(humidity) + "%";
  const char* newHumidity = (const char*) thisHumidity.c_str();
  u8g.drawStr(15,50, newHumidity);  
  u8g.setFont(u8g_font_profont12);
  u8g.drawStr(65,38, "humidity");
  
 // now display the dew point
  thisDewPoint = String(dewPoint1) + "\260C";
  const char* newDewPoint = (const char*) thisDewPoint.c_str();
  u8g.drawStr(85,50, newDewPoint);  
  u8g.setFont(u8g_font_profont12);
  u8g.drawStr(65,50, "DP="); 
}

void setup(void) {
  Serial.begin(9600);
  Wire.begin();
  Serial.println("DHT11");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT_LIB_VERSION);
  Serial.println();
}

void loop(void) {
  uint32_t start = micros();
  int chk = DHT.read11(DHT11_PIN);
  uint32_t stop = micros();
  stat.total++;
  switch (chk)
  {
    case DHTLIB_OK:
      stat.ok++;
      Serial.print("OK,\t");
      break;
    case DHTLIB_ERROR_CHECKSUM:
      stat.crc_error++;
      Serial.print("Checksum error,\t");
      break;
    case DHTLIB_ERROR_TIMEOUT:
      stat.time_out++;
      Serial.print("Time out error,\t");
      break;
    case DHTLIB_ERROR_CONNECT:
      stat.connect++;
      Serial.print("Connect error,\t");
      break;
    case DHTLIB_ERROR_ACK_L:
      stat.ack_l++;
      Serial.print("Ack Low error,\t");
      break;
    case DHTLIB_ERROR_ACK_H:
      stat.ack_h++;
      Serial.print("Ack High error,\t");
      break;
    default:
      stat.unknown++;
      Serial.print("Unknown error,\t");
      break;
  }
  temperature = DHT.temperature;
  humidity = DHT.humidity;
  dewPoint(temperature,humidity);  
  // picture loop
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
  // delay needed between sensor reads
  delay(2000);
}

double dewPoint(double celsius, double humidity)
{
  double a = 17.271;
  double b = 237.7;
  double temp = (a * celsius) / (b + celsius) + log(humidity/100);
  dewPoint1 = (b * temp) / (a - temp);
}

Is there a question?

I've successfully gotten the temperature readout and hello world example programs to run separately, but how do I merge the two?

was the OPs Q

Hi, Merging two sketches can be a little difficult. There are some suggestions on making this easier HERE:

http://arduino-info.wikispaces.com/CombiningArduinoSketches

I now often "write for merge" by making almost every important feature a single, named, non-blocking function. Then merging is just a matter of including the function with cut and paste or #include, and calling it inside loop().

If you do that, you can at least merge your own code.