Go Down

Topic: what does #endif //ECHO_TO_SERIAL do in this code (Read 1 time) previous topic - next topic

easterly81

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

Code: [Select]
#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);
        }
  }

UKHeliBob

This line
Code: [Select]
#define ECHO_TO_SERIAL 1 // echo data to serial porttells the compiler to replace ECHO_TO_SERIAL with 1 when compiling so, when the compiler comes across the line
Code: [Select]
#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: [Select]
#define ECHO_TO_SERIAL 1with
Code: [Select]
#define ECHO_TO_SERIAL 0then, 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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Nick_Pyner


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


marco_c

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.
Arduino libraries http://arduinocode.codeplex.com<br />Parola for Arduino http://parola.codeplex.com

Nick_Pyner


visual pollution


Gawd, [font=Verdana]that's[/font] the term I was looking for.......

WizenedEE


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

Docedison

#6
Jun 14, 2013, 11:30 am Last Edit: Jun 14, 2013, 11:34 am by Docedison Reason: 1
Nobody likes a smart old fart... That was good though... oldwizenedEE... (or is 'flatulent person' more 'politically'? correct?...) (ROTFL)

Doc
--> 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

Go Up