Timed Lock, need a little help to finish this project [easy]

hello,

i want to make a timed lock, but i am in a pinch. i very rarely program and tried my best. it is not working. please i would need a little help to fisnish this if you guys can spare a little time.

its not working, when i power it up if the servo is close it wont open. when i ground my switch pin [LOW] it close and stay close. when i disconect the switch wire [high or floating] the servo toggle between open [15sec] and closed [5sec]. it must be a simple mistake but i cant find it.

specs:
-when first powered [ON] make sure servo is [OPEN]
-if switch is [OFF] dont do anything
-when switch is [ON] make servo [CLOSE] then start timer [8hours]
-even if switch is turned [OFF] dont do anything until timer is over
-when timer is over [OPEN] servo

#include <Servo.h>
Servo servoMain; // Define our Servo
const int switchgo = 5;
int servostate = 1;

long interval = 60000; //time

void setup()
{
servoMain.attach(10); // servo on digital pin 10
servoMain.write(0); // servo open ready
servoMain.detach();
servostate = 0 ;
}

void loop()
{
unsigned long currentMillis = millis();
while (digitalRead(switchgo) == LOW) {
// Do nothing
}
if(currentMillis > interval) {
servoMain.attach(10);
servoMain.write(0); // servo open over
delay(5000);
servoMain.detach();
servostate = 0 ;
}
else ; {

if(servostate == 0) ; {
delay(10000);
servostate = 1 ;
servoMain.attach(10);
servoMain.write(85); // servo closed
delay(5000);
servoMain.detach();
}
}
}

the timer is suppose to be 8hours=28800000milis, but for testing i wrote 1min=60000

locktimer.ino (907 Bytes)

Please read the "How To Use This Forum" post at the top of any of the boards and learn to post your code correctly. Why would you use the forum without reading the how to use the forum first? Use code tags, not quote tags.

else ; {

That's a funny place for a semicolon. So is this one:

if(servostate == 0) ; {

If I were you I'd take a little time to learn the basics of the language before going much further.

if(currentMillis > interval) {

60 seconds after the Arduino starts, this will be true for the next 49 days. Is that what you want? Usually we get the time from millis when something starts and compare that to the current value to see how long it has been since that point in time. You're getting the time since the Arduino started up. See the "Blink Without Delay" example for some inspiration.

while (digitalRead(switchgo) == LOW) {
  // Do nothing
}

So if the switch is off, you want it to ignore the whole rest of the program, even the timer?

I notice that you are not using the internal pull-ups and are expecting the switch to read backwards. You want it HIGH when made and LOW when not made. Do you have an external pull-down resistor on this switch? It would be far simpler to use the pull-ups built into the chip itself and let the switch read LOW when it is made like things are normally done.

well you gave alot of good information, i will try to fix everything you told me and see if this work any better.

thank you.

[EDIT]
i think this is working as intended now. i will proceed to further testing.

#include <Servo.h>
Servo servoMain; // Define our Servo
const int switchgo = 2;
int servostate = 1;
 
    unsigned long previousMillis = 0;
    long interval = 28800000;          //time
     
    void setup() 
    {
       pinMode(2, INPUT_PULLUP);
       servoMain.attach(10); // servo on digital pin 10
       delay(1000);
       servoMain.write(0);
       delay(2000);
       servostate = 0 ; 
    }
     
    void loop()
    {
      unsigned long currentMillis = millis();
     while (digitalRead(switchgo) == HIGH) {
  // Do nothing
}
      if (currentMillis - previousMillis >= interval){
        
         
   servoMain.attach(10);  
   delay(1000);   
   servoMain.write(0);  // servo open over
   delay(2000);  
   servoMain.detach();
   servostate = 0 ;
   delay(1000);
      }
      else {
      
     if(servostate == 0) {
      delay(10000);
   servostate = 1 ;     
   servoMain.attach(10); 
   delay(1000);    
   servoMain.write(85);  // servo closed
   delay(2000);  
   servoMain.detach();
   delay(1000);
      }
      }
    }

Use the auto-format on your code and it will lines your blocks up for you. Makes the code much easier to read. Also helps keep you from mixing up braces.