Pages: [1]   Go Down
Author Topic: HELP please  (Read 445 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
n00b
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm writing a code for the Arduino at the moment to control the electromagnetic locks on the doors in my office, i have set the Arduino up on a circut that will receive infomation from an input (in this case a keypad) that will tell it to open the doors (by setting the output to the lock high). The code i have so far i think is pretty sweet, but my boss wants a time delay on the locks so that when the output goes high it continues to stay that way for approx. 5 seconds so that the lock is open for that time. i've been messing about for ages trying to work out how to do this. this is my current code without the time delay:


int potPin = 1;  
int Pin = 13;  
int val = 0;  


void setup() {
  pinMode(Pin, OUTPUT);
}

void loop() {
  {val = analogRead(potPin);    
  if (val < 200) digitalWrite(Pin, HIGH);  
  else
  if (val > 200) digitalWrite(Pin, LOW);}
}

can anyone suggest a way to put a time delay in or refine my code. thanks!
Logged

rock the...

Grenoble/Lyon - France
Offline Offline
Sr. Member
****
Karma: 0
Posts: 363
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd say

Code:
int potPin = 1;    
int Pin = 13;    
int val = 0;  
 
void setup() {
  pinMode(Pin, OUTPUT);
}
 
void loop() {
   val = analogRead(potPin);    
   if (val < 200) {
      digitalWrite(Pin, HIGH);
      delay(5000);
   }
   digitalWrite(Pin, LOW);
}
Logged


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

thanks, i'll see what the boss says. ;D
Logged

rock the...

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

my boss just got back to me.... he said he didn't want to use delay as the timer >smiley-sad. thanks so much for taking the time to write that it worked the way we wanted it to as well, just not for multiple locks he said. Please anymore suggestions?
Logged

rock the...

Las Vegas, NV
Offline Offline
God Member
*****
Karma: 0
Posts: 507
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is your boss asking you to do this as a learn-to-program exercise or does he really just want the answer?

I think it would be more rewarding for you to figure out the solution for yourself, so I'll just try to point you in the right direction:

1) your loop() function is a loop.  Your program consists of loop() executed over and over again.

2) you can have loop carry out different functions based on how much time has elapsed by using if statements.  Your loop() just consists of a series of these time-based if statements so that the right events happen at the right time.  This in effect simulates a multi-threaded application in which you can deal with multiple events in parallel.  Using delay() blocks processor execution and forces serial event-handling.  Using

if (current time >= time for event 1)
{
  do_event_1();
}

Allows processor execution to continue while it's not time for event 1, so if it becomes time for other events, it's able to run them.

3) millis() returns the number of milliseconds that have elapsed since the last reset or power-up.  You can use this as your timer source.

4) What you're really looking for here is a finite-state machine.  You should use global variables to record what state you are currently in so that you know which step to next take in your main loop.  You might need multiple state variables, especially if you have multiple events going on in parallel.

- Ben
Logged


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

knowing bill it probably is a learning excersise (though he doesn't know much about the Arduino himself from what i gather), but i dont know anything much about code (i know the basics) and he has thrown me in at the deep end. but i have come up with this awesome (i reckon) peice of code thanks to you:

long endtime = 0;
int potPin = 1;
int potPin2 = 2;
int potPin3 = 3;
int Pin = 13;
int Pin2 = 6;
int Pin3 = 7;
int Pin4 = 8;
int Pin5 = 9;
int val = 0;


void setup() {
  pinMode(Pin, OUTPUT);
  pinMode(Pin2, OUTPUT);
  pinMode(Pin3, OUTPUT);
  pinMode(Pin4, OUTPUT);
  pinMode(Pin5, OUTPUT);
  
}
void loop() {
  {val = analogRead(potPin);
 
  if (val < 200){
    digitalWrite(Pin, HIGH);
    endtime=millis()+5000;
  }
  
 if(millis()>endtime) digitalWrite(Pin, LOW);}
 

 { val = analogRead(potPin2);
 if (val < 200) {
     digitalWrite(Pin2, HIGH);
     digitalWrite(Pin3, LOW);
    endtime=millis()+5000;}
    
    if(millis()>endtime) {digitalWrite(Pin2, LOW);
    digitalWrite(Pin3, LOW);}}
    
 { val = analogRead(potPin3);
 if (val < 200) {
     digitalWrite(Pin4, HIGH);
     digitalWrite(Pin5, LOW);
    endtime=millis()+5000;}
    
    if(millis()>endtime) {digitalWrite(Pin4, LOW);
    digitalWrite(Pin5, LOW);}
}
}


and the best thing is it works!!

but now he wants me to seperate some devices (the analog inputs are actually monitoring 3 sets of push buttons and fingerprint/keypad things that are on the same circut because they connect to the same door) that are on the same circut in that peice of code, i'll post it once its done.
thanks guys you've helped a bunch. ;D
Logged

rock the...

Las Vegas, NV
Offline Offline
God Member
*****
Karma: 0
Posts: 507
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm very glad to hear you have things working!  Nice job on the implementation.

- Ben
Logged


Pages: [1]   Go Up
Jump to: