Vytápěné demižony

Chcete se pochlubit, slyšet názor někoho jiného nebo hledáte spolupráci ke svému projektu?
Odpovědět
Zpráva
Autor
Uživatelský avatar
newerMore
mimozemský/á
Příspěvky: 811
Registrován: 19 čer 2008 19:13
Kontaktovat uživatele:

Vytápěné demižony

#1 Příspěvek od newerMore » 31 led 2013 17:11

Elektronicky vytápěné demižony se stabilizovanou teplotou, řízené mikroprocesorem ATmega168 a teplotními čidly DS18B20.

Kód: Vybrat vše

/*

Vyroba domaci medoviny
======================

            Power source (3.3-5V):
                  USB | Acu (4-8x AA 1,5V .. AAA 1.2V | 9V)
                      |
  -------------- ATmega168 = Arduino ---------
  4=2    5=3     14=8    16..19=10..13
   |      |        |           |
  1Wire   PWM     LED      SPI & SD
          Heating
  
TODO
----
### COMUNICATION ###
+ ISP (for on-chip Arduino without Arduino board)
- Serial
+ OneWire
### INPUTS ###
- Photodiode
+ Temp
- EC
- Ph
- CO2
### STORAGE ###
- SD
### OUTPUTS ###
- 7segment LED
- LCD

*/

#include "OneWire.h"

int ledPin = 8;
OneWire ds(2);          // OneWire pin
int pwmPin = 3;         // PWM
//int photoPin = A0;    // AD
//int speakerPin = 5;   // Speaker
int heatingPin = 4;      // Switch ON/OFF


/*int photoDiode()
{
  return analogRead(photoPin);
}*/


// ----- SPEAKER -----

/*void speakerPulse(int ms)
{
  digitalWrite(speakerPin, HIGH);
  delay(ms);
  digitalWrite(speakerPin, LOW);
}

void beep(void)
{
  speakerPulse(1000);
}

void sound(int freq, int durationMilliseconds)
{
  int halfPeriod = 500000 / freq;
  unsigned long int periods = durationMilliseconds;
 periods  *= 500;
 periods  = periods / halfPeriod;

  for(int p = 0; p < periods; p++) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(halfPeriod);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(halfPeriod);
  }
}*/


// ----- OneWire -----

int mysearch(byte *addr)
{
  int result;
  digitalWrite(ledPin, LOW);
  result = ds.search(addr);
  digitalWrite(ledPin, HIGH);
  return result;
}

void oneWireDevices()
{
  byte addr[8];

  ds.reset_search();
  
  while (/*ds.search(addr)*/ mysearch(addr)) {
    /*digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    delay(1000);*/
    //printAddress(addr);
  
    if ( OneWire::crc8( addr, 7) != addr[7]) {
      //Serial.print("CRC is not valid!\n");
      continue;
    }
  
    runFamilyCode(addr);
  }
}


/*void printAddress(byte addr[8])
{
  for (byte i = 0; i < 8; i++) {
    Serial.print(addr[i], HEX);
    if (i < 7)
      Serial.print(":");
  }
  Serial.print(" ");
}*/


/*
 * Return 1 if devices' family code is correct, otherwise returns 0
 */
int runFamilyCode(byte addr[8])
{
  switch (addr[0]) {
  case 0x10:
    //Serial.print("[DS18S20] ");
    ds18X20Temp(addr);
    break;

  case 0x28:
    //Serial.print("[DS18B20] ");
    ds18X20Temp(addr);
    break;

  default:
    //Serial.print("Device family is not recognized: 0x");
    //Serial.println(addr[0],HEX);
    return 0;
  }
  return 1;
}


void ds18X20Temp(byte addr[8])
{
  //byte present = 0;
  byte data[12];

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);         // start conversion, with parasite power on at the end

  delay(850);     // maybe 750ms is enough, maybe not

  // we might do a ds.depower() here, but the reset will take care of it.
  //present =
  ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  //Serial.print(present,HEX);
  //Serial.print("{");
  for (byte i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    //Serial.print(data[i], HEX);
    //Serial.print(":");
  }
  //Serial.print("{");
  //Serial.print( OneWire::crc8( data, 8), HEX);
  //Serial.print("}} = ");
  printTemp(data[0], data[1]);
}


void printTemp(byte data0, byte data1) {
  int TReading, SignBit, Tc_100, Whole, Fract;

  TReading = (data1 << 8) + data0;
  SignBit = TReading & 0x8000;  // test most sig bit
  if (SignBit) // negative
  {
    TReading = (TReading ^ 0xffff) + 1; // 2's comp
    //Serial.print("-");
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25
  
  Whole = Tc_100 / 100;  // separate off the whole and fractional portions
  Fract = Tc_100 % 100;

  // Switch heating
  if(Whole < 45) {
    analogWrite(pwmPin, (byte) (255 - min(255, (45 - Tc_100 / 100.0) * 255)));
    //switchOnHeating();
  } else { //if(Whole > 27)
    analogWrite(pwmPin, 255);
    //switchOffHeating();
  }

  //Blink with LED
  for (byte i = 40; i < Whole; i++) {
    digitalWrite(ledPin, LOW);
    delay(300);
    digitalWrite(ledPin, HIGH);
    delay(150);
    //beep();
    //delay(150);
  }
  delay(500);
//  sound(Tc_100, 1000);
//  speakerPulse(250);
   
/*  sound(2000, 1000);
  sound(3000, 1000);
  sound(4000, 1000);
  sound(5000, 1000);
*/

  //Serial.print(Whole);
  //Serial.print(".");
  //if (Fract < 10) {
  //  Serial.print("0");
  //}
  //Serial.print(Fract);
  //Serial.println(" deg. C");  // °
}


/*void switchOnHeating()
{
  digitalWrite(heatingPin, LOW);
}

void switchOffHeating()
{
  digitalWrite(heatingPin, HIGH);
}*/



// ---------- SETUP ----------
void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  //pinMode(speakerPin, OUTPUT);
  pinMode(heatingPin, OUTPUT);
  pinMode(pwmPin, OUTPUT);
}


// ---------- LOOP ----------
void loop()
{
  int incomingByte = 0;
  /*int photoValue = 0;
  
  photoValue = photoDiode();
  Serial.print("Photo diode: ");
  Serial.println(photoValue);*/
  
  oneWireDevices();
  //Serial.print('-');
  
  /*if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);
  }*/

  //digitalWrite(ledPin, HIGH);
  
  /*for (int i = 0; i < 255; i++) {
    analogWrite(pwmPin, i);
    delay(500);
  }*/
 
  /*digitalWrite(ledPin, HIGH);
  delay(3000);
  digitalWrite(ledPin, LOW);
  delay(3000);*/
}



Uživatelský avatar
newerMore
mimozemský/á
Příspěvky: 811
Registrován: 19 čer 2008 19:13
Kontaktovat uživatele:

Re: Vytápěné demižony

#2 Příspěvek od newerMore » 20 úno 2013 00:59

/*

Vyroba domaci medoviny
======================

Power source (3.3-5V):
USB | Acu (4-8x AA 1,5V .. AAA 1.2V | 9V)
|
-------------- ATmega168 = Arduino ---------
4=2 5=3 14=8 16..19=10..13
| | | |
1Wire PWM LED SPI & SD
Heating

TODO
----
### COMUNICATION ###
+ ISP (for on-chip Arduino without Arduino board)
- Serial
+ OneWire
### INPUTS ###
- Photodiode
+ Temp
- EC
- Ph
- CO2
### STORAGE ###
- SD
### OUTPUTS ###
- 7segment LED
- LCD

*/

#include "OneWire.h"

int ledPin = 8;
OneWire ds(2); // OneWire pin
int pwmPin = 3; // PWM
//int photoPin = A0; // AD
//int speakerPin = 5; // Speaker
int heatingPin = 4; // Switch ON/OFF


/*int photoDiode()
{
return analogRead(photoPin);
}*/


// ----- SPEAKER -----

/*void speakerPulse(int ms)
{
digitalWrite(speakerPin, HIGH);
delay(ms);
digitalWrite(speakerPin, LOW);
}

void beep(void)
{
speakerPulse(1000);
}

void sound(int freq, int durationMilliseconds)
{
int halfPeriod = 500000 / freq;
unsigned long int periods = durationMilliseconds;
periods *= 500;
periods = periods / halfPeriod;

for(int p = 0; p < periods; p++) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(halfPeriod);
digitalWrite(speakerPin, LOW);
delayMicroseconds(halfPeriod);
}
}*/


// ----- OneWire -----

int mysearch(byte *addr)
{
int result;
digitalWrite(ledPin, LOW);
result = ds.search(addr);
digitalWrite(ledPin, HIGH);
return result;
}

void oneWireDevices()
{
byte addr[8];

ds.reset_search();

while (/*ds.search(addr)*/ mysearch(addr)) {
/*digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);*/
printAddress(addr);

if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.print("CRC is not valid!\n");
continue;
}

runFamilyCode(addr);
}
}


void printAddress(byte addr[8])
{
for (byte i = 0; i < 8; i++) {
Serial.print(addr, HEX);
if (i < 7)
Serial.print(":");
}
Serial.print(" ");
}


/*
* Return 1 if devices' family code is correct, otherwise returns 0
*/
int runFamilyCode(byte addr[8])
{
switch (addr[0]) {
case 0x10:
Serial.print("[DS18S20] ");
ds18X20Temp(addr);
break;

case 0x28:
Serial.print("[DS18B20] ");
ds18X20Temp(addr);
break;

default:
Serial.print("Device family is not recognized: 0x");
Serial.println(addr[0], HEX);
return 0;
}
return 1;
}


void ds18X20Temp(byte addr[8])
{
//byte present = 0;
byte data[12];

ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end

delay(850); // maybe 750ms is enough, maybe not

// we might do a ds.depower() here, but the reset will take care of it.
//present =
ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

//Serial.print(present,HEX);
//Serial.print("{");
for (byte i = 0; i < 9; i++) { // we need 9 bytes
data = ds.read();
//Serial.print(data, HEX);
//Serial.print(":");
}
//Serial.print("{");
//Serial.print( OneWire::crc8( data, 8), HEX);
//Serial.print("}} = ");
printTemp(data[0], data[1]);
}


void printTemp(byte data0, byte data1) {
int TReading, SignBit, Tc_100, Whole, Fract;

TReading = (data1 << 8) + data0;
SignBit = TReading & 0x8000; // test most sig bit
if (SignBit) // negative
{
TReading = (TReading ^ 0xffff) + 1; // 2's comp
//Serial.print("-");
}
Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25

Whole = Tc_100 / 100; // separate off the whole and fractional portions
Fract = Tc_100 % 100;

// Switch heating
if(Whole < 45) {
analogWrite(pwmPin, (byte) (255 - min(255, (45 - Tc_100 / 100.0) * 255)));
//switchOnHeating();
} else { //if(Whole > 27)
analogWrite(pwmPin, 255);
//switchOffHeating();
}

//Blink with LED
for (byte i = 40; i < Whole; i++) {
digitalWrite(ledPin, LOW);
delay(300);
digitalWrite(ledPin, HIGH);
delay(150);
//beep();
//delay(150);
}
delay(500);
// sound(Tc_100, 1000);
// speakerPulse(250);

/* sound(2000, 1000);
sound(3000, 1000);
sound(4000, 1000);
sound(5000, 1000);
*/

Serial.print(Whole);
Serial.print(".");
if (Fract < 10) {
Serial.print("0");
}
Serial.print(Fract);
Serial.println(" deg. C");
}


/*void switchOnHeating()
{
digitalWrite(heatingPin, LOW);
}

void switchOffHeating()
{
digitalWrite(heatingPin, HIGH);
}*/



// ---------- SETUP ----------
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
//pinMode(speakerPin, OUTPUT);
pinMode(heatingPin, OUTPUT);
pinMode(pwmPin, OUTPUT);
}


// ---------- LOOP ----------
void loop()
{
int incomingByte = 0;
/*int photoValue = 0;

photoValue = photoDiode();
Serial.print("Photo diode: ");
Serial.println(photoValue);*/

oneWireDevices();
//Serial.print('w');

if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

// say what you got:
//Serial.print("I received: ");
Serial.println(incomingByte, BYTE);
}

//digitalWrite(ledPin, HIGH);

/*for (int i = 0; i < 255; i++) {
analogWrite(pwmPin, i);
delay(500);
}*/

/*digitalWrite(ledPin, HIGH);
delay(3000);
digitalWrite(ledPin, LOW);
delay(3000);*/
}

Odpovědět

Kdo je online

Uživatelé prohlížející si toto fórum: Žádní registrovaní uživatelé a 1 host