Помощь с метеостанцией
- Войдите на сайт для отправки комментариев
Ср, 17/08/2016 - 21:14
Рботаю над метеостанцией. Возникли проблеы с датчиком DHT22. Это рабочий код, но если добаить в setup строчку Serial.begin(9600); то выдает ошибку DHT датчик 'Failed to read from DHT sensor'. Как это можно исправить?
[code]
#include <SD.h>
#include <RTClib.h>
#include <RTC_DS3231.h>
#include <UTFT.h>
#include <Wire.h>
#include <DHT.h>
#define DHTPIN 2 // what pin we're connected to
#include <SPI.h>
RTC_DS3231 RTC;
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
extern uint8_t BigFont[];
extern uint8_t SmallFont[];
extern uint8_t SevenSegNumFont[];
UTFT myGLCD(TFT32MEGA, 38, 39, 40, 41);
File myFile;
void setup()
{
pinMode(1,OUTPUT);
pinMode(3,OUTPUT);
// SD card
//
myGLCD.InitLCD();
dht.begin();
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
RTC.adjust(DateTime(__DATE__, __TIME__));
}
DateTime now = RTC.now();
DateTime compiled = DateTime(__DATE__, __TIME__);
if (now.unixtime() < compiled.unixtime()) {
RTC.adjust(DateTime(__DATE__, __TIME__));
}
RTC.enable32kHz(true);
//RTC.SQWEnable(true);
//RTC.BBSQWEnable(true);
//RTC.SQWFrequency( SQW_FREQ );
char datastr[100];
RTC.getControlRegisterData( datastr[0] );
//Serial.print( datastr );
//--------INT 0---------------
//EICRA = 0; //clear it
//EICRA |= (1 << ISC01);
//EICRA |= (1 << ISC00); //ISC0[1:0] = 0b11 rising edge INT0 creates interrupt
// EIMSK |= (1 << INT0); //enable INT0 interrupt
}
//EICRA = 0; //clear it
//EICRA |= (1 << ISC01);
//EICRA |= (1 << ISC00); //ISC0[1:0] = 0b11 rising edge INT0 creates interrupt
// EIMSK |= (1 << INT0); //enable INT0 interrupt
}
typedef void (*LCD_demo)();
LCD_demo const demos[] =
{
showTime,
showData,
showHum
};
#define dotimes(n, code) for (int i = 0; i < (n); ++i) code;
void loop()
{
myGLCD.clrScr();
digitalWrite(1,LOW);
digitalWrite(3,HIGH);
dotimes(sizeof(demos) / sizeof(demos[0]),
{
demos[i](); // запускаем очередную демонстрацию
{
demos[i](); // запускаем очередную демонстрацию
/* Даём насладиться её последними "кадрами", после чего очищаем экран */
delay(5000);
});
}
delay(5000);
});
}
void showTime()
{
DateTime now = RTC.now();
RTC.forceTempConv(true); //DS3231 does this every 64 seconds, we are simply testing the function here
float temp_float = RTC.getTempAsFloat();
int16_t temp_word = RTC.getTempAsWord();
int8_t temp_hbyte = temp_word >> 8;
int8_t temp_lbyte = temp_word &= 0x00FF;
int m = now.minute();
int hour = now.hour();
myGLCD.setColor(VGA_GREEN);
myGLCD.setFont(SmallFont);
myGLCD.print("TIME", 5, 20);
myGLCD.setFont(SevenSegNumFont);
if (hour<10) {myGLCD.printNumI(now.hour(), 70, 15); }
else if(hour >= 10){
myGLCD.printNumI(now.hour(), 40, 15); }
myGLCD.setFont(BigFont);
myGLCD.print(".", 103, 20);
myGLCD.print(".", 103, 35);
myGLCD.setFont(SevenSegNumFont);
if (m < 10) {myGLCD.print("0",116, 15); myGLCD.printNumI(now.minute(),151
, 15); }
else if (m>=10) {
myGLCD.printNumI(now.minute(),116,15);
}
RTC.forceTempConv(true); //DS3231 does this every 64 seconds, we are simply testing the function here
float temp_float = RTC.getTempAsFloat();
int16_t temp_word = RTC.getTempAsWord();
int8_t temp_hbyte = temp_word >> 8;
int8_t temp_lbyte = temp_word &= 0x00FF;
int m = now.minute();
int hour = now.hour();
myGLCD.setColor(VGA_GREEN);
myGLCD.setFont(SmallFont);
myGLCD.print("TIME", 5, 20);
myGLCD.setFont(SevenSegNumFont);
if (hour<10) {myGLCD.printNumI(now.hour(), 70, 15); }
else if(hour >= 10){
myGLCD.printNumI(now.hour(), 40, 15); }
myGLCD.setFont(BigFont);
myGLCD.print(".", 103, 20);
myGLCD.print(".", 103, 35);
myGLCD.setFont(SevenSegNumFont);
if (m < 10) {myGLCD.print("0",116, 15); myGLCD.printNumI(now.minute(),151
, 15); }
else if (m>=10) {
myGLCD.printNumI(now.minute(),116,15);
}
myGLCD.drawLine(5,70,180,70);
}
void showData()
{
DateTime now = RTC.now();
int mon = now.month();
int date = now.day();
myGLCD.setColor(VGA_BLUE);
myGLCD.setFont(SmallFont);
myGLCD.print("DATE", 5, 75);
myGLCD.setFont(SevenSegNumFont);
if (date < 10){ myGLCD.printNumI(0, 35, 75); myGLCD.printNumI(now.day(), 70, 75); }
else if (date >=10) {myGLCD.printNumI(now.day(), 40, 75); }
myGLCD.setFont(BigFont);
myGLCD.print(".", 103, 110);
myGLCD.setFont(SevenSegNumFont);
if ( mon < 10) {myGLCD.printNumI(0, 116, 75); myGLCD.printNumI(now.month(), 151, 75);}
else if (mon >=10) {myGLCD.printNumI(now.month(), 115, 75);}
myGLCD.setFont(BigFont);
myGLCD.printNumI(now.year(), 180, 110);
myGLCD.drawLine(5,135,180,135);
}
}
void showData()
{
DateTime now = RTC.now();
int mon = now.month();
int date = now.day();
myGLCD.setColor(VGA_BLUE);
myGLCD.setFont(SmallFont);
myGLCD.print("DATE", 5, 75);
myGLCD.setFont(SevenSegNumFont);
if (date < 10){ myGLCD.printNumI(0, 35, 75); myGLCD.printNumI(now.day(), 70, 75); }
else if (date >=10) {myGLCD.printNumI(now.day(), 40, 75); }
myGLCD.setFont(BigFont);
myGLCD.print(".", 103, 110);
myGLCD.setFont(SevenSegNumFont);
if ( mon < 10) {myGLCD.printNumI(0, 116, 75); myGLCD.printNumI(now.month(), 151, 75);}
else if (mon >=10) {myGLCD.printNumI(now.month(), 115, 75);}
myGLCD.setFont(BigFont);
myGLCD.printNumI(now.year(), 180, 110);
myGLCD.drawLine(5,135,180,135);
}
void showHum()
{
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float hum = dht.readHumidity();
// Read temperature as Celsius
float temp = dht.readTemperature();
// Read temperature as Fahrenheit
float f = dht.readTemperature(true);
myGLCD.setFont(BigFont);
{
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float hum = dht.readHumidity();
// Read temperature as Celsius
float temp = dht.readTemperature();
// Read temperature as Fahrenheit
float f = dht.readTemperature(true);
myGLCD.setFont(BigFont);
// Check if any reads failed and exit early (to try again).
if (isnan(hum) || isnan(temp) || isnan(f)) {
myGLCD.print("Failed to read from DHT sensor!",15,60);
return;
}
float hi = dht.computeHeatIndex(f, hum);
myGLCD.setFont(SmallFont);
myGLCD.setColor(VGA_TEAL);
myGLCD.print("HUM", 5, 145);
myGLCD.setFont(SevenSegNumFont);
myGLCD.printNumI(int(hum), 50, 140);
myGLCD.setFont(BigFont);
myGLCD.print(" %", 111, 170);
myGLCD.drawLine(5,195,180,195);
if (isnan(hum) || isnan(temp) || isnan(f)) {
myGLCD.print("Failed to read from DHT sensor!",15,60);
return;
}
float hi = dht.computeHeatIndex(f, hum);
myGLCD.setFont(SmallFont);
myGLCD.setColor(VGA_TEAL);
myGLCD.print("HUM", 5, 145);
myGLCD.setFont(SevenSegNumFont);
myGLCD.printNumI(int(hum), 50, 140);
myGLCD.setFont(BigFont);
myGLCD.print(" %", 111, 170);
myGLCD.drawLine(5,195,180,195);
myGLCD.setFont(SmallFont);
myGLCD.setColor(VGA_LIME);
myGLCD.print("TEMP", 5, 220);
myGLCD.setFont(SevenSegNumFont);
myGLCD.printNumI(int(temp),50, 210);
myGLCD.setFont(BigFont);
myGLCD.print(" *C", 111, 247);
myGLCD.drawLine(5,265,180,265);
myGLCD.setColor(VGA_LIME);
myGLCD.print("TEMP", 5, 220);
myGLCD.setFont(SevenSegNumFont);
myGLCD.printNumI(int(temp),50, 210);
myGLCD.setFont(BigFont);
myGLCD.print(" *C", 111, 247);
myGLCD.drawLine(5,265,180,265);
}
[/code]
Покажите сообщение, которое выдаётся после компиляции (там про память написано)
И, кстати, выкладывайте код нормально, как вот здесь описано, а то читать трудно, а обсуждлать без номеров строк вообще невозможно.
Вот Ваш код
Извините, я новичок. Вот сообщение после компиляции:
avrdude: verifying flash memory against C:\Users\5308~1\AppData\Local\Temp\build5a015498f9064eb697d5f4af46572c19.tmp/meteo3senscheck9.ino.hex:
avrdude: load data flash data from input file C:\Users\5308~1\AppData\Local\Temp\build5a015498f9064eb697d5f4af46572c19.tmp/meteo3senscheck9.ino.hex:
avrdude: input file C:\Users\5308~1\AppData\Local\Temp\build5a015498f9064eb697d5f4af46572c19.tmp/meteo3senscheck9.ino.hex contains 29396 bytes
avrdude: reading on-chip flash data:
avrdude: 29396 bytes of flash verified
Нет, там было ещё.
Давайте так, запустите не загрузку в плату, а только компиляцию (в IDE Она проверкой называется) и давайте сюда её сообщение.
И, кстати, что у Вас за Аруина? Мега?
Ардуино Мега. Сообщение:
Скетч использует 29 396 байт (11%) памяти устройства. Всего доступно 253 952 байт.
Глобальные переменные используют 1 813 байт (22%) динамической памяти, оставляя 6 379 байт для локальных переменных. Максимум: 8 192 байт.