Multi-Tasking with One Arduino

I've been messing around with Arduino lately but running into problems with understanding how to extrapolate information to the point where I can combine two codes.

My goal is to have one Arduino board where I could hook up two screens, (uctronics .96" OLED module), where one would display the CO2 concentration in ppm, and the other displays the Humidity and Temperature.

I've came across two codes on google that I'd like to intertwine together so I can use one Arduino to run two separate displays that'll show their own readings.

(This code is for the CO2 ppm readings)

#include <Adafruit_GFX.h>
#include <Adafruit_MonoOLED.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <gfxfont.h>

/*

/*

  • Atmospheric CO2 Level..............400ppm
  • Average indoor co2.............350-450ppm
  • Maxiumum acceptable co2...........1000ppm
  • Dangerous co2 levels.............>2000ppm
    */

//---------------------------------------------------------------------------------------------------------------
// LIBRARIES
//---------------------------------------------------------------------------------------------------------------
#include <Wire.h> //I2C for OLED
#include <Adafruit_GFX.h> //grafix library for OLED
#include <Adafruit_SSD1306.h> //OLED Driver

//---------------------------------------------------------------------------------------------------------------
// DEFINES
//---------------------------------------------------------------------------------------------------------------
#define anInput A0 //analog feed from MQ135
#define digTrigger 2 //digital feed from MQ135
#define co2Zero 55 //calibrated CO2 0 level
#define led 9 //led on pin 9
#define OLED_RESET 4 //OLED reset on lin 4

//---------------------------------------------------------------------------------------------------------------
// LIBRARY CALL
//---------------------------------------------------------------------------------------------------------------
Adafruit_SSD1306 display(OLED_RESET); //create instance of OLED called display

//---------------------------------------------------------------------------------------------------------------
// SETUP
//---------------------------------------------------------------------------------------------------------------
void setup() {

pinMode(anInput,INPUT); //MQ135 analog feed set for input
pinMode(digTrigger,INPUT); //MQ135 digital feed set for input
pinMode(led,OUTPUT); //led set for output
Serial.begin(9600); //serial comms for debuging
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //begin display @ hex addy 0x3C
display.display(); //show buffer
display.clearDisplay(); //clear buffer

}
//---------------------------------------------------------------------------------------------------------------
// MAIN LOOP
//---------------------------------------------------------------------------------------------------------------
void loop() {

int co2now[10]; //int array for co2 readings
int co2raw = 0; //int for raw value of co2
int co2comp = 0; //int for compensated co2
int co2ppm = 0; //int for calculated ppm
int zzz = 0; //int for averaging
int grafX = 0; //int for x value of graph

display.clearDisplay(); //clear display @ beginning of each loop

for (int x = 0;x<10;x++){ //samplpe co2 10x over 2 seconds
co2now[x]=analogRead(A0);
delay(200);
}

for (int x = 0;x<10;x++){ //add samples together
zzz=zzz + co2now[x];

}
co2raw = zzz/10; //divide samples by 10
co2comp = co2raw - co2Zero; //get compensated value
co2ppm = map(co2comp,0,1023,400,5000); //map value for atmospheric levels

display.setTextSize(2); //set text size
display.setTextColor(WHITE); //set text color
display.setCursor(0,0); //set cursor
display.println("CO2 Level"); //print title
display.println(" "); //skip a line
display.print(co2ppm); //print co2 ppm
display.print(" PPM"); //print units
grafX = map(co2ppm,0,1000,0,127); //map value to screen width
display.fillRect(0, 52, grafX, 10, WHITE); //print graph 400min 1000max
display.display(); //show the buffer
if(co2ppm>999){ //if co2 ppm > 1000
digitalWrite(led,HIGH); //turn on led
}
else{ //if not
digitalWrite(led,LOW); //turn off led
}
}

(This code is for the Humidity and Temperature)
#include <Wire.h>
// two libraries inserted into the 'libraries' folder
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// dht library
#include "DHT.h"

#define DHTPIN 2 // what digital pin we're connected to
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define DHTTYPE DHT22

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

DHT dht(DHTPIN, DHTTYPE);

void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
display.display(); // show splashscreen
delay(2000);
display.clearDisplay(); // clears the screen and buffer

dht.begin();
delay(2000);
}

void loop() {
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.
if (isnan(h) || isnan(t) || isnan(f)) {
delay(2000);
} else{
// routine for converting temp/hum floats to char arrays
char temp_buff[5]; char hum_buff[5];
char temp_disp_buff[11] = "Tmp:";
char hum_disp_buff[11] = "Hum:";

// appending temp/hum to buffers
dtostrf(t,2,1,temp_buff);
strcat(temp_disp_buff,temp_buff);
dtostrf(h,2,1,hum_buff);
strcat(hum_disp_buff,hum_buff);

// routine for displaying text for temp/hum readout
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println(temp_disp_buff);
display.println(hum_disp_buff);
display.display();
delay(2000);
}
}

If anyone could direct me towards some newbie literature that'd show me the ropes I'd appreciate that! or has the ability to rewrite this headache, thank you

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

OPs pic.

Thanks.. Tom... :slight_smile:

Hi,
if you google
2 oled display arduino

and this YouTube by one of our forum members.

Tom... :slight_smile:

TomGeorge:
Hi,
if you google
2 oled display arduino

and this YouTube by one of our forum members.
https://www.youtube.com/watch?v=yef23sJjiU0

Tom... :slight_smile:

TomGeorge:
Hi,
if you google
2 oled display arduino

and this YouTube by one of our forum members.
https://www.youtube.com/watch?v=yef23sJjiU0

Tom... :slight_smile:

Thank you so much!! This was exactly what I was hoping for