for loop is exited too soon if the internal conditions are not met

Hi, sorry if I’m asking a simple question but I’m new to this.

The for loop is supposed to stay looking for the pin1 to go high for 3 seconds and then switch to the next unless hold is pressed and kept high.

But in this case the for loop stays ON only if the pin 1 is high but not if the pin 1 is low. If the pin 1 is low the loop stops and continues with the next.

Please let me know where i’m going wrong. I want the loop to run for 3 seconds and keep scanning no matter what and stay in the loop if the hold is pressed and goto the next loop if the hold is removed.

(I could use else after the if but I want the light to stay on even if the pin1 goes high just once and so i just left out the else.)

const int led1 = 2;
const int led2 = 3;
const int led3 = 4;

int pin1 = A3;
int hold = A2;

int led = 0;
int val = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);

  pinMode(hold, INPUT);

  pinMode(pin1, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  {
    led = digitalRead(res);
    for (long unsigned int i = 0; i <= 2850; i++) {
      if (pin1 == HIGH) {
        digitalWrite(led1, HIGH);
      }                                         // don't want to use else to keep the led ON for the received signal            
      val = digitalRead(hold);

      if (val == HIGH) {
        while (val == HIGH) {

          if (pin1 == HIGH) {

            digitalWrite(led1, HIGH);
          }
          else {
            digitalWrite(led1, LOW);
          }

          val = digitalRead(hold);
        }
      }
    }
  }


  {
    led = digitalRead(res);
    for (long unsigned int i = 0; i <= 2850; i++) {
      if (pin1 == HIGH) {
        digitalWrite(led2, HIGH);
      }
      val = digitalRead(hold);

      if (val == HIGH) {
        while (val == HIGH) {

          if (pin1 == HIGH) {

            digitalWrite(led2, HIGH);
          }
          else {
            digitalWrite(led2, LOW);
          }

          val = digitalRead(hold);
        }
      }
    }
  }


  {
    led = digitalRead(res);
    for (long unsigned int i = 0; i <= 3000; i++) {
      if (pin1 == HIGH) {
        digitalWrite(led3, HIGH);
      }
      val = digitalRead(hold);

      if (val == HIGH) {
        while (val == HIGH) {

          if (pin1 == HIGH) {

            digitalWrite(led3, HIGH);
          }
          else {
            digitalWrite(led3, LOW);
          }

          val = digitalRead(hold);
        }
      }
    }
  }
}

    for (long unsigned int i = 0; i <= 2850; i++)What is that magic number in the for loop ?

in this case the for loop stays ON only if the pin 1 is high but not if the pin 1 is low.

for loops don’t stay on (or off for that matter)

      if (pin1 == HIGH)What exactly is pin1 and why would its value change ?

I’m not sure I understand exactly what you’re trying to do but this should give you some idea of how to proceed towards your goal:

       unsigned long holdTimestamp = millis();
        while (val == HIGH || millis() - holdTimestamp < 3000) {

For information on millis() see:
https://www.arduino.cc/en/Reference/Millis

Thanks for the reply UKHeliBob and Pert.

sorry for not revealing all. I just didn't want to waste your time.

I'm trying to make a system which will monitor resistance in 3 wires with an interval of 3 seconds and indicates an led if there is no continuity. The continuity will be measured by an analogue circuit which I made and will send a signal through Pin1(hardware will take care of changing the channels). If I have a problem in that channel I need to hold that channel forever and try and figure out where the wire is broken while scanning that channel and if I release the hold, it will continue with the scanning. I need the led to stay on even if the continuity is broken once until reset. I can put else over there and do it but I wanted to know if there is another way to do it. Or can I register the led when the channel's continuity is broken in some other way?

Just food for thought I guess. Sorry if you think I asked a stupid question. It means a lot if I can learn how many ways one can approach a problem and solve it.

Pert, I really appreciate the suggestion. I'm completely new to this. So after you've read the description of what I'm trying to achieve would the code still be suitable? I have a vague idea of millis and timestamps but I'm not completely aware of their usage yet. I promise I won't pose any more stupid questions until I learn about them.

Thanks again for letting me know there are people who care to reply for the tiny of questions.

I don’t think it’s a stupid question. It’s just that the description was vague enough that it was possible I didn’t interpret it exactly as you meant.

You forgot to answer UKHeliBob’s questions regarding the magic numbers in your for loops.

You also need to address the issue UKHeliBob pointed out regarding these:

if (pin1 == HIGH) {

Your code for reading the hold pin is correct but then you got it wrong with pin1. Maybe you need to take some time to read this:
https://www.arduino.cc/en/Reference/DigitalRead

saitej:
So after you’ve read the description of what I’m trying to achieve would the code still be suitable?

Sounds like it.

saitej:
I have a vague idea of millis and timestamps but I’m not completely aware of their usage yet.

millis() returns the number of milliseconds since you Arduino was last turned on or reset. This means it can be used for timing things. So when you start a process that needs to be timed you set a timestamp variable with the value of millis(). Then you can determine how long it’s been since that process began by comparing millis() to the timestamp. Thus the code:

millis() - holdTimestamp < 3000

will be true while it has been less than 3 seconds since the timestamp was set. You might be tempted to use delay() instead but that won’t work because you also need to be monitoring the state of pin1 but a delay will block any other code from running so that wouldn’t work.

Also, I do not understand why there is a while statement nested inside of an if statement with the same condition.

For UKHeliBob

There is no magic number in that. I forgot to put in the delay(150); the circuit I'm using has a little time delay hence that and the balance for 3 seconds is 2850.

"for loops don't stay on (or off for that matter)" apologies for my poor choice of words UKHeliBob. But I guess you understood what I was trying to imply. I meant the process is not staying in the loop.

"What exactly is pin1 and why would its value change ?" I've described that in the previous post.

For Pert

Thank you for explaining about the millis Pert. I'll be sure to test it and let you know.
The program I made is good enough. I know if I don't add else the program would exit the loop before 3 seconds so popped in an else statement to return a value via serial print. But I just want to know if there is another way. So I'll be sure to try it.

For vaj4088

I tried it without the if statement and for some reason it is not working so I had to put it there.
Anyways I did make a lot of files with the same program so may be I'm just confused. Let me check that again and get back to you.

I'm out of town and I'll be sure to let you guys know after trying out all your suggestions. Thanks again.

"What exactly is pin1 and why would its value change ?" I've described that in the previous post.

Actually what you said was

The continuity will be measured by an analogue circuit which I made and will send a signal through Pin1

but I don't recall seeing any mention of reading the state of pin1, which I assume is pin 1of the Arduino. If it is pin 1 then can I suggest that you do not use it because it is used by the hardware serial interface.

There is no magic number in that.

Sorry, but that is exactly what 2850 is. Actually, the for loop will iterate 2851 times. Why do you think that each iteration of the for loop takes 1 millisecond ? Use millis() for timing as has been suggested and remove the guesswork.