Hi guys,
first off, apologies if this is the wrong place to post such question, new to the platform so forgive me.
As you may have seen, recently there was a popular project on the hub which i thought looked good so i'd replicate myself as it has some real world applications i could use in my day job.
it is an air quality monitor which just measures basic humidity temp and ppm etc. and displays it which i thought was pretty cool and useful. however i was reading in the comments that the code posted didn't actually work.
now this is where i get it right, for a second, then make it worse
i ammend the code for it to work with the display i have, and then to actually fix the original code which people claim wasn't working.
done!
then i plug in my arduino nano, hook up the equipment and it all works after a couple tweaks.
brilliant, but then i switch the board over to my arduino uno r3, as this is the board i carry with me during work, and then this is where the code for some reason doesn't work, by which i mean it gets to the part where it shows "air&Gas quality monitor" which is only supposed to hang for 2 seconds and then move to the loop, but it just hangs!
i know its going to be a real small thing i've missed but its been bugging me for days now,
any help or advise is really appreciated and thanks again!
UPDATE: So it must be something to do with the boards as I got home this eve, plugged it into my Nano (yes I re-uploaded the code to ensure it is as you see it). And straight away it started to work again. Yes I was using also a standalone PSU, which I did try whilst using my Uno to no success. So my only conclusion is that there must be something that ties it to the Nano, or there must be something I'm missing when I want to put this on the Uno.
Thank you for your replies before and most of you saying it must be the PSU, thought we had it solved, but it can't be that after all
code:
#include <DHT.h>
#include <DHT_U.h>
#include <Adafruit_SH110X.h>
#include <splash.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeMonoOblique9pt7b.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SH1106G display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define sensor A2 //MQ135 A2
#define DHTPIN 2 // Digital pin 2
#define DHTTYPE DHT11 // DHT 11
int gasLevel = 0; //int variable for gas level
String quality ="";
DHT dht(DHTPIN, DHTTYPE);
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000
};
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
display.setTextSize(1);
display.setFont();
display.setCursor(0, 43);
display.println("Temp :");
display.setCursor(80, 43);
display.println(t);
display.setCursor(114, 43);
display.println("C");
display.setCursor(0, 56);
display.println("RH :");
display.setCursor(80, 56);
display.println(h);
display.setCursor(114, 56);
display.println("%");
}
void air_sensor()
{
gasLevel = analogRead(sensor);
if(gasLevel<181){
quality = " Excellent";
}
else if (gasLevel >181 && gasLevel<225){
quality = " Not great";
}
else if (gasLevel >225 && gasLevel<300){
quality = "Poor";
}
else if (gasLevel >300 && gasLevel<350){
quality = "Na Fam";
}
else{
quality = " lol ur dead";
}
display.setTextSize(1);
display.setCursor(1,5);
display.setFont();
display.println("Air Quality:");
display.setTextSize(1);
display.setCursor(-5,23);
display.setFont(&FreeMonoOblique9pt7b);
display.println(quality);
}
void setup() {
Serial.begin(9600);
pinMode(sensor,INPUT);
dht.begin();
delay(200);
display.begin();
display.setTextSize(2);
display.setTextColor(SH110X_WHITE);
display.display();
delay(1000);
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 0);
display.println("Air&Gas");
display.setTextSize(1);
display.setCursor(0,20);
display.println("Quality monitor");
display.setTextSize(1);
display.setCursor(0, 40);
display.println("By Viper");
display.display();
delay(1000);
display.clearDisplay();
}
void loop() {
display.clearDisplay();
air_sensor();
sendSensor();
display.display();
}