IF statement with three conditions

Hi everybody!

I just need your help regarding IF statement...I need to meet the three conditions to enable the LED...

const int switch1 = 9;

int switchState = 0;

void setup () {

Serial.begin(9600);
pinMode(13,OUTPUT); // Pin of the LED to be Switched ON / OFF
pinMode(2,OUTPUT);
pinMode(switch1, INPUT);

void setup () {

Serial.begin(9600);

pinMode(13,OUTPUT); // Pin of the LED to be Switched ON / OFF
pinMode(switch1, INPUT);

void loop () {

DateTime now = RTC.now();
switchState = digitalRead(switch1);

if ((switchState == LOW) && (now.second() > 00 && now.second() < 30))
digitalWrite(13,HIGH);
if ((switchState == HIGH) && (now.second() > 31 && now.second() < 59))
digitalWrite(13,LOW);

I tried the code above but nothing happens when I tried to change the state of the switch it will just follow the condition made by the time. I connect a pull-up resistor for the switch. I tried also the button state example and it is working fine...

If that is supposed to be your complete sketch, there is no #include for the RTC, there is no setup for the RTC therefor I am guessing that you have 2 unrelated issues causing nothing to seemingly be happening.

Do you get compile errors as a result of the unreferenced RTC?

As others will be quick to point out, enclose your code in `` tags!

Regards,

Graham

That shouldn't even be compiling.
Two setups, no closing braces, no library inclusion.

That is not a complete code.... here is the complete one:

#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
Adafruit_PCD8544 display = Adafruit_PCD8544(3, 4, 5, 7, 6);

#include <DHT.h>
#define DHTPIN A2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

#include <SPI.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;

const int switch1 = 9;

int switchState = 0;

static unsigned char PROGMEM picatura[] =
{ B0001000,
B0001000,
B0001000,
B0010100,
B0100010,
B0100010,
B0011100
};

void setup () {

Serial.begin(9600);
pinMode(13,OUTPUT);
pinMode(2,OUTPUT);

pinMode(switch1, INPUT);

display.begin();

display.setContrast(50);
display.clearDisplay();

dht.begin();

display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,0);
display.println("AAAAAAA");
display.setCursor(0, 15);
display.print("by:AAAAAAAA");
display.setCursor(8, 28);
display.print("DATE & TIME");
display.setCursor(0, 36);
display.print("TEMP./HUMIDITY");
display.display();
delay (3000);
display.clearDisplay();

Wire.begin();

Wire.beginTransmission(0x68);
Wire.write(0x07);
Wire.write(0x10);
Wire.endTransmission();

RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");

}
}

int k1;
int ko;

void loop () {

DateTime now = RTC.now();
switchState = digitalRead(switch1);

if ((switchState == LOW) && (now.second() > 00 && now.second() < 30))
digitalWrite(13,HIGH);
if ((switchState == HIGH) && (now.second() > 31 && now.second() < 59))
digitalWrite(13,LOW);

int h = dht.readHumidity();
int t = dht.readTemperature();

if(t > 31)
digitalWrite(2,LOW);

if(t <= 31)
digitalWrite(2,HIGH);

int zs = now.second()/10;
int us = now.second() - zs*10;

if (us > 2 )
{
display.setTextSize(2);
display.setTextColor(BLACK);
display.setCursor(0, 0);

{ if ( now.hour() < 10)
{
display.print(" ");
display.print(now.hour(), DEC);
}
else
{
display.print(now.hour(), DEC);
}
display.setCursor(20, 0);
display.print(":");
display.setCursor(28, 0);
if ( now.minute() < 10)
{
display.print("0");
display.print(now.minute(), DEC);
}
else
{
display.print(now.minute(), DEC);
}

display.setCursor(48, 0);
display.print(":");
display.setCursor(57, 0);
if ( now.second() < 10)
{
display.print("0");
display.print(now.second(), DEC);
}
else
{
display.print(now.second(), DEC);
}
}
}
else
{
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(16, 0);
if ( now.hour() < 10)
{
display.print(" ");
display.print(now.hour(), DEC);
}
else
{
// display.setCursor(16, 0);
display.print(now.hour(), DEC);
}
display.print(":");
if ( now.minute() < 10)
{
display.print("0");
display.print(now.minute(), DEC);
}
else
{
display.print(now.minute(), DEC);
}
display.print(":");
if ( now.second() < 10)
{
display.print("0");
display.print(now.second(), DEC);
}
else
{
display.print(now.second(), DEC);
}

display.setCursor(10, 8);
if ( now.day() < 10)
{
display.print("0");
display.print(now.day(), DEC);
}
else
{
display.print(now.day(), DEC);
}
display.print("/");
if ( now.month() < 10)
{
display.print("0");
display.print(now.month(), DEC);
}
else
{
display.print(now.month(), DEC);
}
display.print("/");
display.print(now.year(), DEC);
}

if (us < 5 )
{
display.setTextSize(4);
display.setTextColor(BLACK);
display.setCursor(0,20);
display.print(t);
display.setCursor(60,20);
display.print("C");
display.setTextSize(2);
display.setTextColor(BLACK);
display.setCursor(48,20);
display.print("o");
display.display();
}
else
{
display.setTextSize(4);
display.setCursor(0,20);
display.print(h);
display.drawLine(60, 20, 60, 45, BLACK);
display.drawLine(61, 20, 61, 45, BLACK);
display.drawLine(60, 44, 80, 44, BLACK);
display.drawLine(60, 45, 80, 45, BLACK);
display.drawLine(79, 20, 79, 45, BLACK);
display.drawLine(80, 20, 80, 45, BLACK);

int hp = h/4;
for (int x = 0; x < hp+1; x++)
{
display.drawLine(60, 44-x, 80, 44-x, BLACK);
}

display.drawBitmap(65, 20, picatura, 8, 8, 1);
display.drawBitmap(70, 26, picatura, 8, 8, 1);

display.setTextSize(2);
display.setTextColor(BLACK);
display.setCursor(48,20);
display.print("%");
display.display();
}

delay (500);
display.clearDisplay();
}

How is Pin 9 wired? Do you have a pull-up or pull-down resistor?

it was wired as this http://arduino.cc/en/Tutorial/Debounce .... so it's a pull-down resistor...

  if ((switchState == LOW) && (now.second() > 00 && now.second() < 30)) 
   digitalWrite(13,HIGH);  
  if ((switchState == HIGH) && (now.second() > 31 && now.second() < 59)) 
   digitalWrite(13,LOW);

So you want the LED to come on if the button is NOT pressed and the time is between 1 and 29 seconds after the minute and stay on until the button IS pressed while the time is between 32 and 58 seconds after the minute. Why?!?

If your LED is always coming on in the first half od the minute and going off in the second half of the minute your switch pin must be bouncing up and down. This could happen if your pull-up or pull-down resistor was not properly connected.

What's supposed to happen when now.second() equals 0 or 30?

The LED represents the motor direction. HIGH - FORWARD / LOW - REVERSE
My 2nd option is to use 2 limit switches (default - sw1 (Normally Open) = HIGH / sw2 (Normally Close) = LOW)

Actually, it's not by seconds but by hours I used only the seconds just to get the results right away...

if ((switchState == LOW) && (now.second() > 00 && now.second() < 30)) 
   
   digitalWrite(13,HIGH);  
   digitalWrite(11,HIGH);   // input1 to LD293D motor driver
   digitalWrite(12,LOW);   // input2 to LD293D motor driver



if ((switchState == HIGH) && (now.second() > 31 && now.second() < 59)) 

   digitalWrite(13,LOW); 
   digitalWrite(11,LOW);   // input1 to LD293D motor driver
   digitalWrite(12,HIGH);   // input2 to LD293D motor driver

Okay, but it's still not a good practice to leave certain states dangling in the wind. Also, dont' forget that multi-line statement blocks need to be enclosed in braces:

if ((switchState == LOW) && (now.second() > 00 && now.second() < 30))  {   // Start if statement block
   
   digitalWrite(13,HIGH);  
   digitalWrite(11,HIGH);   // input1 to LD293D motor driver
   digitalWrite(12,LOW);   // input2 to LD293D motor driver
}                                                                          // Close if statement block


if ((switchState == HIGH) && (now.second() > 31 && now.second() < 59))  {  // ditto...

   digitalWrite(13,LOW); 
   digitalWrite(11,LOW);   // input1 to LD293D motor driver
   digitalWrite(12,HIGH);   // input2 to LD293D motor driver
}                                                                          // ...and ditto

I'll demonstrate the error of your logic by another example.

Heads I win, Tails you loose. :slight_smile:

ok...I'll try this but do you think the 3 conditions will work?

Please read Nick's two posts at the top of this Forum. It gives you the rules for posting here and also will help you get responses from the readers. Also, once you get your code to compile, reformat it using Ctrl-T in the IDE before posting, which makes it easier for us to read. Also, the reformat won't work if the code doesn't compile, which is a good indicator that syntax errors still remain. It's a lot easier for us to help if the code compiles.

This is the compiled code but it does not respond with the switch..

/*********************************************************************
 * This is an example sketch for our Monochrome Nokia 5110 LCD Displays
 * Pick one up today in the adafruit shop!
 * ------> http://www.adafruit.com/products/338
 * These displays use SPI to communicate, 4 or 5 pins are required to
 * interface
 * Adafruit invests time and resources providing this open source code,
 * please support Adafruit and open-source hardware by purchasing
 * products from Adafruit!
 * Written by Limor Fried/Ladyada  for Adafruit Industries.
 * BSD license, check license.txt for more information
 * All text above, and the splash screen must be included in any redistribution
 *********************************************************************/

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
// original sketck from http://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit/
// Nokia 5110 LCD (PCD8544) from https://code.google.com/p/pcd8544/


/* case for Nokia 5110 LCD (PCD8544) - LPH 7366:
 For module from China, you must connect like this:
 * Pin 1 (RST) -> Arduino digital 6 (D6)
 * Pin 2 (CE) -> Arduino digital 7 (D7)
 * Pin 3 (DC) -> Arduino digital 5 (D5)
 * Pin 4 (DIN) -> Arduino digital 4 (D4)
 * Pin 5 (CLK) - Arduino digital 3 (D3)
 * Pin 6 (Vcc) -> +5V thru adaptor module 
 * Pin 7 (LIGHT) -> +5V thru 56-100 ohms resistor (for permanent lights) or... other pin control
 * Pin 8 (GND) -> GND1 or GND2 
 */



#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
// Adafruit_PCD8544 display = Adafruit_PCD8544(SCLK, DIN, DC, CS, RST);
Adafruit_PCD8544 display = Adafruit_PCD8544(3, 4, 5, 7, 6);

#include <DHT.h>
#define DHTPIN A2     // what pin we're connected DHT11
#define DHTTYPE DHT11   // DHT 11 
DHT dht(DHTPIN, DHTTYPE);


#include <SPI.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;


//#define DHT_max = 32 ;
//int DHT_min = 19 ;

int switch1 = 9;

static unsigned char PROGMEM picatura[] =
{ 
  B0001000,
  B0001000,
  B0001000,
  B0010100,
  B0100010,
  B0100010,
  B0011100
};

void setup () {

  Serial.begin(9600);
  pinMode(13,OUTPUT); 
  pinMode(2,OUTPUT);

  pinMode(switch1, INPUT);


  display.begin();
  // init done

  // you can change the contrast around to adapt the display
  // for the best viewing!
  display.setContrast(50);
  display.clearDisplay();

  // sensor DHT for humidity and temperature 
  dht.begin();

  // Print a logo message to the LCD.

  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(0,0);
  display.println("SAMPLE");
  display.setCursor(0, 15);
  display.print("by:AAAAAA");
  display.setCursor(8, 28);
  display.print("DATE & TIME");  
  display.setCursor(0, 36);
  display.print("TEMP./HUMIDITY");
  display.display();
  delay (3000);
  display.clearDisplay(); 

  Wire.begin();

  // part code from http://tronixstuff.wordpress.com/
  Wire.beginTransmission(0x68);
  Wire.write(0x07); // move pointer to SQW address
  Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
  Wire.endTransmission();
  // end part code from http://tronixstuff.wordpress.com/

  RTC.begin();
  // RTC.adjust(DateTime(__DATE__, __TIME__));
  // if you need set clock... just remove // from line above this
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    //RTC.adjust(DateTime(__DATE__, __TIME__));
  }
}

int k1;
int ko;

void loop () {

  DateTime now = RTC.now();


  if(now.second() > 00 && now.second() < 10 && digitalRead(switch1) == HIGH)   // This the code that does not respond with the switch.   

  {
    digitalWrite(13,HIGH); // Indicator for motor forward direction
  }

  if(now.second() > 10 && now.second() < 20 && digitalRead(switch1) == LOW)    // This the code that does not respond with the switch.    

  {
    digitalWrite(13,LOW); // Indicator for motor reverse direction
  }



  int h = dht.readHumidity();
  int t = dht.readTemperature();

  if(t > 31)
    digitalWrite(12,LOW);

  if(t <= 31)  
    digitalWrite(12,HIGH);



  // need for display time 
  int zs = now.second()/10;
  int us = now.second() - zs*10;

  if (us > 2 )
  {
    display.setTextSize(2);
    display.setTextColor(BLACK);
    display.setCursor(0, 0);


    {   
      if ( now.hour() < 10)
      {
        display.print(" "); 
        display.print(now.hour(), DEC);
      }
      else
      {
        display.print(now.hour(), DEC);
      }
      display.setCursor(20, 0);
      display.print(":");
      display.setCursor(28, 0);
      if ( now.minute() < 10)
      {
        display.print("0"); 
        display.print(now.minute(), DEC);
      }
      else
      {
        display.print(now.minute(), DEC);
      }

      display.setCursor(48, 0);
      display.print(":");
      display.setCursor(57, 0);
      if ( now.second() < 10)
      {
        display.print("0"); 
        display.print(now.second(), DEC);
      }
      else
      {
        display.print(now.second(), DEC);
      }
    }
  }
  else 
  {
    display.setTextSize(1);
    display.setTextColor(BLACK);
    display.setCursor(16, 0);
    if ( now.hour() < 10)
    {
      display.print(" "); 
      display.print(now.hour(), DEC);
    }
    else
    {
      //  display.setCursor(16, 0);
      display.print(now.hour(), DEC);
    }
    display.print(":");
    if ( now.minute() < 10)
    {
      display.print("0"); 
      display.print(now.minute(), DEC);
    }
    else
    {
      display.print(now.minute(), DEC);
    }
    display.print(":");
    if ( now.second() < 10)
    {
      display.print("0"); 
      display.print(now.second(), DEC);
    }
    else
    {
      display.print(now.second(), DEC);
    }

    display.setCursor(10, 8);
    if ( now.day() < 10)
    {
      display.print("0"); 
      display.print(now.day(), DEC);
    }
    else
    {
      display.print(now.day(), DEC);
    }
    display.print("/");
    if ( now.month() < 10)
    {
      display.print("0"); 
      display.print(now.month(), DEC);
    }
    else
    {
      display.print(now.month(), DEC);
    }
    display.print("/");
    display.print(now.year(), DEC);
  }

  if (us < 5 )
  {
    display.setTextSize(4);
    display.setTextColor(BLACK);
    display.setCursor(0,20);
    display.print(t);
    display.setCursor(60,20);
    display.print("C");
    display.setTextSize(2);
    display.setTextColor(BLACK);
    display.setCursor(48,20);
    display.print("o");
    display.display();
  }
  else
  {
    display.setTextSize(4);
    // display.setTextColor(WHITE, BLACK);
    display.setCursor(0,20);
    display.print(h);
    /*   display.setCursor(60,20);
     display.print("H");
     */
    // draw an empty glass
    display.drawLine(60, 20, 60, 45, BLACK);
    display.drawLine(61, 20, 61, 45, BLACK);
    display.drawLine(60, 44, 80, 44, BLACK);
    display.drawLine(60, 45, 80, 45, BLACK);
    display.drawLine(79, 20, 79, 45, BLACK);
    display.drawLine(80, 20, 80, 45, BLACK);
    // display.display();
    int hp = h/4;
    for (int x = 0; x < hp+1; x++)
    { 
      display.drawLine(60, 44-x, 80, 44-x, BLACK);
    }
    // draw drops water
    display.drawBitmap(65, 20,  picatura, 8, 8, 1);
    display.drawBitmap(70, 26,  picatura, 8, 8, 1);

    display.setTextSize(2);
    display.setTextColor(BLACK);
    display.setCursor(48,20);
    display.print("%");
    display.display();
  }

  delay (500);
  display.clearDisplay();  
}

What do you see with these statements:

  int temp = now.second();               // ADD
  Serial.print("now.second() = ");       // ADD
  Serial.println(temp);                  // ADD

  if(now.second() > 00 && now.second() < 10 && digitalRead(switch1) == HIGH)   // This the code that does not respond with the switch.   

  {
    digitalWrite(13,HIGH); // Indicator for motor forward direction
  }

  if(now.second() > 10 && now.second() < 20 && digitalRead(switch1) == LOW)    // This the code that does not respond with the switch.    

  {
    digitalWrite(13,LOW); // Indicator for motor reverse direction
  }

This will help you find out why it's not behaving correctly.

If nothing responds to the switch there is likely a hardware error. When you measure the voltage at Pin 9 does it cleanly switch from 0V to +5V when you close the switch?

I suspect your switch wiring.
Try without the switch.

I will try to check the output of the switch....I will try also Econjack's suggestion.

Hi everbody!!!

I tried all your suggestions from the switch wiring to Econjack's code suggestions, it's working but from 01 to 29 seconds range, switching from low to high the LED will activate but switching to LOW, the LED is still activated...same problem with the other condition.

  if(digitalRead(switch1) == HIGH && now.second() > 00 && now.second() < 30)  

  {
    digitalWrite(13,HIGH); // Indicator for motor forward direction
  }

  if(digitalRead(switch1) == LOW && now.second() > 30 && now.second() < 59)     

  {
    digitalWrite(13,LOW); // Indicator for motor reverse direction
  }