Stuck in setup can't move to loop

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 :slight_smile:

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();
  }

Now this is just a real out-there suggestion:
You're doing your testing with the Uno connected to your computer, right?
Do you get the same result if you connect the Uno to a "dumb" power source, like a phone charger? I've had weird issues before to do with the different USB Serial ICs on Uno vs Nano, and this could be something to consider if nothing more obvious pops up?

[quote="viper6077, post:1, topic:1215781"]

  if (isnan(h) || isnan(t)) {
  Serial.println("Failed  to read from DHT sensor!");
    return;
  }

[/quote]

This should be in setup()

Also... those "air and gas" sensors use a lot of power to warm up (which usually takes a day) and work, needing an external power supply.

1 Like

Your code shows that it also prints "by Viper" then waits for 1 second not 2.
Are you sure you posted the right code?

1 Like

I see, so before I had it hooked up to an additional PSU when using the nano, do you think in fact that the code is fine but just needs the PSU and should run?

1 Like

Yep it's the right code, 1 or 2 seconds isn't really a critical factor here but the rest of it is accurate

Yeh I think we've cracked it! So you and another commenter have pointed out the obvious (to everyone besides me), that when I had it on the nano it was ALSO connected to an external PSU, and when I use my Uno, I'm also using it as power and so this could be the cause, thank you for commenting, I shall post an update to confirm results

So does it print out "by Viper" or stop right after "quality monitor"

To be fair, I was guessing at a slightly sillier idea, about the USB connection resetting the board, but the PSU issue sounds a lot more likely!

Follow @jim-p on the code. Find the "datasheet" for this sensor... it will give you some details on power consumption, find the normal voltage and "max" or "peak" current ... your PSU should be 110% of the max current/Amp.

So...
You clear the display.
Then you do some readings.
Then you display something (I suppose this sends the data to the display? Not familiar with this).
This is instantly followed by you clearing the display as loop() starts again.

My best guess of what you get to see is a clear display, as it never gets a chance to actually show the data you send to it.

Try adding a delay(500) at the end of loop() as crude way of not immediately clearing it.

It'll print out

Air and gas monitor
By viper

Then hang on that, won't clear display just sit

So it's working now?

Updated above

So this part of the code is unchanged from what I downloaded from project hub, as I have said I am new so please correct me, but my assumption was that this simply refreshes the display when the values are changing continuously, but no I've updated since you have replied and can confirm I do not see a blank display with this code

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.