-7

[code::

include

include

include

int hour1 = 2; int min1 = 40; int sec1 = 0; int hour2 = 2; int min2 = 39 ; int sec2 = 10;

const int light = 13; int buttonState1 = 0; int buttonState2 = 0; int buttonState3 = 0;

RTC_DS1307 rtc;

void setup() {

Serial.begin(9600); Wire.begin(); rtc.begin();

pinMode(light, OUTPUT); pinMode(2, INPUT_PULLUP); pinMode(3,INPUT_PULLUP); pinMode(4, INPUT_PULLUP); }

void loop() { DateTime now = rtc.now(); buttonState1 = digitalRead(2); buttonState2 = digitalRead(3); buttonState3 = digitalRead(4); if( buttonState1 == LOW) { if(now.hour() == hour1 && now.minute()== min1 && now.second() == sec1) { digitalWrite(light, HIGH); } if(now.hour() == hour2 && now.minute()== min2 && now.second() == sec2) { digitalWrite(light, LOW); } }

if(buttonState2 == LOW)

   { int x1 =  now.hour();
     int x2 =  now.minute();
    if (now.hour() == x1 && now.minute()== x2)
     {  
      digitalWrite(light, HIGH);

    }

if(now.hour() == hour2 && now.minute()== min2 && now.second() == sec2) { digitalWrite(light, LOW); } }

if(buttonState3 == LOW)

  {   int y1 =  now.hour();
      int y2 = now.minute();
   { if (now.hour() == y1 && now.minute()== y2)
     {  
      digitalWrite(light, LOW);

    }

if(now.hour() == hour1 && now.minute()== min1 && now.second() == sec1) { digitalWrite(light, HIGH); } } } }
]

pin 2 for pushbutton 1 pin 3 for pushbutton 2 pin 4 for pushbutton 3

my rerquirement:- pin 2 for pushbutton 1: light should be on and off as per predefined time i.e. hour1, min1, sec1 and hour2, min23 and sec2 respectively....

pin 3 for pushbutton 2: light should be on after pressing button and that time should be saved in x1, it should be off at hour2, min2, and sec2...and next time again it should be on at x1...

pin 4 for pushbutton 3: light should be off after pressing button and that time should be saved in y1, it should be on at hour1, min1, and sec1...and next time again it should be off at y1 ...\

working of program: pin 2 for pushbutton 1: working correctly pin 3 for pushbutton 2: light is starting immediately after it is in off condition...not on the time x1 and x2 saved pin 4 for pushbutton 3 light is off immediately after it is in on condition...not on the time y1 and y2 saved

  • this code is for turning on and off light according to rtc when button is pressed, but only first butto0n is working,,,,, and other two are unable to read and store the time ......can u plz suggest and suggest the corrections – pranav Sep 4 '16 at 4:38
  • 2
    Can u plz make a minimal effort to clarify your question, ie write correct English, properly format the code, describe the errors returned by the compiler... – jfpoilpret Sep 4 '16 at 7:45
6

Your problem is in these lines:

if(digitalRead(2 == HIGH)) ...

if(digitalRead(3 == HIGH)) ...

if(digitalRead(4 == HIGH)) ...

The parentheses are all wrong! The correct syntax is:

if(digitalRead(2) == HIGH) ...

if(digitalRead(3) == HIGH) ...

if(digitalRead(4) == HIGH) ...

What you had compared 2, 3 and 4 against the value HIGH, and because they aren't equal, the result was 0. The code then did a digitalRead(0) and if the result was not 0, it executed the contents of the braces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.