hey i need some help with my first Arduino hack, I have a greenhouse in which I have an oil heater which cycle on and off by pressing a button, if you hold the button it´s reporting an error - so it must be a pressure "on" and then "off" ie a so-called simulate pushbutton hack to be turned on by my arduino with a dallas temp sensor and a solid-state relay.
but my relay turns on and off constantly, and it is here you can help a beginner Thank you
my code looks so far like this:
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
//set maximum tank temperature
int drivhusMax = 27.00;
//set maximum tank temperature
int drivhusMin = 25.00;
DeviceAddress insideThermometer = { 0x28, 0x34, 0x6d, 0x95, 0x04, 0x0, 0x0, 0x2c };
//variables used for control logic
static bool ovnOn;
//variables to hold temperature values
float temp;
//define pin for pump relay
const int ovn = A1;
void setup(void)
{
// start serial port
Serial.begin(9600);
// Start up the library
sensors.begin();
// set the resolution to 10 bit (good enough?)
sensors.setResolution(insideThermometer, 10);
//set pins to output
pinMode (ovn, OUTPUT);
digitalWrite(ovn, LOW);
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
Serial.print("Error getting temperature");
} else {
Serial.print("C: ");
Serial.print(tempC);
Serial.print(" F: ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
}
}
void loop(void)
{
delay(2000);
Serial.print("Getting temperatures...\n\r");
sensors.requestTemperatures();
//assign value to variables
temp = (sensorValue(insideThermometer));
Serial.print("Inside temperature is: ");
printTemperature(insideThermometer);
Serial.print("\n\r");
if ( (drivhusMin < temp ) )
{
digitalWrite (ovn, HIGH);
delay(250);
digitalWrite(ovn, LOW);
ovnOn = true;
}
else
{
if ( (drivhusMax > temp ) )
digitalWrite (ovn, HIGH);
delay(250);
digitalWrite(ovn, LOW);
ovnOn = false;
}
Serial.print ("ovn status ");
Serial.println (ovnOn);
Serial.println ("");
Serial.println ("");
}
float sensorValue (byte deviceAddress[])
{
float tempC = sensors.getTempC (deviceAddress);
float tempF = (DallasTemperature::toFahrenheit(tempC));
return tempC;
}
Moderator edit: </mark> <mark>[code]</mark> <mark>
The code in your main loop will toggle on for 250 ms every 2 s, except for when the temperature is between 25 and 27. I'm not sure if this is what you wanted, what kind of temperatures are you getting from the output
Hello
No, I want the program to start the oven when it is at minimum, and turn off when it reaches maximum, but it must do it every time with an on / off therefore the 250 msec -remember I´m new in this.
Thx
tobyb121:
The code in your main loop will toggle on for 250 ms every 2 s, except for when the temperature is between 25 and 27. I'm not sure if this is what you wanted, what kind of temperatures are you getting from the output
The ovn (is it really that difficult to type oven?) won't really be on, will it? Why set the boolean true after turning the oven off?
if ( (drivhusMax > temp ) )
digitalWrite (ovn, HIGH);
If that how you talk? If the maximum temperature is greater than the current temperature, do something? No, of course it isn't. So, structure the code to match:
if(temp < drivhusMax)
digitalWrite (ovn, HIGH);
Pay attention to how many parentheses are really needed.
But, again, why are you testing the temperature again? You already know, at this point, that it is too hot, and that you need to turn the oven off.
I think the oven is turned on or off in each case by simulating the press and release of a switch, so the 250 is the delay to make sure the "press" is noticed.
The issue then is that you're not checking oven state. If the temperature requires that the oven be on, you need to be sure that it isn't already, otherwise pressing the button turns it off unnecessarily.
ovn is oven in danish and drivhus is greenhouse that´s it.
OK. Then I apologize for picking on your spelling.
I think that what you need to do, though, is turn the oven on when it is cold, and off when it is hot, without worrying about whether it is on or off, and without turning it back off right away.
I think I get what you are trying to do now, each 250 ms HIGH period will cause the heater to toggle between on/off, which also explains why it is toggling on and off at the moment, if you imagine that the temperature is 20 deg, the heater will be switched on, the next time it goes through the loop the heater will be on, but it's only been 2s, so the temperature isn't going to have changed much, as a result, your code will treat this as the oven being off, so will toggle it, switching it off again.
You already have a variable (ovnOn) which is keeping track of whether the oven is on or off, you just need to use it:
if ( !ovnOn && temp < drivhusMin ) //if the oven is switched off, and then temperature is less than drivhusMin
{
digitalWrite (ovn, HIGH);
delay(250);
digitalWrite(ovn, LOW);
ovnOn = true;
}
else if ( ovnOn && temp > drivhusMax )//if the oven is switched on, and then temperature is greater than drivhusMax
digitalWrite (ovn, HIGH);
delay(250);
digitalWrite(ovn, LOW);
ovnOn = false;
}
(BTW the greater than/less than signs in your logic were the wrong way round)
Why do you want the heater (ovn) to go on/off every 250ms. The relay is going to go click, click, click, click every second. The latency time (dødtid) in your system is going to far higer than that so why not keep the on/off cycles much higher when you are in the temperature regon where you wanna heat the greenhouse.
It seems like a strange way to lower the heat output to 50% of nominel capacity.
I just want the relay to click two times to start the oven when drivhusMin is reached, and click two times when drivhusMax is reached, but I can not get it to work.
the 250ms is to simulate the touch of a button.
this code works, it took me some time to figure out the "else" should off
if ( !ovnOn && temp < drivhusMin ) //if the oven is switched off, and then temperature is less than drivhusMin
{
digitalWrite (ovn, HIGH);
delay(250);
digitalWrite(ovn, LOW);
ovnOn = true;
}
if ( ovnOn && temp > drivhusMax ) //if the oven is switched off, and then temperature is less than drivhusMin
{
digitalWrite (ovn, HIGH);
delay(250);
digitalWrite(ovn, LOW);
ovnOn = false;
}
It won't make any difference to how the sketch works, but I'd suggest that you sort out your indentation - as you have it, it looks like the first if contains the second, which isn't the case. Your second comment is a copy of the first too, which obviously isn't appropriate. Personally, I'd take this repeated code: