Another timing and delay question.

Hi guy’s, so i have previously started a topic called On delay timer help or On milli timer help! and got loads of help there.
I have a problem though. I have a pot that controls a timer and it works great. But, what I don’t understand is if I move the pot a new problem occurs. see below, in quote.

//Project Gown Folder 
//use potentiometer output to set time rate
int FirstPot = A0;
int SecondPot = A1;
int ThirdPot = A2;

int potRead1 = 0;
int potRead2 = 0;
int potRead3 = 0;

int FirstSwitch = 2;
int SecondSwitch = 3;
int ThirdSwitch = 4;

int FirstRelay = 12;
int SecondRelay = 11;
int ThirdRelay = 10;
int FourthRelay = 9;

int isOn1 = 1;
int isOn2 = 1;
int isOn3 = 1;

unsigned long now1 = 0; //millis() reading
unsigned long now2 = 0;
unsigned long now3 = 0;

// variables will change:
int buttonState1 = 0;   // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int count = 0;

void setup(){
  // initialize the pushbutton pin as an input:
  pinMode(FirstSwitch, INPUT_PULLUP); //<<<<<< ADDED _PULLUP 
  pinMode(SecondSwitch, INPUT_PULLUP);
  pinMode(ThirdSwitch, INPUT_PULLUP);
  pinMode(FirstRelay,OUTPUT);
  pinMode(SecondRelay,OUTPUT);
  pinMode(ThirdRelay,OUTPUT);
  pinMode(FourthRelay,OUTPUT);
 
 //Test Mode uncomment below//

   // int isOn1 = 0;
   // int isOn2 = 0;
   // int isOn3 = 0; 
  
 }

void loop(){
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(FirstSwitch);
  FirstFold();
  buttonState2 = digitalRead(SecondSwitch);
  SecondFold();
  buttonState3 = digitalRead(ThirdSwitch);
  ThirdDrop();
  
[quote]Problem occurs here. (Below quote) If I set the third pot to zero it works. But, what is supposed to happen is I set the third pot to "Whatever" and I push the switch and after a certain time the relay comes on then goes off. 
Now, what happens when I move the pot. I get the fourth relay to come on and THEN does the timing of the third relay happen. This might be confusing to the reader, but, that the only way to explain it. 

"BAD" >> Set Third pot>> push and release button>> Fourth Relay ON waits the delay OFF>> Third Relay ON then OFF 

"Good" >> Set Third pot>> push and release button>> Third Relay ON waits the time of third pot then OFF>> Fourth Relay ON waits the delay OFF[/quote]

  if (buttonState3 == HIGH) {    
      count ++;               // add 1 to the count
       if (count >= 4) {
           count = 0;
         digitalWrite(FourthRelay, HIGH);
         delay(2000);
         digitalWrite(FourthRelay, LOW);
      }
    }
  }

void FirstFold() {
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState1 == HIGH)  //so your button if not pushed is at a low then ????? 
  { 
    isOn1 = 0;////
    digitalWrite(FirstRelay,isOn1);
    now1 = millis();
  } 
    potRead1 = analogRead(FirstPot);
    if( isOn1 == 0 && millis() - now1 >=  (potRead1 * 5) )////
  {
    digitalWrite(FirstRelay, HIGH); ////
    delay(500);
    digitalWrite(FirstRelay, LOW); ////
    isOn1 = 1;////
  }
  
}

void SecondFold() {
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState2 == HIGH)  //so your button if not pushed is at a low then ????? 
  { 
    isOn2 = 0;////
    digitalWrite(SecondRelay,isOn2);
    now2 = millis();
  } 
    potRead2 = analogRead(SecondPot);
    if( isOn2 == 0 && millis() - now2 >=  (potRead2 * 5) )////
  {
    digitalWrite(SecondRelay, HIGH); ////
    delay(500);
    digitalWrite(SecondRelay, LOW); ////
    isOn2 = 1;////
  }
 }
 
 void ThirdDrop (){
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState3 == HIGH)  //so your button if not pushed is at a low then ????? 
  { 
    isOn3 = 0;////
    digitalWrite(ThirdRelay,isOn3);
    now3 = millis();
  } 
    potRead3 = analogRead(ThirdPot);
    if( isOn3 == 0 && millis() - now3 >=  (potRead3 * 5) )////
  {
    digitalWrite(ThirdRelay, HIGH); ////
    delay(1000);
    digitalWrite(ThirdRelay, LOW); ////
    isOn3 = 1;////
   }
 }

I know it’s quite the read but I wanted to be as thorough as possible. If any help would be helpfull.

int isOn1 = 1;
int isOn2 = 1;
int isOn3 = 1;

Do you really need ints to hold 0 or 1?

    isOn1 = 1;////

This is just plain stupid. // as the comment adds nothing but uselss f**king clutter to your program.

But, what I don't understand is if I move the pot a new problem occurs. see below, in quote.

I'm not going hunting for you problem. You state it outside of the code in plain and simple terms.

I bet you never bothered to check how your post looks after you posted it. It's too difficult to read the loooonnnnng lines of comment in your code.

Please write them out properly.

...R

PaulS:     isOn1 = 1;////

This is just plain stupid. // as the comment adds nothing but uselss f**king clutter to your program. I'm not going hunting for you problem. You state it outside of the code in plain and simple terms.

Well PaulS sorry for being so f**king STUPID on my code kind sir. Those ////'s are for me to know my last change. Here let me clear that up for you. Thanks for not being a prick about it.

Robin2: I bet you never bothered to check how your post looks after you posted it. It's too difficult to read the loooonnnnng lines of comment in your code.

Please write them out properly.

...R

I know it's quite lengthy, but, could you offer some assistance as to how to structure my post. That way there won't be any confusion on what goes where.

int FirstPot = A0;
int SecondPot = A1;
int ThirdPot = A2;
int potRead1 = 0;
int potRead2 = 0;
int potRead3 = 0;
int FirstSwitch = 2;
int SecondSwitch = 3;
int ThirdSwitch = 4;
int FirstRelay = 12;
int SecondRelay = 11;
int ThirdRelay = 10;
int FourthRelay = 9;
int isOn1 = 1;
int isOn2 = 1;
int isOn3 = 1;
unsigned long now1 = 0; //millis() reading
unsigned long now2 = 0;
unsigned long now3 = 0;
 // variables will change:
int buttonState1 = 0;   // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int count = 0;

void setup(){
  // initialize the pushbutton pin as an input:
  pinMode(FirstSwitch, INPUT_PULLUP); //<<<<<< ADDED _PULLUP 
  pinMode(SecondSwitch, INPUT_PULLUP);
  pinMode(ThirdSwitch, INPUT_PULLUP);
  pinMode(FirstRelay,OUTPUT);
  pinMode(SecondRelay,OUTPUT);
  pinMode(ThirdRelay,OUTPUT);
  pinMode(FourthRelay,OUTPUT);
    
 }

void loop(){
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(FirstSwitch);
  FirstFold();
  buttonState2 = digitalRead(SecondSwitch);
  SecondFold();
  buttonState3 = digitalRead(ThirdSwitch);
  ThirdDrop();
  
  if (buttonState3 == HIGH) {    
      count ++;               // add 1 to the count
       if (count >= 4) {
           count = 0;
         digitalWrite(FourthRelay, HIGH);
         delay(2000);
         digitalWrite(FourthRelay, LOW);
      }
    }
  }

Problem occurs at if (buttonState3 == HIGH) { in the loop function. If I set the third pot to zero it works. But, what is supposed to happen is I set the third pot to “Whatever” and I push the switch and after a certain time the relay comes on then goes off.
Now, what happens when I move the pot. I get the fourth relay to come on and THEN does the timing of the third relay happen. This might be confusing to the reader, but, that the only way to explain it.

“BAD” >> Set Third pot>> push and release button>> Fourth Relay ON waits the delay OFF>> Third Relay ON then OFF

“Good” >> Set Third pot>> push and release button>> Third Relay ON waits the time of third pot then OFF>> Fourth Relay ON waits the delay OFF

void FirstFold() {
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState1 == HIGH)  //so your button if not pushed is at a low then ????? 
  { 
    isOn1 = 0;////
    digitalWrite(FirstRelay,isOn1);
    now1 = millis();
  } 
    potRead1 = analogRead(FirstPot);
    if( isOn1 == 0 && millis() - now1 >=  (potRead1 * 5) )////
  {
    digitalWrite(FirstRelay, HIGH); ////
    delay(500);
    digitalWrite(FirstRelay, LOW); ////
    isOn1 = 1;////
  }
  
}

void SecondFold() {
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState2 == HIGH)  //so your button if not pushed is at a low then ????? 
  { 
    isOn2 = 0;////
    digitalWrite(SecondRelay,isOn2);
    now2 = millis();
  } 
    potRead2 = analogRead(SecondPot);
    if( isOn2 == 0 && millis() - now2 >=  (potRead2 * 5) )////
  {
    digitalWrite(SecondRelay, HIGH); ////
    delay(500);
    digitalWrite(SecondRelay, LOW); ////
    isOn2 = 1;////
  }
 }
 
 void ThirdDrop (){
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState3 == HIGH)  //so your button if not pushed is at a low then ????? 
  { 
    isOn3 = 0;////
    digitalWrite(ThirdRelay,isOn3);
    now3 = millis();
  } 
    potRead3 = analogRead(ThirdPot);
    if( isOn3 == 0 && millis() - now3 >=  (potRead3 * 5) )////
  {
    digitalWrite(ThirdRelay, HIGH); ////
    delay(1000);
    digitalWrite(ThirdRelay, LOW); ////
    isOn3 = 1;////
   }
 }

In Reply #5 I presume the stuff in quotes is the comment that I could not easily read in your code. It would be even easier to read and assimilate if it was not in quotes.

I have loaded your code into my text editor and had a look at it. You seem to have three similar functions for 3 different folds and you are saying (I think) that the third one does not work properly. I can't easily understand what these functions are trying to do - can you provide a description?

You don't seem to have any Serial.print() statements to allow you to see what your program is doing.

I wonder if your process of reading your buttons (in loop() ) works as you expect. Why not read all the buttons at the same time rather than between the folds. If reading between the folds how can the user know when to press a button?

As a separate matter, rather than have three functions with almost identical code, why not write a single function that takes parameters to tell it which items to act on - especially as it seems that the functions never overlap with each other. That way there is much less chance of silly typos allowing one function to work and another not.

...R