Pages: 1 [2]   Go Down
Author Topic: Freezer code error help  (Read 923 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Lets start with a review of the global variables.
Code:
int tempReading;
int tempPin = 1;
int photocellReading;
int photocellPin = 2;
int relayPin = 6;
float temperatureC;

The setup() function references relayPin. All the others are used only in loop() or functions called by loop(). Therefore, they should not be global in scope.

Code:
    if (relayPin = HIGH)
    if (relayPin = LOW)
Bzzzt. Wrong. You just assigned a new value to relayPin. This illustrates why pin variables (relayPin, tempPin, etc.) should be either #define or const. In this case, #define would not have help, but making relayPin const would have. The compiler would have told you that you were trying to overwrite a const variable, and that that is not allowed.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Paul my good man, you are a champ haha. Let me explain - Running one program as a tester in which i change the code and see if it works or not, and then if it works, put it back into my main code. The code i last posted was the testing one in which i was attempting to display the status of the relay pin. Regardless After a bit more research I see the error in how I was trying to do it.
I also understand Global and Local scope better altho one question as a side not - you said
Quote
All the others are used only in loop() or functions called by loop(). Therefore, they should not be global in scope.
so does that mean that i can even move my " float temperatureC into the void loop? In other words even though temperatureC is called in the subroutine, it only needs to be declared locally?

Back to the problems that I am having - I measured the output voltage from the Digital I/O pin and it is 5 volts, and I rebuilt the circuit again, so there is no problem with the freezer. Ive gone over the code and over it and still don't understand what I am doing wrong. I changed the DAYHIGH, DAYLOW ect. to floating variable to give them decimal places like the temperature but even still it doesnt follow the routine. It just outputs a HIGH and doesn't change. Also when I change any of those parameters to a positive temperature (eg DAYHIGH 15.0 ) the arduino will not output anything at all. Could the delay be an issue?

Once again, thanks for your help - and here is the code
Code:
#include <Wire.h>
#include "RTClib.h"
#define NIGHTHIGH -19.0
#define NIGHTLOW -21.0
#define DAYHIGH 3.0
#define DAYLOW 2.0
#define aref_voltage 3.3

RTC_DS1307 RTC;

int relayPin = 6;
float temperatureC;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  pinMode(relayPin, OUTPUT);      // sets relay pin as output
  analogReference(EXTERNAL);
}

void loop()
{
  int tempReading;
  int tempPin = 1;
  int photocellReading;
  int photocellPin = 2;

  photocellReading = analogRead(photocellPin); 

  if (photocellReading < 10)
  {     
    Serial.println("Dark");
  }
  else if (photocellReading < 200)
  {
    Serial.println("Dim");
  }
  else if (photocellReading < 500)
  {
    Serial.println("Light");
  }
  else if (photocellReading < 800)
  {
    Serial.println("Bright");
  }
  else
  {
    Serial.println("Very bright");
  }

  tempReading = analogRead(tempPin); 

  // converting that reading to voltage, which is based off the reference voltage
  float voltage = tempReading * aref_voltage / 1024;

  // now print out the temperature
  float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
  //to degrees ((volatge - 500mV) times 100)
  Serial.print(temperatureC);
  Serial.println(" degrees C");

  DateTime now = RTC.now();                   // Get time from the RTC chip

  Serial.print("Time:");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();

  delay(1000);

  if (now.hour() >= 7 && now.hour() <= 18)   //If time is between 7am and 7pm print Day time setting)
  {
    Daytime();
    Serial.println("Temp set to -5'C");        // Display what Setting we are currently in
  }
  else                                     //Else set to Night time setting 
  {                                 
    Nighttime();
    Serial.println("Temp set to -20'C");      // Display what Setting we are currently in
  }
}

void Nighttime() 
{                     
  if (temperatureC > (float)NIGHTHIGH)  //Check temp and compare with setting and set the output accordingly
  {
    digitalWrite(relayPin, HIGH);
  }
  else if (temperatureC < (float)NIGHTLOW)
  {
    digitalWrite(relayPin, LOW);
  }
}

void Daytime()
{
  if (temperatureC > (float)DAYHIGH)  //Check temp and compare with setting and set the output accordingly
  {
    digitalWrite(relayPin, HIGH);
  }
  else if (temperatureC < (float)DAYLOW)
  {
    digitalWrite(relayPin, LOW);
  }
}

Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try putting your print statements back in the daytime and nighttime functions. If that doesn't help solve it, post the new code and the serial output again. Also, do you have a simpler sketch that proves you have control of the freezer using this relay? i.e. can you actually get it to run and stop?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A global variable:
Code:
float temperatureC;
(initial value is 0)

A local variable containing the value read from the sensor:
Code:
  float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset

A statement in a different function:
Code:
  if (temperatureC > (float)NIGHTHIGH)  //Check temp and compare with setting and set the output accordingly
Cat you guess which temperatureC this refers to? Hint: It's not the one in loop().
Logged

Pages: 1 [2]   Go Up
Jump to: