Arduino Polska Forum

Pełna wersja: Sterownik do akwarium
Aktualnie przeglądasz uproszczoną wersję forum. Kliknij tutaj, by zobaczyć wersję z pełnym formatowaniem.
Stron: 1 2 3
Witam
Kod:
// --- najpierw to na początku programu : (tam gdzie definuijesz P1,P2,P3)
// ten twój kawalek kodu zamieniasz na to:  
DeviceAddress P[3];
P[0] = { 0x28, 0xFF, 0xB2, 0x4C, 0x65, 0x14, 0x1, 0x6F };
P[1] = { 0x28, 0xFF, 0xE, 0x97, 0x65, 0x14, 0x1, 0x94 };
P[2] = { 0x28, 0xFF, 0x6E, 0x35, 0x65, 0x14, 0x1, 0x2B };
// --- potem w miejscu gdzie masz definicję pinów dla wentylatorow dodajesz to:
int w[3]={went_1, went_2, went_3};
// --- a potem to zamiast tych 3 powtarzających się bloków dajesz to:
// jesli o czymś nie zapomnialem to powinno się skompilować bez błędu----
for(int u=0;u<3;u++)
 {
   float temp = sensors.getTempC(P[u]);
   lcd.setCursor (0, u+1);
   lcd.write(byte(0));
   lcd.print(" ");
   lcd.print(temp, 2);
   lcd.write(byte(223));
   if (sensors.getTempCByIndex(u) < (nastaw+histereza)) {
     histereza=0;
     digitalWrite(w[u], HIGH);
     int val = digitalRead(w[u]);
     if (val)
     lcd.setCursor(9, u+1);
     lcd.print("Ok ");
   }
   else {
     lcd.setCursor(9, u+1);
     lcd.print("Wla");
     histereza=-2;
     digitalWrite(w[u], LOW);
   }
 }
// ----koniec -----------------------------------------------  
Pozdrawiam :-)
Niestety nie kompiluje się, takie błędy wyskakują:
Cytat:sketch_feb11b:19: error: 'P' does not name a type

 P[0] = { 0x28, 0xFF, 0xB2, 0x4C, 0x65, 0x14, 0x1, 0x6F };

 ^

sketch_feb11b:20: error: 'P' does not name a type

 P[1] = { 0x28, 0xFF, 0xE, 0x97, 0x65, 0x14, 0x1, 0x94 };

 ^

sketch_feb11b:21: error: 'P' does not name a type

 P[2] = { 0x28, 0xFF, 0x6E, 0x35, 0x65, 0x14, 0x1, 0x2B };

 ^

exit status 1
'P' does not name a type


a tutaj jeszcze sprawdź proszę czy wszystko dobrze powklejałem.

Kod:
#include <DS1307RTC.h>
#include <Time.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DS1307_ADDRESS 0x68
#define LIGHT_ON 0
#define LIGHT_OFF 1
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

#define ONE_WIRE_BUS 12
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

int nastaw = 26; // nastaw temperatury zalaczenia przekaznikow wentylatorow
int swiatlo = 11; // pin przekaznika oswietlenia
int went_1 = 8; // pin przekaznika wentylatora 1
int went_2 = 9; // pin przekaznika wentylatora 2
int went_3 = 10; // pin przekaznika wentylatora 3
int w[3]={went_1, went_2, went_3};
volatile int histereza=0;

byte state = LOW;
const char *monthName[12] = {
 "Sty", "Lut", "Mar", "Kwi", "Maj", "Cze",
 "Lip", "Sie", "Wrz", "Paź", "Lis", "Gru"
};

byte temp[8] = //ikona temperatury
{
 B00100,
 B01010,
 B01010,
 B01110,
 B01110,
 B11111,
 B11111,
 B01110
};

DeviceAddress P[3];
P[0] = { 0x28, 0xFF, 0xB2, 0x4C, 0x65, 0x14, 0x1, 0x6F };
P[1] = { 0x28, 0xFF, 0xE, 0x97, 0x65, 0x14, 0x1, 0x94 };
P[2] = { 0x28, 0xFF, 0x6E, 0x35, 0x65, 0x14, 0x1, 0x2B };

void setup()
{
 Serial.begin(9600);
 sensors.begin();
 sensors.setResolution(P[0], 12);
 sensors.setResolution(P[1], 12);
 sensors.setResolution(P[2], 12);
 Wire.begin();
 digitalWrite(swiatlo, LIGHT_OFF);
 pinMode(swiatlo, OUTPUT);
 pinMode(went_1, OUTPUT);
 pinMode(went_2, OUTPUT);
 pinMode(went_3, OUTPUT);
 lcd.begin(20, 4);
 lcd.createChar(0, temp);
}


void loop() {
 czasidata();
 czasowy();
 termometry();
}
byte bcdToDec(byte val)  {
 return ( (val / 16 * 10) + (val % 16) );
}

void czasowy() {
 Wire.beginTransmission(DS1307_ADDRESS);
 byte zero = 0x00;
 Wire.write(zero);
 Wire.endTransmission();
 Wire.requestFrom(DS1307_ADDRESS, 7);
 int second = bcdToDec(Wire.read());
 int minute = bcdToDec(Wire.read());
 int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time

 if ((hour == 12 ) && (minute == 30))
   digitalWrite(swiatlo, LIGHT_ON);
 if ((hour == 12) && (minute == 31))
   digitalWrite(swiatlo, LIGHT_OFF);
}

void czasidata() {
 tmElements_t tm;

 if (RTC.read(tm)) {
 
   lcd.setCursor (0, 0);
   LCDprint2digits(tm.Hour);
   lcd.print(':');
   LCDprint2digits(tm.Minute);
   lcd.print(':');
   LCDprint2digits(tm.Second);
   lcd.setCursor (9, 0);
   lcd.print(tm.Day);
   lcd.print('/');
   lcd.print(monthName[tm.Month-1]);
   lcd.print('/');
   lcd.print(tmYearToCalendar(tm.Year));
   
   
 } else {
   if (RTC.chipPresent()) {
     lcd.println("DS1307 stoi. Uruchom Zegar");
     }
      else {
     lcd.println("DS1307 blad oczczytu!");
     lcd.println("sprawdz polaczenie");
   }
 }
}


void termometry() {
 
for(int u=0;u<3;u++)
{
  float temp = sensors.getTempC(P[u]);
  lcd.setCursor (0, u+1);
  lcd.write(byte(0));
  lcd.print(" ");
  lcd.print(temp, 2);
  lcd.write(byte(223));
  if (sensors.getTempCByIndex(u) < (nastaw+histereza)) {
    histereza=0;
    digitalWrite(w[u], HIGH);
    int val = digitalRead(w[u]);
    if (val)
    lcd.setCursor(9, u+1);
    lcd.print("Ok ");
  }
  else {
    lcd.setCursor(9, u+1);
    lcd.print("Wla");
    histereza=-2;
    digitalWrite(w[u], LOW);
  }
}
}

void LCDprint2digits(int number) {
 if (number >= 0 && number < 10) {
   lcd.write('0');
 }
 lcd.print(number);

}

I jeszcze jedno, w void setup zmieniłem z:
Kod:
sensors.begin();
sensors.setResolution(P1, 12);
sensors.setResolution(P2, 12);
sensors.setResolution(P3, 12);
na:
Kod:
 sensors.setResolution(P[0], 12);
 sensors.setResolution(P[1], 12);
 sensors.setResolution(P[2], 12);
można to zapisać w setupie jako poniżej?
Kod:
 sensors.begin();
 sensors.setResolution(P[u], 12);
Witam
Kurcze umieszczasz oderwane fragmenty kodu. Ciężko to potem posklejać. 
Co do błędów z P[3] to faktycznie. Zajrzałem do biblioteki Dallasa i trzeba ogarnąć temat tej tablicy nieco inaczej.
Otóż zmienne typo DeviceAdress to tablice ośmiobitowe i zobaczysz w kodzie jak to ogarnąć :-)
Wziąłem Twój cały, kompletny kod i poprawiłem błędy. Nawiasem mówiąc musiałem pobrać z netu bibliotekę Time.h i DS_1307RTC (bo miałem tylko DS1307). Pewnie to jest to samo ale już nie wnikam.
Świadomie też nie korzystam z Twojej LiquidCrystal_I2C (wolę swoją gdzie w konstruktorze są tylko 3 zmienne :-).
Poniżej poprawiony kod. Sprawdź, powinno wszystko działać.
Kod:
#include <DS1307RTC.h>
#include <Time.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DS1307_ADDRESS 0x68
#define LIGHT_ON 0
#define LIGHT_OFF 1
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//LiquidCrystal_I2C lcd(0x27, 16,4);

#define ONE_WIRE_BUS 12
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

int nastaw = 26; // nastaw temperatury zalaczenia przekaznikow wentylatorow
int swiatlo = 11; // pin przekaznika oswietlenia
int went_1 = 8; // pin przekaznika wentylatora 1
int went_2 = 9; // pin przekaznika wentylatora 2
int went_3 = 10; // pin przekaznika wentylatora 3
int w[3]={went_1, went_2, went_3};
volatile int histereza=0;

byte state = LOW;
const char *monthName[12] = {
"Sty", "Lut", "Mar", "Kwi", "Maj", "Cze",
"Lip", "Sie", "Wrz", "Paź", "Lis", "Gru"
};

byte temp[8] = //ikona temperatury
{
B00100,
B01010,
B01010,
B01110,
B01110,
B11111,
B11111,
B01110
};
//DeviceAddress P1 = { 0x28, 0xFF, 0xB2, 0x4C, 0x65, 0x14, 0x1, 0x6F };
//DeviceAddress P2 = { 0x28, 0xFF, 0xE, 0x97, 0x65, 0x14, 0x1, 0x94 };
//DeviceAddress P3 = { 0x28, 0xFF, 0x6E, 0x35, 0x65, 0x14, 0x1, 0x2B };
DeviceAddress P[3]={
{ 0x28, 0xFF, 0xB2, 0x4C, 0x65, 0x14, 0x1, 0x6F },
{ 0x28, 0xFF, 0xE, 0x97, 0x65, 0x14, 0x1, 0x94 },
{ 0x28, 0xFF, 0x6E, 0x35, 0x65, 0x14, 0x1, 0x2B }
};
//P[0]=P1;P[1]=P2;P[2]=P3;
//P[0] = { 0x28, 0xFF, 0xB2, 0x4C, 0x65, 0x14, 0x1, 0x6F };
//P[1] = { 0x28, 0xFF, 0xE, 0x97, 0x65, 0x14, 0x1, 0x94 };
//P[2] = { 0x28, 0xFF, 0x6E, 0x35, 0x65, 0x14, 0x1, 0x2B };

void setup()
{
Serial.begin(9600);
sensors.begin();
sensors.setResolution(P[0], 12);
sensors.setResolution(P[1], 12);
sensors.setResolution(P[2], 12);
Wire.begin();
digitalWrite(swiatlo, LIGHT_OFF);
pinMode(swiatlo, OUTPUT);
pinMode(went_1, OUTPUT);
pinMode(went_2, OUTPUT);
pinMode(went_3, OUTPUT);
lcd.begin(20, 4);
lcd.createChar(0, temp);
}


void loop() {
czasidata();
czasowy();
termometry();
}
byte bcdToDec(byte val)  {
return ( (val / 16 * 10) + (val % 16) );
}

void czasowy() {
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time

if ((hour == 12 ) && (minute == 30))
  digitalWrite(swiatlo, LIGHT_ON);
if ((hour == 12) && (minute == 31))
  digitalWrite(swiatlo, LIGHT_OFF);
}

void czasidata() {
tmElements_t tm;

if (RTC.read(tm)) {
 
  lcd.setCursor (0, 0);
  LCDprint2digits(tm.Hour);
  lcd.print(':');
  LCDprint2digits(tm.Minute);
  lcd.print(':');
  LCDprint2digits(tm.Second);
  lcd.setCursor (9, 0);
  lcd.print(tm.Day);
  lcd.print('/');
  lcd.print(monthName[tm.Month-1]);
  lcd.print('/');
  lcd.print(tmYearToCalendar(tm.Year));
 
 
} else {
  if (RTC.chipPresent()) {
    lcd.println("DS1307 stoi. Uruchom Zegar");
    }
     else {
    lcd.println("DS1307 blad oczczytu!");
    lcd.println("sprawdz polaczenie");
  }
}
}


void termometry() {

for(int u=0;u<3;u++)
{
 float temp = sensors.getTempC(P[u]);
 lcd.setCursor (0, u+1);
 lcd.write(byte(0));
 lcd.print(" ");
 lcd.print(temp, 2);
 lcd.write(byte(223));
 if (sensors.getTempCByIndex(u) < (nastaw+histereza)) {
   histereza=0;
   digitalWrite(w[u], HIGH);
   int val = digitalRead(w[u]);
   if (val)
   lcd.setCursor(9, u+1);
   lcd.print("Ok ");
 }
 else {
   lcd.setCursor(9, u+1);
   lcd.print("Wla");
   histereza=-2;
   digitalWrite(w[u], LOW);
 }
}
}

void LCDprint2digits(int number) {
if (number >= 0 && number < 10) {
  lcd.write('0');
}
lcd.print(number);

}

Odpowiednie fragmenty kodu, które zmieniłem oznaczyłem komentarzem.
Pozdrawiam
Dziękuję Tobie bardzo, program kompiluje się, ale temperatura pokazywana kolejno nie zmienia się, próbowałem resetować arduino, wgrać od nowa program trzymając czujniki w ręku w celu podniesienia temperatury, odczyt cały czas jest taki sam: 22.37, 21.94. 22.31. Nawet przeniesienie zawartości funkcji "termometry" do pętli głównej nie przyniosło efektu. Dodam, że zegar i wyłącznik czasowy działają prawidłowo, w funkcji "czasidata" także okazują kolejno 22.37, 21.94. 22.31.
Witam
Wiesz na razie nie przyglądałem się "logice" Twojego programu. Skupiłem się raczej na poprawie błędów i uproszczeniu kodu tak jak chciałeś. Może jutro postaram się oblukać co masz skopane. Najprościej to testować każdą z tych funkcji osobno: czas temperaturę itd. 
Tak na szybko to mam 2 uwagi: Nie widzę tu delay oraz lcd.clear(). 
Pewnie nakładają Ci się odczyty i w sumie nie masz możliwości zobaczenia np. czasu, bo przykrywa to pomiar temperatury, który trwa najdłużej. 
Sprawdź każdą z funkcji osobno i sam zobaczysz o co biega.
Pozdrawiam
Już nieistotne i przepraszam za kłopot, ale postanowiłem, że zostanie to w pierwotnej wersji, bo łatwiej mi operować w ten sposób na funkcji termometry. Problem miałem z histereza, bo przypisując do niej jedną zmienną globalną załączało mi trzy przekaźniki jednocześnie gdy jeden z czujników osiągnął pożądaną temperaturę i wyłączało wszystkie trzy gdy np. na drugim temperatura spadła poniżej dolnej pożądanej granicy. Rozwiązałem to w taki sposób, że do każdego czujnika przypisałem osobną zmienną.
Poniżej cały kod jakby ktoś chciał kiedyś skorzystać.

Kod:
#include <DS1307RTC.h>
#include <Time.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DS1307_ADDRESS 0x68
#define LIGHT_ON 0
#define LIGHT_OFF 1
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

#define ONE_WIRE_BUS 12
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

int nastaw = 26; // nastaw temperatury zalaczenia przekaznikow wentylatorow
int swiatlo = 11; // pin przekaznika oswietlenia
int went_1 = 8; // pin przekaznika wentylatora 1
int went_2 = 9; // pin przekaznika wentylatora 2
int went_3 = 10; // pin przekaznika wentylatora 3
volatile int his_1=0;
volatile int his_2=0;
volatile int his_3=0;
byte state = LOW;
const char *monthName[12] = {
 "Sty", "Lut", "Mar", "Kwi", "Maj", "Cze",
 "Lip", "Sie", "Wrz", "Paź", "Lis", "Gru"
};

byte temp[8] = //ikona temperatury
{
 B00100,
 B01010,
 B01010,
 B01110,
 B01110,
 B11111,
 B11111,
 B01110
};

DeviceAddress P1 = { 0x28, 0xFF, 0xB2, 0x4C, 0x65, 0x14, 0x1, 0x6F };
DeviceAddress P2 = { 0x28, 0xFF, 0xE, 0x97, 0x65, 0x14, 0x1, 0x94 };
DeviceAddress P3 = { 0x28, 0xFF, 0x6E, 0x35, 0x65, 0x14, 0x1, 0x2B };

void setup()
{
 Serial.begin(9600);
 sensors.begin();
 sensors.setResolution(P1, 12);
 sensors.setResolution(P2, 12);
 sensors.setResolution(P3, 12);
 Wire.begin();
 digitalWrite(swiatlo, LIGHT_OFF);
 pinMode(swiatlo, OUTPUT);
 pinMode(went_1, OUTPUT);
 pinMode(went_2, OUTPUT);
 pinMode(went_3, OUTPUT);
 lcd.begin(20, 4);
 lcd.createChar(0, temp);
}


void loop() {
 czasidata();
 czasowy();
 termometry();
}
byte bcdToDec(byte val)  {
 return ( (val / 16 * 10) + (val % 16) );
}

void czasowy() {
 Wire.beginTransmission(DS1307_ADDRESS);
 byte zero = 0x00;
 Wire.write(zero);
 Wire.endTransmission();
 Wire.requestFrom(DS1307_ADDRESS, 7);
 int second = bcdToDec(Wire.read());
 int minute = bcdToDec(Wire.read());
 int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time

 if ((hour == 12 ) && (minute == 30))
   digitalWrite(swiatlo, LIGHT_ON);
 if ((hour == 12) && (minute == 31))
   digitalWrite(swiatlo, LIGHT_OFF);
}

void czasidata() {
 tmElements_t tm;

 if (RTC.read(tm)) {
 
   lcd.setCursor (0, 0);
   LCDprint2digits(tm.Hour);
   lcd.print(':');
   LCDprint2digits(tm.Minute);
   lcd.print(':');
   LCDprint2digits(tm.Second);
   lcd.setCursor (9, 0);
   lcd.print(tm.Day);
   lcd.print('/');
   lcd.print(monthName[tm.Month-1]);
   lcd.print('/');
   lcd.print(tmYearToCalendar(tm.Year));
   
   
 } else {
   if (RTC.chipPresent()) {
     lcd.println("DS1307 stoi. Uruchom Zegar");
     }
      else {
     lcd.println("DS1307 blad oczczytu!");
     lcd.println("sprawdz polaczenie");
   }
 }
}


void termometry() {
 
 sensors.requestTemperatures();
 float tempC1 = sensors.getTempC(P1);
 float tempC2 = sensors.getTempC(P2);
 float tempC3 = sensors.getTempC(P3);
 
 lcd.setCursor (0, 1);
 lcd.write(byte(0));
 lcd.print(" ");
 lcd.print(tempC1, 2);
 lcd.write(byte(223));

 if (sensors.getTempCByIndex(0)+his_1 < (nastaw)) {
   his_1=0;
   digitalWrite(went_1, HIGH);
   int val = digitalRead(went_1);
   if (val)
     lcd.setCursor(9, 1);
   lcd.print("Ok ");
 } else {
   his_1=2;
   digitalWrite(went_1, LOW);
   lcd.setCursor(9, 1);
   lcd.print("Wla");
 }
 lcd.setCursor (0, 2);
 lcd.write(byte(0));
 lcd.print(" ");
 lcd.print(tempC2, 2);
 lcd.write(byte(223));
 if (sensors.getTempCByIndex(1)+his_2 < (nastaw)) {
   his_2=0;
   digitalWrite(went_2, HIGH);
   int val = digitalRead(went_2);
   if (val)
     lcd.setCursor(9, 2);
   lcd.print("Ok ");
 } else {
   his_2=2;
   digitalWrite(went_2, LOW);
   lcd.setCursor(9, 2);
   lcd.print("Wla");
 }
 lcd.setCursor (0, 3);
 lcd.write(byte(0));
 lcd.print(" ");
 lcd.print(tempC3, 2);
 lcd.write(byte(223));
 if (sensors.getTempCByIndex(2)+his_3 < (nastaw)) {
   his_3=0;
   digitalWrite(went_3, HIGH);
   int val = digitalRead(went_3);
   if (val)
     lcd.setCursor(9, 3);
   lcd.print("Ok ");
 } else {
   his_3=2;
   digitalWrite(went_3, LOW);
   lcd.setCursor(9, 3);
   lcd.print("Wla");
 }
}


void LCDprint2digits(int number) {
 if (number >= 0 && number < 10) {
   lcd.write('0');
 }
 lcd.print(number);

}

Teraz będę próbował wykorzystać Twój tuturial z majsterkowo - gotowiec3 - obsługa 3 przycisków, do zmiany parametru nastawu temperatury. Nie wiem czy mi coś z tego wyjdzie, zobaczymy, wszystkie wskazówki mile widziane. Wink

Pozdrawiam Smile
Stron: 1 2 3