Hej alla goa och glada.
Jag är ny inom arduino och all annan typ av programmering. Håller på med två olika projekt. Och har klart fastnat på båda två.
Men tänkte ta det mindre projektet först så ett blir avslutat.
Detta projektet har jag absolut inte skrivit själv utan hittat det på nätet.
Efter att jag skickat koden till min arduino så får jag fram både tempen fukten och set pointsen för de båda.
allt ser rätt ut enligt vad jag kan se. Men mina relän reagerar inte på värdena. Nån som har lust att hjälpa mig så jag kan komma vidare. Och jobba på mitt större projekt.
Tackar på förhand
/*
* Measure humidity and temperature with a DHT11 every 2 seconds,
* references two potentiometers for set-point values to control
* two SSR that will drive a refrigerator and a humidifier,
* and output the readings on the serial port, and a 16x2 LCD panel.
*
*
* Originally based on the DHT11 example code from http://www.dfrobot.com/wiki/index.php?title=DHT11_Temperature_and_Humidity_Sensor_(SKU:_DFR0067) (unknown license)
* and the LCD 16x2 tutorial/example code from http://www.arduino.cc/en/Tutorial/LiquidCrystal (Public Domain)
* Furthere based on Johan Herland <johan@herland.net> DHT 11, LCD code
*/
#include <EEPROM.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize LCD library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
// DHT11 signal pin is connected to this analog port
const unsigned int DHT11_PIN = 0;
// EEPROM size (1024 bytes on ATmega328)
const unsigned int EEPROM_SIZE = 1024;
// Current datagram from DHT11 (2B humidity, 2B temperature, 1B checksum)
byte dht11_dat[5];
int cur_minute; // Used in serial output averaging
int humd_sum; // Used in serial output data
int temp_sum; // Used in serial output data
int nsamples; // Used in serial output averaging
int humd_set = A1; // Potentiometer value read from analog pin A1 to set humidity
int temp_set = A2; // Potentiometer value read from analog pin A1 to set temperature
int h_tobe; // Mapped value from converting pot value to humidity scale
int t_tobe; // Mapped value from converting pot value to temperature scale
int HumSSR = 2; // Digital pin for Solid State Relay driving the humidifier
int TemSSR = 3; // Digital pin for Solid State Relay driving the refrigerator
int h_actual = dht11_dat[0]; // Actual humidity value as read by DHT 11 sensor
int t_actual = dht11_dat[2]; // Actual temperature vvalue as read by DHT 11 sensor
int t_hhyst = 2; // hyst = 4degF - you need to experiment with proper value
int h_hhyst = 2; // hyst = 4% - you need to experiment with proper value
unsigned int eeprom_addr; // EEPROM crap
void setup()
{
// Set DHT11 port as output port with initial value '1'
DDRC |= _BV(DHT11_PIN);
PORTC |= _BV(DHT11_PIN);
// Initialize serial port
Serial.begin(9600);
Serial.println("Ready");
// Set up 16x2 LCD panel
lcd.begin(16, 2);
// Dump EEPROM to serial port
lcd.setCursor(0, 0);
lcd.print("Dumping EEPROM ");
lcd.setCursor(0, 1);
lcd.print("to serial port..");
Serial.println("EEPROM dump start");
int i, j;
byte b = 0;
for (i = 0; i < EEPROM_SIZE; i += 0x10) {
Serial.print(i, HEX);
Serial.print(":");
for (j = 0; j < 0x10; j++) {
if (j % 2 == 0)
Serial.print(" ");
else
Serial.print("/");
b = EEPROM.read(i + j);
if (b == 0xff)
break;
Serial.print((float)b / 4.0);
}
Serial.println();
lcd.setCursor(15, 1);
if (i % 0x80 >= 0x40)
lcd.print(".");
else
lcd.print(" ");
if (b == 0xff)
break;
}
Serial.println("EEPROM dump end");
cur_minute = millis() / 60000;
humd_sum = 0;
temp_sum = 0;
nsamples = 0;
humd_set = 0;
temp_set = 0;
h_tobe = 0;
t_tobe = 0;
}
byte read_dht11_dat()
{
byte i = 0;
byte result = 0;
for (i = 0; i < 8; i++) {
// wait forever until analog input port 0 is '1'
// (NOTICE: PINC reads all the analog input ports
// and _BV(X) is the macro operation which pull up
// positon 'X' to '1' and the rest positions to '0'.
// It is equivalent to 1 << X.)
while(!(PINC & _BV(DHT11_PIN)));
// if analog input port 0 is still '1' after 30 us
// this position is 1.
delayMicroseconds(30);
if(PINC & _BV(DHT11_PIN))
result |= (1 << (7 - i));
// wait '1' finish
while((PINC & _BV(DHT11_PIN)));
}
return result;
}
boolean acquire_dht11_sample()
{
byte dht11_in;
byte i; // start condition
PORTC &= ~_BV(DHT11_PIN); // 1. pull-down i/o pin for 18ms
delay(18);
PORTC |= _BV(DHT11_PIN); // 2. pull-up i/o pin for 40ms
delayMicroseconds(1);
DDRC &= ~_BV(DHT11_PIN); // let analog port 0 be input port
delayMicroseconds(40);
dht11_in = PINC & _BV(DHT11_PIN); // read only the input port 0
if (dht11_in) {
// wait for DHT response signal: LOW
Serial.println("dht11 start condition 1 not met");
delay(1000);
return false;
}
delayMicroseconds(80);
dht11_in = PINC & _BV(DHT11_PIN);
if (!dht11_in) {
// wait for second response signal: HIGH
Serial.println("dht11 start condition 2 not met");
return false;
}
delayMicroseconds(80); // now ready for data reception
// receive 40 bits data. Details are described in datasheet
for (i = 0; i < 5; i++)
dht11_dat[i] = read_dht11_dat();
// set DHT11 port to output value '1', after data is received
DDRC |= _BV(DHT11_PIN);
PORTC |= _BV(DHT11_PIN);
// verify checksum
byte dht11_check_sum =
dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3];
if (dht11_dat[4] != dht11_check_sum)
Serial.println("DHT11 checksum error");
return true;
}
boolean update_avg(int minutes)
{
nsamples += 1;
humd_sum += dht11_dat[0];
temp_sum += dht11_dat[2];
if (minutes == cur_minute)
return false;
cur_minute = minutes;
nsamples = 0;
humd_sum = 0;
temp_sum = 0;
return true;
}
void serial_output(unsigned long runtime, boolean updated)
{
// report humidity and temperature on serial port
Serial.print("Current runtime/humidity/temperature: ");
Serial.print(runtime);
Serial.print(" ms, ");
Serial.print(dht11_dat[0], DEC);
Serial.print(".");
Serial.print(dht11_dat[1], DEC);
Serial.print(" %, ");
Serial.print(dht11_dat[2], DEC);
Serial.print(".");
Serial.print(dht11_dat[3], DEC);
Serial.println(" C");
if (updated) {
Serial.print("Average humidity/temperature last minute: ");
Serial.print("%, ");
Serial.println("C");
}
}
void lcd_output(int minutes, int seconds)
{
byte h_actual = dht11_dat[0];
byte t_actual = dht11_dat[2];
// print humidity on lcd
lcd.setCursor(0, 0);
lcd.print("Humd:");
if (h_actual < 10)
lcd.print(" ");
lcd.print(h_actual);
lcd.print("% SET:");
// print Set Point humidity
lcd.print(h_tobe);
lcd.print("%");
/*
// print runtime on lcd
if (minutes < 100) {
lcd.print(" ");
if (minutes < 10)
lcd.print(0);
}
lcd.print(minutes);
lcd.print(":");
if (seconds < 10)
lcd.print(0);
lcd.print(seconds);
*/
// print temperature on lcd
lcd.setCursor(0, 1);
lcd.print("Temp:");
if (t_actual < 10)
lcd.print(" ");
lcd.print(t_actual);
lcd.print("C SET:");
// print Set Point temperature
lcd.print(t_tobe);
lcd.print("C");
}
void log_to_eeprom()
{
eeprom_addr %= EEPROM_SIZE;
}
void loop()
{
// For some reason this needs to be here...
int h_actual = dht11_dat[0]; // Actual humidity value as read by DHT 11 sensor
int t_actual = dht11_dat[2]; // Actual temperature vvalue as read by DHT 11 sensor
humd_set = analogRead(A1); // Read potentiometer
temp_set = analogRead(A2); // Read potentiometer
h_tobe = map(humd_set, 0, 1023, 0, 100); // convert potentiometer value to 0-100% humidity
t_tobe = map(temp_set, 0, 1023, 0, 80); // convert potentiometer value to 0-80degF
if (!acquire_dht11_sample())
return;
unsigned long runtime = millis();
unsigned long minutes = runtime / 60000;
unsigned long seconds = (runtime / 1000) % 60;
boolean updated = update_avg(minutes);
serial_output(runtime, updated);
lcd_output(minutes, seconds);
// ***** humidifier on/off with hysteresis:
if (h_actual < (h_tobe - h_hhyst)) {
digitalWrite(HumSSR, HIGH); // humidifier ON
//humid_led = ON;
}
if (h_actual > (h_tobe + h_hhyst)) {
digitalWrite(HumSSR, LOW); // humidifier OFF
//humid_led = OFF;
}
// ***** fridge on/off with hysteresis:
if (t_actual > (t_tobe + t_hhyst)) {
digitalWrite(TemSSR, HIGH); // fridge ON
//fridge_led = ON;
}
if (t_actual < (t_tobe - t_hhyst)) {
digitalWrite(TemSSR, LOW); // fridge OFF
//fridge_led = OFF;
}
if (updated)
log_to_eeprom();
// wait 2 seconds until next reading
delay(2000);
}