Pages: [1]   Go Down
Author Topic: what does #endif //ECHO_TO_SERIAL do in this code  (Read 494 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What does it do and why is this necessary,or is it necessary ?

Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);


// degree fahrenheit
byte newChar[8]= {
        B01100,
        B10010,
        B10010,
        B01100,
        B00000,
        B00000,
        B00000,
        B00000
};


#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include "DHT.h"

/*
 A simple data logger for the Arduino analog pins.  This one is simplifed by taking away the
 calculation that determined file flush interval.  Since the readings are far enought apart,
 the file is flushed for every reading.
 */

#define ECHO_TO_SERIAL 1 // echo data to serial port

// The analog pins that connect to the sensors
#define PHOTOCELLPIN A0           // analog 0
#define chipSelect 10            // for the data logging shield, we use digital pin 10 for the SD cs line

#define DHTTYPE DHT11            // DHT 11 

#define DHTPIN 2  // The DHT output is connected to digital pin 7.
DHT dht(DHTPIN, DHTTYPE); //define the DHT object



RTC_DS1307 RTC; // define the Real Time Clock object

File fileData;                          // the logging file on the SD card
unsigned long ulintMillis = 0;              // milliseconds since start
int Relay = 3;
int intPhotocellreading = 0;
int light_sensitivity = 850;
float fHumidity = 0;
float fTemp = 0;

const unsigned long ulintLoginterval = 5000;       // mills between entries (reduce to take more/faster data)

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);     
}


void setup()//------------------------------------------------------------------------------------------------------------------------SETUP
{
  pinMode(Relay, OUTPUT);  //Relay
 
  Serial.begin(9600);
  //Serial.println();
  Serial.println("Millis, Date , Time , Light , Humidity , Temp");
 
  lcd.createChar(1, newChar);
  lcd.begin(16,2);
  lcd.clear();
  lcd.print("TEMP         LDR");
  lcd.setCursor(4,1);
  lcd.print("HUMIDITY");
 
  pinMode(DHTPIN, INPUT);
  pinMode(PHOTOCELLPIN, INPUT);
  pinMode(chipSelect, OUTPUT);


  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    error("Card failed, or not present, or datalog.csv file not present");
  }
  //Serial.println("card initialized.");
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  fileData = SD.open("datalog.csv", FILE_WRITE);

  // connect to RTC
  Wire.begin(); 
  if (!RTC.begin()) {
    fileData.println("RTC failed");
#if ECHO_TO_SERIAL
    error ("RTC failed");
#endif  //ECHO_TO_SERIAL
  }

  dht.begin();    // Start reading from the  temperature and humidity sensor

  if (fileData){
    fileData.println("Millis , Date , Time, Light, Humidity , Temp");
  }
  else{
    error("Could not write to file.");
  }

}

void loop(void)//---------------------------------------------------------------------------------------------------------LOOP
{
  DateTime now;

  // delay for the amount of time we want between readings
  delay((ulintLoginterval -1) - (millis() % ulintLoginterval));

  // log milliseconds since starting
  ulintMillis = millis();
  fileData.print(ulintMillis);           // milliseconds since start
  fileData.print(", ");
#if ECHO_TO_SERIAL
  Serial.print(ulintMillis);         // milliseconds since start
  Serial.print(", "); 
#endif
  // fetch the time
  now = RTC.now();
  // log time
  fileData.print(now.year(), DEC);
  fileData.print("/");
  fileData.print(now.month(), DEC);
  fileData.print("/");
  fileData.print(now.day(), DEC);
  fileData.print(",");
  fileData.print(now.hour(), DEC);
  fileData.print(":");
  fileData.print(now.minute(), DEC);
  fileData.print(":");
  fileData.print(now.second(), DEC);
#if ECHO_TO_SERIAL
  Serial.print(now.year(), DEC);
  Serial.print("/");
  Serial.print(now.month(), DEC);
  Serial.print("/");
  Serial.print(now.day(), DEC);
  Serial.print(",");
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.print(now.second(), DEC);
#endif //ECHO_TO_SERIAL

  intPhotocellreading = analogRead(PHOTOCELLPIN); 
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  fHumidity = dht.readHumidity();
  fTemp = dht.readTemperature(true);

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(fHumidity) || isnan(fTemp)) {
    Serial.println("Failed to read from DHT");
  }
  fileData.print(", ");   
  fileData.print(intPhotocellreading);
#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(intPhotocellreading);      //
#endif //ECHO_TO_SERIAL
  fileData.print(", ");   
  fileData.print(fHumidity);
#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(fHumidity);
#endif //ECHO_TO_SERIAL
  fileData.print(", ");   
  fileData.print(fTemp);
#if ECHO_TO_SERIAL
  Serial.print(", ");   
  Serial.print(fTemp);
#endif //ECHO_TO_SERIAL
  fileData.println();
#if ECHO_TO_SERIAL
  Serial.println();
#endif // ECHO_TO_SERIAL

lcd.clear();
lcd.setCursor(0,0);
lcd.print(fTemp);
lcd.write(1);
lcd.setCursor(0,1);
lcd.print(fHumidity);
lcd.print("%");
lcd.setCursor(12,0);
lcd.print("LDR");
lcd.setCursor(12,1);
lcd.print(intPhotocellreading);
fileData.flush();

  intPhotocellreading = analogRead(intPhotocellreading);          //reads the ldr’s value through LDR which we have set to Analog input 0 “A0″
  if (intPhotocellreading > light_sensitivity)
      {
  digitalWrite(Relay, HIGH);
  delay(180000);
  digitalWrite(Relay, LOW);
  delay(1620000);
        }
    if (intPhotocellreading < light_sensitivity)
      {
  digitalWrite(Relay, LOW);
  delay(900000);
        }
  }
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3453
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This line
Code:
#define ECHO_TO_SERIAL 1 // echo data to serial port
tells the compiler to replace ECHO_TO_SERIAL with 1 when compiling so, when the compiler comes across the line
Code:
#if ECHO_TO_SERIAL
it replaces the text with a 1 which makes the #if test true so the program lines between the #if and #endif are executed.  If you look at them you will see that they print to the serial monitor.

If you replace
Code:
#define ECHO_TO_SERIAL 1
with
Code:
#define ECHO_TO_SERIAL 0
then, when the replacement occurs the #if statement will evaluate to false and the serial output will not happen.  This provides a means of turning debug prints on and off when testing code simply by changing one line of code rather than finding an commenting many lines in a program.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 29
Posts: 1667
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

is it necessary ?

It isn't. I can't remember the details from the other day but it was pretty clear that a lot of your problem was caused by the code being cluttered with superfluous junk like this.

If I recall correctly, checking the presence of the DS1307 was another. It's either there or it isn't, the battery is good for about five years, and the only justification for checking it is there is that you are scared somebody might steal it.  I don't recall seeing what your intentions was in the event of that actually happening, but maybe it was hidden away somewhere.

Similarly for the stuff about memory. I see you are using a Due and I don't know about their memory requirements, but all you are doing is picking up data and passing it along. This uses little memory and I can't see why there would be a problem with that.

It would probably help if you laid out the code in the same order as the standard examples

preliminaries, libraries etc.
setup
loop
subroutines

Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1178
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Strictly speaking it is not necessary. However, if you set the #define to 0, it has no effect on your code size as it is not compiled. If you ever need to debug the program you can turn it on again.

Having said that, there are probably better ways to do this that create less visual pollution in the code.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 29
Posts: 1667
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

visual pollution

Gawd, that's the term I was looking for.......
Logged

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Having said that, there are probably better ways to do this that create less visual pollution in the code.
Code folding
Logged

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 44
Posts: 2806
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nobody likes a smart old fart... That was good though... oldwizenedEE... (or is 'flatulent person' more 'politically'? correct?...) (ROTFL)

Doc
« Last Edit: June 14, 2013, 04:34:11 am by Docedison » Logged

--> WA7EMS <--
“The solution of every problem is another problem.” -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

Pages: [1]   Go Up
Jump to: