user274: I am trying to make an automatic lock which stays open during different times and on different days. I want the door open Monday through Friday 8am to 5pm. On Saturdays and Sundays I want the door locked. I will be using an electric lock strike the + wire to pin 9 and - to ground on arduino. I am using an LED for development purposes for now because I don't have the lock yet. The code I have now works (stays open) for 8am to 5pm through the whole week but i want the door locked for Saturdays and Sundays. This is my code so far:
#include <Time.h>
#define doorPin 9
void setup() {
  // put your setup code here, to run once:
  pinMode(doorPin, OUTPUT); // Connected to relay to activate the door lock
  setTime(10,1,1,6,7,2014);
}

void loop() {
if (hour()>=8 && hour()<=17){
  digitalWrite(doorPin, HIGH);
}
else if (weekday()==1 || weekday()==7){
  digitalWrite(doorPin, LOW);
}


user3054: Adding to Annonomus Penguin's answer, it might be cleaner and easier to understand if you extract the variable:
bool doorOpen = ( (hour()>=8 && hour()<=17) && !(weekday()==1 || weekday()==7) );

Or in my eyes even nicer if you unnegate the last part, ymmv:
bool doorOpen = ( (hour()>=8 && hour()<=17) && (weekday()!=1 && weekday()!=7) );

And then use them directly in the code without the if statement:
digitalWrite( doorPin, doorOpen ? HIGH : LOW );

This way you make sure that the doorPin always gets set and you din't forget a path like in your if-elseif-nothing version.
Another benefit is you can test independently if the doorOpen variable is correct and if the functionality that is triggered by it is correct.

user220: This is what's happening: it's still between 8 AM and 5 PM on the weekend, so it unlocks it. Since you use an else if, it terminates the if statement and never checks the day between 8 and 5. I'm almost positive that if you checked it another time in the day, it'd be "locked."
Fixed code:
#include <Time.h>
#define doorPin 9
void setup() {
  // put your setup code here, to run once:
  pinMode(doorPin, OUTPUT); // Connected to relay to activate the door lock
  setTime(10,1,1,6,7,2014);
}

void loop() {
if ((hour()>=8 && hour()<=17) && !(weekday()==1 || weekday()==7)){
  digitalWrite(doorPin, HIGH);
}
else {
  digitalWrite(doorPin, LOW);
}

Note: ! means "not." Thus, the door is locked only if it's not a weekend and it's between 8 and 5.
Additionally, you might want to get a lock that is the other way around: unlocked only with power applied so someone can't unplug the Arduino to get access. EDIT: miscommunication, ignore this part paragraph...

274: <p>I am trying to make an automatic lock which stays open during different times and on different days. I want the door open Monday through Friday 8am to 5pm. On Saturdays and Sundays I want the door locked. I will be using an electric lock strike the + wire to pin 9 and - to ground on arduino. I am using an LED for development purposes for now because I don't have the lock yet. The code I have now works (stays open) for 8am to 5pm through the whole week but i want the door locked for Saturdays and Sundays. This is my code so far:</p>

<pre><code>#include &lt;Time.h&gt;
#define doorPin 9
void setup() {
  // put your setup code here, to run once:
  pinMode(doorPin, OUTPUT); // Connected to relay to activate the door lock
  setTime(10,1,1,6,7,2014);
}

void loop() {
if (hour()&gt;=8 &amp;&amp; hour()&lt;=17){
  digitalWrite(doorPin, HIGH);
}
else if (weekday()==1 || weekday()==7){
  digitalWrite(doorPin, LOW);
}
</code></pre>
 3054: <p>Adding to Annonomus Penguin's answer, it might be cleaner and easier to understand if you extract the variable:</p>

<pre><code>bool doorOpen = ( (hour()&gt;=8 &amp;&amp; hour()&lt;=17) &amp;&amp; !(weekday()==1 || weekday()==7) );
</code></pre>

<p>Or in my eyes even nicer if you unnegate the last part, ymmv:</p>

<pre><code>bool doorOpen = ( (hour()&gt;=8 &amp;&amp; hour()&lt;=17) &amp;&amp; (weekday()!=1 &amp;&amp; weekday()!=7) );
</code></pre>

<p>And then use them directly in the code without the if statement:</p>

<pre><code>digitalWrite( doorPin, doorOpen ? HIGH : LOW );
</code></pre>

<p>This way you make sure that the doorPin always gets set and you din't forget a path like in your if-elseif-nothing version.</p>

<p>Another benefit is you can test independently if the doorOpen variable is correct and if the functionality that is triggered by it is correct.</p>
 220: <p>This is what's happening: it's still between 8 AM and 5 PM on the weekend, so it unlocks it. Since you use an <code>else if</code>, it terminates the <code>if</code> statement and never checks the day between 8 and 5. I'm almost positive that if you checked it another time in the day, it'd be "locked."</p>

<p>Fixed code:</p>

<pre><code>#include &lt;Time.h&gt;
#define doorPin 9
void setup() {
  // put your setup code here, to run once:
  pinMode(doorPin, OUTPUT); // Connected to relay to activate the door lock
  setTime(10,1,1,6,7,2014);
}

void loop() {
if ((hour()&gt;=8 &amp;&amp; hour()&lt;=17) &amp;&amp; !(weekday()==1 || weekday()==7)){
  digitalWrite(doorPin, HIGH);
}
else {
  digitalWrite(doorPin, LOW);
}
</code></pre>

<p>Note: <code>!</code> means "not." Thus, the door is locked only if it's not a weekend and it's between 8 and 5.</p>

<p><strike>Additionally, you might want to get a lock that is the other way around: unlocked only with power applied so someone can't unplug the Arduino to get access.</strike> EDIT: miscommunication, ignore this part paragraph...</p>
