Go Down

Topic: if statements, something going wrong (Read 527 times) previous topic - next topic

bowlerhatman

Hi there, I'm building an alarm clock, and I've come across an annoying problem, I assume it's my fault, but I can't figure out what might be wrong. Any help would be greatly appreciated.

Essentially what I'm doing is taking the time from a DS1307, that bit works fine. Next, I'm using two potentiometers to set the alarm time. That bit works fine too. Thirdly, alarm clocks never wake me up, so I'm building a digital sensor into my bed that will prevent the alarm from being turned off if I'm in bed. I can read the value of this sensor no worries.

So here's the problem, I want to set up a statement that will turn the alarm on if a)I'm in bed and b)the actual time is the same as the alarm time.

This is the statement as it stands, but it doesn't work. alarm_off is a digital input, I have tried using both ==1 and ==HIGH, neither work.

Code: [Select]
if( time_h == alarm_h   &&   time_m == alarm_m && alarm_off == HIGH) 
  {
    digitalWrite(LED, HIGH);
    Serial.print("alarm on");
  }


If I use this code the LED comes on, and the message is printed to the terminal:
Code: [Select]
if( time_h == alarm_h   &&   time_m == alarm_m )
  {
    digitalWrite(LED, HIGH);
    Serial.print("alarm on");
  }


Basically, the second of the two statements works perfectly, but as soon as I add a third condition, it doesn't.

Any ideas? I'm wondering if there's a limit to two conditions or something like that.

Grumpy_Mike

Put the two statements in  brackets () inside the if statement. It is the order of precedence that is killing you.

sixeyes

There doesn't appear to be anything wrong with the if statement, so it would appear alarm_off == HIGH is false.

Iain

wildbill


There doesn't appear to be anything wrong with the if statement, so it would appear alarm_off == HIGH is false.

Iain

Indeed. You'll need to post the whole thing to get more help I think.

bowlerhatman

In the end I managed to get it going, it was indeed the case that alarm_off == 1 wasn't true, but that's sorted now. On to bigger problems...

Go Up