Hello, first at all sorry for my bad english.
I built a weather station with the arduino. It has two DTH (IN/OUT) sensors, pressure sensor, RTC clock, SD card data logging and photoresistor. Everything works perfectly. But there is one problem. I have a button which I want to turn on the backlight of the display. For some reason code found here: http://blog.rastating.com/toggling-the-backlight-of-hd44780-lcds-with-an-arduino-uno/ does not work in my program (display backlight and display itself doesnt turn on when button pressed. But thic code works properly when loaded separately. What could be the reason? The following program code is my code for weather station, probably has many flaws but it works, at the moment I'm interested in the inclusion of the backlight button. Im sure that hardware connections are correct. Thanks for any help.
/Libs
#include <LiquidCrystal.h> //LCD
#include <DHT.h> //DHT
#include <Wire.h> //1-Wire
#include <Adafruit_Sensor.h> //Sensors
#include <Adafruit_BMP085_U.h> //BMP180
#include <RTClib.h> //RTC
#include <SPI.h> //SPI
#include <SD.h> //SD card
#define DHT1PIN 8 //DHT IN pin
#define DHT2PIN 9 //DHT OUT pin
#define DHT1TYPE DHT11 //DHT IN type
#define DHT2TYPE DHT22 //DHT OUT type
#define LCD_LIGHT_PIN A2 //This is backlight pin
const int buttonPin = A1; //Button pin
int buttonState = 0;
DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);
//Icons
byte t[8] =
{
0b00100,
0b01010,
0b01010,
0b01110,
0b01110,
0b11111,
0b11111,
0b01110
};
byte w[8] =
{
0b00100,
0b00100,
0b01010,
0b01010,
0b10001,
0b10001,
0b10001,
0b01110
};
byte sto[8] = {
0b00110,
0b01001,
0b01001,
0b00110,
0b00000,
0b00000,
0b00000,
0b00000
};
byte mc[8] = {
0b00000,
0b00111,
0b01001,
0b10001,
0b10001,
0b10001,
0b10001,
0b11111
};
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);
RTC_DS1307 rtc;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Piny wy?wietlacza
const int chipSelect = 53; //Pin CS karty SD
File logfile;
void setup() {
lcd.begin(20, 4);
//Turn off display
lcd.noDisplay();
// Set the button pin as an input.
pinMode(buttonPin, INPUT);
// Set the LCD display backlight pin as an output.
pinMode(LCD_LIGHT_PIN, OUTPUT);
// Turn off the LCD backlight.
digitalWrite(LCD_LIGHT_PIN, LOW);
dht1.begin();
dht2.begin();
bmp.begin();
//rtc.adjust(DateTime(__DATE__, __TIME__));
Serial.begin(9600);
lcd.createChar(0, t);
lcd.createChar(3, sto);
lcd.createChar(5, w);
lcd.createChar(7, mc);
// initialize the SD card
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
error("Card failed, or not present");
}
Serial.println("card initialized.");
lcd.setCursor(19, 0);
lcd.write(byte(7));
// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
}
//Wysy?anie danych do portu szeregowego
void serialread(){
delay(2000);
sensors_event_t event;
bmp.getEvent(&event);
Serial.print("Cisnienie: ");
Serial.print(event.pressure);
Serial.print(" hPa\n");
float temperature;
bmp.getTemperature(&temperature);
Serial.print("Temperatura: ");
Serial.print(temperature);
Serial.print("C\n");
float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
Serial.print("Wysokosc: ");
Serial.print(bmp.pressureToAltitude(seaLevelPressure, event.pressure));
Serial.print(" m\n");
float h1 = dht1.readHumidity();
Serial.print("Wilgotnosc IN: ");
Serial.print(h1);
Serial.print("%\n");
float t1 = dht1.readTemperature();
Serial.print("Temperatura IN: ");
Serial.print(t1);
Serial.print("C\n");
float h2 = dht2.readHumidity();
Serial.print("Wilgotnosc OUT: ");
Serial.print(h2);
Serial.print("%\n");
float t2 = dht2.readTemperature();
Serial.print("Temperatura OUT: ");
Serial.print(t2);
Serial.print("C\n");
Serial.print("Natezenie swiatla");
Serial.print(analogRead(A0));
Serial.println();
}
void error(char *str)
{
Serial.print("error: ");
while(1);
}
//Wilgotno?? i temperatura IN
void htIN(){
delay(2000);
int h1 = dht1.readHumidity();
float t1 = dht1.readTemperature();
float temperature;
bmp.getTemperature(&temperature);
lcd.setCursor(0, 1);
lcd.print("I: ");
lcd.write(byte(5));
lcd.print(h1);
lcd.print("% ");
lcd.write(byte(0));
lcd.print((t1+temperature)/2);
lcd.write(byte(3));
lcd.print("C");
}
//Wilgotno?? i temperatura OUT
void htOUT(){
delay(2000);
int h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();
lcd.setCursor(0, 2);
lcd.print("O: ");
lcd.write(byte(5));
lcd.print(h2);
lcd.print("% ");
lcd.write(byte(0));
lcd.print(t2);
lcd.write(byte(3));
lcd.print("C");
}
//Ci?nienie i temperatura IN
void ptIN(){
sensors_event_t event;
bmp.getEvent(&event);
//float temperature; //Nie wyswietlamy temperatury
lcd.setCursor(0, 3);
lcd.print("O: ");
lcd.print(event.pressure);
lcd.print("hPa ");
//bmp.getTemperature(&temperature); //Nie wyswietlamy temperatury
//lcd.print(temperature); //Nie wyswietlamy temperatury
//lcd.print("C"); //Nie wyswietlamy temperatury
}
//Czas i data
void cl(){
DateTime now = rtc.now();
lcd.setCursor(2, 0);
if(now.hour() < 10) (lcd.print(0));
lcd.print(now.hour(), DEC);
lcd.print(':');
if(now.minute() < 10) (lcd.print(0));
lcd.print(now.minute(), DEC);
//lcd.print(':');
//lcd.print(now.second(), DEC);
lcd.print(" ");
if(now.day() < 10) (lcd.print(0));
lcd.print(now.day(), DEC);
lcd.print('/');
if(now.month() < 10) (lcd.print(0));
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.year(), DEC);
}
void light(){
int lightLevel = analogRead(A0);
lcd.setCursor(14, 3);
lcd.print(lightLevel);
lcd.print("lux");
}
//Rejestrowanie na karcie SD (hh:mm:ss, dd/mm/rrrr, w1, t1, w2, t2, light, pressure, t3)
void sd(){
delay(60000);
DateTime now = rtc.now();
if(now.hour() < 10) (logfile.print(0));
logfile.print(now.hour(), DEC);
logfile.print(':');
if(now.minute() < 10) (logfile.print(0));
logfile.print(now.minute(), DEC);
logfile.print(':');
if(now.second() < 10) (logfile.print(0));
logfile.print(now.second(), DEC);
logfile.print(", ");
if(now.day() < 10) (logfile.print(0));
logfile.print(now.day(), DEC);
logfile.print('/');
if(now.month() < 10) (logfile.print(0));
logfile.print(now.month(), DEC);
logfile.print('/');
logfile.print(now.year(), DEC);
logfile.print(", ");
sensors_event_t event;
bmp.getEvent(&event);
logfile.print(dht1.readHumidity());
logfile.print(", ");
logfile.print(dht1.readTemperature());
logfile.print(", ");
logfile.print(dht2.readHumidity());
logfile.print(", ");
logfile.print(dht2.readTemperature());
logfile.print(", ");
logfile.print(analogRead(A0));
logfile.print(", ");
logfile.print(event.pressure);
logfile.print(", ");
float temperature;
bmp.getTemperature(&temperature);
logfile.print(temperature);
logfile.println();
logfile.flush();
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
// Turn the backlight on.
digitalWrite(LCD_LIGHT_PIN, HIGH);
// Display the text on the LCD.
lcd.display();
// Wait for 10 seconds and then turn off the display and backlight.
delay(10000);
lcd.noDisplay();
digitalWrite(LCD_LIGHT_PIN, LOW);
}
htIN();
htOUT();
ptIN();
cl();
light();
serialread();
sd();
}