Delay for only one function! [SOLVED]

I made this code below and everything worked.

my problem with delay
so what I want is to make something like this:
values = 1023 "green light on"
values = 0 "red light on"
for now everything worked with me my problem start from here:
values < 1023 "delay 1min"
values > 1023 "delay 1min"

how can I make a delay for only one function not for the whole code?
second how can I make it ignore "0"
That's my code

int lightPin = 0;  

void setup(){
    Serial.begin(9600); 
    pinMode(8, OUTPUT);
    pinMode(5, OUTPUT);
}

void loop(){
    Serial.println(analogRead(lightPin)); 

    if(analogRead(lightPin) == 1023 ){    
        digitalWrite(8, HIGH);
        Serial.println("high"); }
        
        else
        {
        digitalWrite(8, LOW);
        Serial.println("low"); 
        }
    
    if(analogRead(lightPin) == 0 ){    
        digitalWrite(5, HIGH);
        Serial.println("high"); }
        
        else
        {
        digitalWrite(5, LOW);
        Serial.println("low"); 
        }

    delay(5000);
}

how can I make a delay for only one function not for the whole code?

You can't. You can, however, look at the blink without delay example to see how to do things without the need to use delay.

Come on. You don't REALLY think that you're the first to use the Arduino for a traffic light system, do you?

    if(analogRead(lightPin) = 0 ){

Bzzzt. Wrong.

Why are you assuming that the only values you will get from the analog pin are 0 or 1023?

Come on. You don't REALLY think that you're the first to use the Arduino for a traffic light system, do you?

loool nop :sweat_smile: I am not using it for traffic light, I want to make a lock from Arduino by using specific input values
To be more hard to brake, or hack, a delay will happen if you put other values or tried to know the right values

if(analogRead(lightPin) = 0 ){

I correct that to ==
hmm let me google it then, thanks anyway :slight_smile:

To be more hard to brake, or hack, a delay will happen if you put other values

What other values? If you read the potentiometer, and get something other than 0 or 1023, is that "the other values" you are referring to? If so, is that WHEN you want to delay? Why is then a bad time to delay everything?

I don't really understand what you are trying to accomplish, or what security will be obtained using a delay.

it's a lock by using only 1 resistor
I removed everything from USB flash memory and put a resistor inside.

Then I connect a female USB port to pinA0 with a resistor to GND
now if you put your USB flash memory your lock will be open, because that's resistor have 1023 values that I mention in my code.

There is a problem here, If you put a potentiometer to brake the lock it will be very easy to know the value.

What I want is to make a delay for 1min if you tried to put other values your lock will be locked.

I think my code will be something like that

if(analogRead(lightPin) >= 1) 
       Serial.println("LOCKED"); }
       
       else
       {
       Serial.println("low");
       }

But now I must make it ignore "1023"

The demo several things at a time illustrates how to manage timing with millis(). You can manage the timing of as many different things as you wish.

...R

ok Robin let me read that, Thanks :slight_smile:

Then I connect a female USB port to pinA0 with a resistor to GND

I need a picture of this. A resistor between A0 and ground will not cause a reading on A0. 5V to A0, with a resistor to ground, will cause differing amounts of current to flow, depending on the resistor. The voltage read at A0 will NOT change, though.

What I want is to make a delay for 1min if you tried to put other values your lock will be locked.

So, "other values" means a different resistor, which means different current, but the same voltage?


That's how I connect them, everything work great, My code too
I just want add this function.

So, "other values" means a different resistor, which means different current, but the same voltage?

If someone connect a potentiometer to that port, he can open the lock easily! that's my whole problem will if you have any other idea :slight_smile: share it

maybe use millis function not delay, delay is stopped everything.

@Arlove Actually Robin made a good article there about millis, I am reading it, I think that's will solve my problem

okay good luck

this is basicly millis example i hope job benefits

/* Flashing LED Version 2
 * ------------------------
 *
 * Turns on and off a light emitting diode(LED) connected to a digital  
 * pin, in intervals of 2 seconds using millis() function
 *
 */
int ledPin = 13;                 // LED connected to digital pin 13
unsigned long currentTime;
unsigned long loopTime;

void setup()
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  currentTime = millis();
  loopTime = currentTime;  
}

void loop()
{
  currentTime = millis();
  if(currentTime >= (loopTime + 1000)){  
    digitalWrite(ledPin, !digitalRead(ledPin));   // toggles the LED on/off
    loopTime = currentTime;  // Updates loopTime
  }
  // Other processing can be done here
}

that's my whole problem

So, your solution is security-by-obscurity?

Have you CONFIRMED that different value resistors result in different readings from the A0 pin? I don't believe that they will.

Your millis() example is flawed. The only way to avoid rollover issues is to subtract from millis() and test the result.

Arlove:
okay good luck

this is basicly millis example i hope job benefits

  currentTime = millis();

if(currentTime >= (loopTime + 1000)){ 
[/quote]

Have you CONFIRMED that different value resistors result in different readings from the A0 pin? I don't believe that they will.

@PaulS actually it's work :slight_smile:
This is my final project
Simple Resistor Lock

I made a problem from nothing my code work without any problems from beginning
Thanks for all and +1 for everyone, awesome guys really :slight_smile: