Sketch Writing Problem

Hello again,

Okay, so now I am having a problem writing a sketch. I used a BMP085 pressure sensor, DHT-11 humidity sensor, Arduino Uno, and Sainsmart TFT screen. Everything is showing up on the screen working fine, but I can't get the BMP085 to read over and over again. What is happening is right when I plug in the Uno, it reads the altitude, pressure, and temperature, but that's it. It reads once, and doesn't keep reading. Here's my sketch:

#include <TFT.h> // Arduino LCD library
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>


// Pins SCLK and MOSI are fixed in hardware, and pin 10 (or 53)
// must be an output
//#define sclk 13 // for MEGAs use pin 52
//#define mosi 11 // for MEGAs use pin 51
#define cs 10 // for MEGAs you probably want this to be pin 53
#define dc 9
#define rst 8 // you can also connect this to the Arduino reset

// pin definition for the Leonardo
// #define cs 7
// #define dc 0
// #define rst 1

// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);

// char array to print to the screen
char tempPrintout[6];
char humPrintout[6];
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);
Adafruit_BMP085_Unified a = Adafruit_BMP085_Unified(10085);


// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// Fahrenheit conversion added by Steve Spence, http://arduinotronics.blogspot.com

#include <DHT.h> //https://learn.adafruit.com/dht/downloads



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

// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Connect pin + (middle) of the sensor to +5V
// Connect pin S (on the right) of the sensor to whatever your DHTPIN is
// Connect pin - (on the left) of the sensor to GROUND
// Connect 10k resistor between S and +

int cycleTime = 2000;

DHT dht(DHTPIN, DHTTYPE);

float h;
float t;
char altPrintout[4];

void setup() {

//Put this line at the beginning of every sketch that uses the GLCD:
TFTscreen.begin();

// clear the screen with a black background
TFTscreen.background(0, 0, 0);

// write the static text to the screen
// set the font color to white
TFTscreen.stroke(255,255,255);
// set the font size
TFTscreen.setTextSize(2);
// write the text to the top left corner of the screen
TFTscreen.text("Temp (F)",0,0);
// write the text to the top left corner of the screen
TFTscreen.text("Humidity (%)",0,40);
// ste the font size very large for the loop
TFTscreen.setTextSize(2);

dht.begin();
bmp.begin();
}

void loop() {

// Read the value of the temp/humidity sensor on D2

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
h = dht.readHumidity();
t = dht.readTemperature();
t = (t*1.8)+32; //C to F conversion

String tempVal = doubleToString(t, 2);
String humVal = doubleToString(h, 0);
String altVal = String(analogRead(A4));
// String sensorVal = String(1.234);

// convert the reading to a char array
tempVal.toCharArray(tempPrintout, 6);
humVal.toCharArray(humPrintout, 6);
altVal.toCharArray(altPrintout, 4);

 /* Get a new sensor event */ 
  sensors_event_t event;
  bmp.getEvent(&event);
 

  /* Display the results (barometric pressure is measure in hPa) */
  if (event.pressure)
  {
    /* Display atmospheric pressue in hPa */
    TFTscreen.print(event.pressure);
    TFTscreen.println(" hPa");
    
    /* Calculating altitude with reasonable accuracy requires pressure    *
     * sea level pressure for your position at the moment the data is     *
     * converted, as well as the ambient temperature in degress           *
     * celcius.  If you don't have these values, a 'generic' value of     *
     * 1013.25 hPa can be used (defined as SENSORS_PRESSURE_SEALEVELHPA   *
     * in sensors.h), but this isn't ideal and will give variable         *
     * results from one day to the next.                                  *
     *                                                                    *
     * You can usually find the current SLP value by looking at weather   *
     * websites or from environmental information centers near any major  *
     * airport.                                                           *
     *                                                                    *
     * For example, for Paris, France you can check the current mean      *
     * pressure and sea level at: http://bit.ly/16Au8ol                   */
     
    /* First we get the current temperature from the BMP085 */
    float temperature;
    bmp.getTemperature(&temperature);
    TFTscreen.print(temperature);
    TFTscreen.println(" C");

    /* Then convert the atmospheric pressure, SLP and temp to altitude    */
    /* Update this next line with the current SLP for better results      */
    float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
    TFTscreen.print(bmp.pressureToAltitude(seaLevelPressure,
                                        event.pressure,
                                        temperature)); 
    TFTscreen.println(" m");
    TFTscreen.println("");
  }
  else
  {
    TFTscreen.println("Sensor error");
  }
  delay(1000);
   // set the font color
TFTscreen.stroke(255,255,255);
// print the sensor value
TFTscreen.text(altPrintout, 0, 25);
TFTscreen.text(altPrintout, 0, 85);
// wait for a moment
delay(cycleTime);
// erase the text you just wrote
TFTscreen.stroke(0,0,0);
TFTscreen.text(altPrintout, 0, 25);
TFTscreen.text(altPrintout, 0, 85);
};
{ 
// set the font color
TFTscreen.stroke(255,255,255);
// print the sensor value
TFTscreen.text(tempPrintout, 0, 25);
TFTscreen.text(humPrintout, 0, 85);
// wait for a moment
delay(cycleTime);
// erase the text you just wrote
TFTscreen.stroke(0,0,0);
TFTscreen.text(tempPrintout, 0, 25);
TFTscreen.text(humPrintout, 0, 85);
}

//Rounds down (via intermediary integer conversion truncation)
String doubleToString(double input,int decimalPlaces){
if(decimalPlaces!=0){
String string = String((int)(input*pow(10,decimalPlaces)));
if(abs(input)<1){
if(input>0)
string = "0"+string;
else if(input<0)
string = string.substring(0,1)+"0"+string.substring(1);
}
return string.substring(0,string.length()-decimalPlaces)+"."+string.substring(string.length()-decimalPlaces);
}
else {
return String((int)input);
}
}

Any help would be great.

Thanks
Connor

sketch_may13a.ino (5.83 KB)

You probably should get rid of this line:

Adafruit_BMP085_Unified a = Adafruit_BMP085_Unified(10085);

This bit of code looks strange:

  TFTscreen.text(altPrintout, 0, 85);
};
{ 
  // set the font color
  TFTscreen.stroke(255,255,255);

I'm surprised it compiles for you.

Yah, that helped, but it is not repeatedly reading the sensor. It only reads it once, and that is when I turn it on. Any help?

johnwasser:
You probably should get rid of this line

It would take me too long to figure out your code. May I suggest that you rearrange it so you separate the reading of sensors from the displaying of data.

If it was my project I would start with reading the sensors (each in its own sketch) and printing the data to the Serial Monitor. When I am confident that I know how to get each to work I would combine them. Then my code would probably look like this

void loop() {
  readDHT();
  readBMP();
  displayResults();
}

void readDHT() {
  // read DHT sensor and save the values to global variables
}

etc etc

Then if there is a problem it will all be in a small piece of code.

...R

Hi, how do you know it only reads the sensor once?
Have you got a counter on the screen that increments each time it reads the sensor.
I agree with Robin2, modularise your sketch so that each activity is in its own part of the program.

Tom.... :slight_smile:

TomGeorge:
Hi, how do you know it only reads the sensor once?

I know the sensor works once because when I change the temperature, say breathing on it, it just stays the same.
But I will give it a try.

connordrone:
Yah, that helped, but it is not repeatedly reading the sensor. It only reads it once, and that is when I turn it on. Any help?[quote

About that second bit of code johnwasser pointed out. It should probably be:

  TFTscreen.text(altPrintout, 0, 85);
              // <-- removed both }; and {
  // set the font color
  TFTscreen.stroke(255,255,255);
  // print the sensor value
  TFTscreen.text(tempPrintout, 0, 25);
  TFTscreen.text(humPrintout, 0, 85);
  // wait for a moment
  delay(cycleTime);
  // erase the text you just wrote
  TFTscreen.stroke(0,0,0);
  TFTscreen.text(tempPrintout, 0, 25);
  TFTscreen.text(humPrintout, 0, 85);
}   // end of loop()

It would help you a LOT if you autoformat your code. When you hit a Ctrl T, it will tell you if you have too many or too few parentheses or curly braces. It will also show you how your code is structured, by indenting it.