Arduino Forum

Using Arduino => Programming Questions => Topic started by: liquidarts on May 27, 2011, 12:38 pm

Title: DS18B20 relay control help.....
Post by: liquidarts on May 27, 2011, 12:38 pm
Could someone check my coding to see if I did this right.....am trying to control 2 relays using 2 different DS18B20's in parasite mode

Code: [Select]
// DS18B20 display
sensors.requestTemperatures(); // Send the command to get temperatures
delay(250);

  lcd.setCursor(13, 2);

float temp1=0, temp2=0;

  //lcd.print("Led Temp:");
  temp1=sensors.getTempFByIndex(1);
  lcd.print(sensors.getTempFByIndex(1));
  lcd.print((char)223);
  lcd.print("F");

  lcd.setCursor(2, 2);
  //lcd.print("Tank Temp: ");
  temp2=sensors.getTempFByIndex(0);
  lcd.print(sensors.getTempFByIndex(0));
  lcd.print((char)223);
  lcd.print("F");



if ( (temp1) > 98)
    {
    analogWrite(fan, 255);
    lcd.setCursor(17,3);
    lcd.print("On");
    }
else if ( (temp1) < 92)
    {
    analogWrite(fan, 0);
    lcd.setCursor(17,3);
    lcd.print("Off");
    }
delay(500);

if ( (temp2) < 78 )
    {
    analogWrite(heater, HIGH);
    //lcd.setCursor(17,3);
    //lcd.print("On");
    }
else if ( (temp2) > 82 )
    {
    analogWrite(heater, LOW);
    //lcd.setCursor(17,3);
    //lcd.print("Off");
    }
delay(500);
Title: Re: DS18B20 relay control help.....
Post by: robtillaart on May 27, 2011, 12:48 pm
A quick view showed no errors but please post the whole code to get a whole review.

Does the code fail somehow? You don't mention any problem...

Some parts could be efficienter coded
  temp1=sensors.getTempFByIndex(1);
  lcd.print(sensors.getTempFByIndex(1));

==>

  temp1=sensors.getTempFByIndex(1);
  lcd.print(temp1,2);   // temp1 is a float so 2 is the nr of decimals

Title: Re: DS18B20 relay control help.....
Post by: Simpson_Jr on May 27, 2011, 01:29 pm
Unfortunately I can't see which pins are driven by the variables fan and heater.
Looking at the program, you want to turn both either completely on or completely off.

Instead of turning them on/off with a digitalWrite() you're using analogWrite(), which is possible.
AnalogWrite doesn't work with all pins though, it's meant for PWM-pins only.

In the first case you use analogWrite() correctly by sending a value of 0 or 255, but the heater is driven by a HIGH and a LOW. I haven't tried to see whether that is possible, but it would be better IMO to use a number instead or go for the digitalWrite-command.

Title: Re: DS18B20 relay control help.....
Post by: liquidarts on May 27, 2011, 01:37 pm

Unfortunately I can't see which pins are driven by the variables fan and heater.
Looking at the program, you want to turn both either completely on or completely off.

Instead of turning them on/off with a digitalWrite() you're using analogWrite(), which is possible.
AnalogWrite doesn't work with all pins though, it's meant for PWM-pins only.

In the first case you use analogWrite() correctly by sending a value of 0 or 255, but the heater is driven by a HIGH and a LOW. I haven't tried to see whether that is possible, but it would be better IMO to use a number instead or go for the digitalWrite-command.




Thanks for seeing that mistake Simpson_Jr.....was wondering why the LED I have in place currently was turning on and why the seconds weren't updating every second but skipping one here n there.I'll stick with the analogWrite...thanks for your help guys....cheers