Go Down

Topic: Conditional statement help (Read 1 time) previous topic - next topic

silasmoeckel

Seems like your rather over complicating it, if I'm understanding your logic you need keep something happening until it no longer needs it then go back and service the next thing. Pop the pin of the variable and keep that state till it's fulfilled.

So something like this:
Code: [Select]

if (digitalRead(S1) == LOW && curS == 0) {
\\ Do Stuff
curS = S1; // Locks us to S1 untill it's high
}
else if (digitalRead(S1) == HIGH && curS == S1 ) }
//Undo S1 Stuff
curS = 0; // This releases the lock
}


Just repeat that block for as many input you have.  Now I would say you should de-bounce the inputs and outputs as you do not want to cycle relays and solenoids at high frequencies you will break them. 

cncb

Silas,

Do I first define curS in the beginning of the program? Because if i put it where you have it in the scope at the end after "do stuff" I get an error that "curS" wasn't defined in the scope.

cncb

My followup question was anything but vague, it's quite specific. I asked him if I need to first define the item at the beginning at the code or not, because as is it doesn't work.

iyahdub

My bad...Confused the post with another i still had open...I do apologise !!! Proves we humans aint good at multitasking !!!
10 LET Loop=Infinite
20 GO TO 10

cncb

No worries! I just want to make things work!  :)

Silas, in regards to your example, I think I see your point now.

So here is my modified code. The loop is limited to 1 switch at the moment for simplicity. (My logic of adding stuff till it breaks or doesn't work!)

This works as said, but when I release switch 1, it doesn't turn off the outputs. I can't at the moment figure out why? What am I missing?

Code: [Select]
// Digital Input Pin Constants
const int S1 = 2;    // Float Switch 1
const int S2 = 3;    // Float Switch 2
const int S3 = 4;    // Manual Switch 1
// Digital Output Pin Constants
const int K1 = 8;    // Relay K1 - Solenoid for Tank 1
const int K2 = 9;    // Relay K2 - Solenoid for Tank 2
const int K3 = 10;   // Relay K3 - Solenoid for Manual Use
const int K4 = 11;   // Relay K4 - Water Pump Relay
// Variables
int curS;
void setup() {
  // Initialize the pins, define digital I/O Pin Use
  pinMode(S1, INPUT);
  pinMode(S2, INPUT);
  pinMode(S3, INPUT);
  pinMode(K1, OUTPUT);
  pinMode(K2, OUTPUT);
  pinMode(K3, OUTPUT);
  pinMode(K4, OUTPUT);
  digitalWrite(S1, HIGH);
  digitalWrite(S2, HIGH);
  digitalWrite(S3, HIGH);
  Serial.begin(9600);
}

void loop() {

  if (digitalRead(S1) == LOW && curS == 0)
  {
    Serial.print("Filling Tank 1");
    digitalWrite(K1, HIGH);
    delay(3000);
    digitalWrite(K4, HIGH);
    curS = S1; 
  }
  else if (digitalRead(S1) == HIGH && curS == 1)
  {
    digitalWrite(K4, LOW);
    delay(3000);
    digitalWrite(K1, LOW);
    curS = 0;
  }
}


Go Up