Please help me understand how to combine sketches.

Hello, I am trying to combine sketches so I can read the data from a DHT22 sent via some XBee’s.

I am using a Mega, and XBee Pro s1. I have the XBee’s communicating and can get them to chat back and forth, (although the Arduino Serial Monitor does not like starting new lines, and I can’t figure out how to get it on a new line when receiving from the XCTU console)

Here is the code I use to get them chatting:

// We'll use SoftwareSerial to communicate with the XBee:
#include <SoftwareSerial.h>
// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
SoftwareSerial XBee(10, 11); // RX, TX

void setup()
  // Set up both ports at 9600 baud. This value is most important
  // for the XBee. Make sure the baud rate matches the config
  // setting of your XBee.

void loop()
  if (Serial.available())
  { // If data comes in from serial monitor, send it out to XBee
  if (XBee.available())
  { // If data comes in from XBee, send it out to serial monitor

And here is the code I use to get the DHT22 to spit out data on the serial monitor in the arduino IDE

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN 2     // what digital pin we're connected to

#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

void setup() {
  Serial.println("DHTxx test!");


void loop() {
  // Wait a few seconds between measurements.

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print("Humidity: ");
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(" *C ");
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(" *C ");
  Serial.println(" *F");

Can someone please help me to understand what I am doing wrong in trying to combine these? I have read numerous articles on how to do it and tried to do it step by step, but alas me brain is feeble, and my father smelt of elderberries.

Thank you.

Can someone please help me to understand what I am doing wrong in trying to combine these?

Sure. What you did wrong was that you failed to post your attempt to combine them.

If, in the one sketch, Serial.print() sends temperature data to the serial monitor app, and in the other sketch, XBee.print() sends data to the XBee, then it should be obvious how to send temperature data to the XBee.