I’m dabbling in Arduino and having trouble running two sketches on my Arduino UNO simultaneously. The first is about a program that reads temperature and relative humidity and the second is a program that reads barometric pressure. Both programs work independently, but when I try to combine the two, No help
this is the code for temperature and relative humidity:
#include <Adafruit_Sensor.h>
#include <LiquidCrystal.h>
#include <DHT.h>
#include <DHT_U.h>
//VARIABLES
//Asignación de los pines digitales
int SENSOR = 2; //Este es el pin #2
int RS = 4;
int E = 5;
int D4 = 6;
int D5 = 7;
int D6 = 8;
int D7 = 9;
//Variables de lectura de tempratura y humedad
int temp;
int hume;
//Creación de variables para el sensor y el display
DHT dht(SENSOR,DHT11); //Formato general: DHT <var_name>(PIN, MODELO DEL SENSOR)
LiquidCrystal lcd(RS, E, D4, D5, D6, D7); // Formato general: LiquidCrystal <var_name>(RS, RW, ENABLE, D0, D1, D2, D3, D4, D5, D6, D7)
//Variable para salida del abanico
int motor = 12;
void setup(){
//Iniciar el sensor de temp
dht.begin();
//Iniciar el LCD
lcd.begin(16,2); //(tipo 16 columnas, 2 líneas)
//Setea el Pin 12 como salida
pinMode(motor,OUTPUT);
}
void loop(){ //Este es el buclé principal del programa
//Lee valores físicos
hume = dht.readHumidity();
temp = dht.readTemperature();
//Limpia el LCD
lcd.clear(); //esta instrucción coloca el cursor en la pos 0,0 (columna, fila)
lcd.setCursor(0,0); //(no necesaria porque la linea de arriba ya lo hace)
//Escribe los valores leídos en el LCD
lcd.print("TEMPERATURA: ");
lcd.print(temp);
lcd.print("C");
lcd.setCursor(0,1); //esta instrucción coloca el cursor en la pos 0,1 (columna, fila)
lcd.print("HUMEDAD: ");
lcd.print(hume);
lcd.print("%");
//Activa el motor si la temperatura pasa el valor deseado
if(temp>=30){ // Compara si la temp ya es 30 grados
digitalWrite(motor,HIGH); //Si lo es manda un 1 lógico a teavés del pin digital #12
}
else{
digitalWrite(motor,LOW);// sino, manda un 0, lo que apagaría el motor
}
delay(2000);//valor en milisengundos para que repita el ciclo
}
and this is the barometric pressure code:
#include <Wire.h>
#include <Adafruit_BMP085.h>
// Connect VCC of the BMP085 sensor to 3.3V (NOT 5.0V!)
// Connect GND to Ground
// Connect SCL to i2c clock - on '168/'328 Arduino Uno/Duemilanove/etc thats Analog 5
// Connect SDA to i2c data - on '168/'328 Arduino Uno/Duemilanove/etc thats Analog 4
// EOC is not used, it signifies an end of conversion
// XCLR is a reset pin, also not used here
Adafruit_BMP085 bmp;
void setup() {
Serial.begin(9600);
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) {}
}
}
void loop() {
Serial.print("Temperature = ");
Serial.print(bmp.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bmp.readPressure());
Serial.println(" Pa");
// Calculate altitude assuming 'standard' barometric
// pressure of 1013.25 millibar = 101325 Pascal
Serial.print("Altitude = ");
Serial.print(bmp.readAltitude());
Serial.println(" meters");
Serial.print("Pressure at sealevel (calculated) = ");
Serial.print(bmp.readSealevelPressure());
Serial.println(" Pa");
// you can get a more precise measurement of altitude
// if you know the current sea level pressure which will
// vary with weather and such. If it is 1015 millibars
// that is equal to 101500 Pascals.
Serial.print("Real altitude = ");
Serial.print(bmp.readAltitude(101500));
Serial.println(" meters");
Serial.println();
delay(500);
}
1 Answer 1
You need to write one program using elements from each block of code. There is no way the Arduino will run both programs concurrently, it will only run one or the other. You can copy and paste them together but unless done correctly it will not work. This is why it is important to understand the code you are using.
dht.begin
,lcd.begin
, clear lcd, display start message on lcd, ifbmp.begin
fails then display error message ..... etc