Pages: [1]   Go Down
Author Topic: if statements, something going wrong  (Read 488 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34725
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Guildford, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 218
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Iain
Logged

New Jersey
Online Online
Faraday Member
**
Karma: 72
Posts: 3762
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Pages: [1]   Go Up
Jump to: